mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add random seed
This commit is contained in:
@@ -18,7 +18,7 @@ public:
|
|||||||
static void window_reshape_callback(GLFWwindow* window, int new_width, int new_height);
|
static void window_reshape_callback(GLFWwindow* window, int new_width, int new_height);
|
||||||
|
|
||||||
static int start_cubed_application(int argc, char** argv);
|
static int start_cubed_application(int argc, char** argv);
|
||||||
|
static unsigned int seed();
|
||||||
static float delte_time();
|
static float delte_time();
|
||||||
static float get_fps();
|
static float get_fps();
|
||||||
private:
|
private:
|
||||||
@@ -38,7 +38,7 @@ private:
|
|||||||
inline static double fps_time_count = 0.0f;
|
inline static double fps_time_count = 0.0f;
|
||||||
inline static int frame_count = 0;
|
inline static int frame_count = 0;
|
||||||
inline static int fps = 0;
|
inline static int fps = 0;
|
||||||
|
inline static unsigned int m_seed = 0;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ public:
|
|||||||
Player& get_player(const std::string& name);
|
Player& get_player(const std::string& name);
|
||||||
void init_world();
|
void init_world();
|
||||||
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);
|
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);
|
||||||
|
|
||||||
|
int get_block(const glm::ivec3& block_pos) const;
|
||||||
bool is_block(const glm::ivec3& block_pos) const;
|
bool is_block(const glm::ivec3& block_pos) const;
|
||||||
|
|
||||||
void need_gen();
|
void need_gen();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
class PerlinNoise {
|
class PerlinNoise {
|
||||||
public:
|
public:
|
||||||
static void init();
|
static void init(unsigned int seed);
|
||||||
static float noise(float x, float y, float z);
|
static float noise(float x, float y, float z);
|
||||||
private:
|
private:
|
||||||
static inline bool is_init = false;
|
static inline bool is_init = false;
|
||||||
|
|||||||
11
src/app.cpp
11
src/app.cpp
@@ -6,6 +6,7 @@
|
|||||||
#include <Cubed/tools/perlin_noise.hpp>
|
#include <Cubed/tools/perlin_noise.hpp>
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
App::App() {
|
App::App() {
|
||||||
|
|
||||||
@@ -30,8 +31,10 @@ void App::init() {
|
|||||||
glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback);
|
glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback);
|
||||||
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
|
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
|
||||||
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
|
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
|
||||||
|
std::random_device d;
|
||||||
PerlinNoise::init();
|
m_seed = d();
|
||||||
|
Logger::info("Seed: {}", m_seed);
|
||||||
|
PerlinNoise::init(m_seed);
|
||||||
|
|
||||||
m_renderer.init();
|
m_renderer.init();
|
||||||
Logger::info("Renderer Init Success");
|
Logger::info("Renderer Init Success");
|
||||||
@@ -189,6 +192,10 @@ int App::start_cubed_application(int argc, char** argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int App::seed() {
|
||||||
|
return m_seed;
|
||||||
|
}
|
||||||
|
|
||||||
float App::delte_time() {
|
float App::delte_time() {
|
||||||
return delta_time;
|
return delta_time;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,10 +29,7 @@ int Chunk::get_index(int x, int y, int z) {
|
|||||||
|
|
||||||
void Chunk::gen_vertex_data() {
|
void Chunk::gen_vertex_data() {
|
||||||
m_vertexs_data.clear();
|
m_vertexs_data.clear();
|
||||||
if (m_vbo != 0) {
|
|
||||||
glDeleteBuffers(1, &m_vbo);
|
|
||||||
m_vbo = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||||
for (int y = 0; y < WORLD_SIZE_Y; y++) {
|
for (int y = 0; y < WORLD_SIZE_Y; y++) {
|
||||||
@@ -67,7 +64,11 @@ void Chunk::gen_vertex_data() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_vbo == 0) {
|
||||||
glGenBuffers(1, &m_vbo);
|
glGenBuffers(1, &m_vbo);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|||||||
@@ -230,13 +230,30 @@ bool World::is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_ex
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool World::is_block(const glm::ivec3& block_pos) const{
|
|
||||||
|
|
||||||
int world_x, world_y, world_z;
|
int World::get_block(const glm::ivec3& block_pos) const {
|
||||||
world_x = block_pos.x;
|
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||||
world_y = block_pos.y;
|
|
||||||
world_z = block_pos.z;
|
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
|
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
|
int x, y, z;
|
||||||
|
y = block_pos.y;
|
||||||
|
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
||||||
|
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
||||||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return chunk_blocks[Chunk::get_index(x, y, z)];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool World::is_block(const glm::ivec3& block_pos) const{
|
||||||
|
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||||
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||||
|
|
||||||
@@ -246,9 +263,9 @@ bool World::is_block(const glm::ivec3& block_pos) const{
|
|||||||
|
|
||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = world_y;
|
y = block_pos.y;
|
||||||
x = world_x - chunk_x * CHUCK_SIZE;
|
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
||||||
z = world_z - chunk_z * CHUCK_SIZE;
|
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,11 @@
|
|||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
void PerlinNoise::init() {
|
void PerlinNoise::init(unsigned int seed) {
|
||||||
p.resize(256);
|
p.resize(256);
|
||||||
std::iota(p.begin(), p.end(), 0);
|
std::iota(p.begin(), p.end(), 0);
|
||||||
|
|
||||||
std::mt19937 engine(SEED);
|
std::mt19937 engine(seed);
|
||||||
std::shuffle(p.begin(), p.end(), engine);
|
std::shuffle(p.begin(), p.end(), engine);
|
||||||
|
|
||||||
p.insert(p.end(), p.begin(), p.end());
|
p.insert(p.end(), p.begin(), p.end());
|
||||||
|
|||||||
Reference in New Issue
Block a user