diff --git a/include/Cubed/gameplay/block.hpp b/include/Cubed/gameplay/block.hpp index b68ba03..9178a03 100644 --- a/include/Cubed/gameplay/block.hpp +++ b/include/Cubed/gameplay/block.hpp @@ -17,9 +17,22 @@ struct Block : public BlockTexture{ }; struct BlockRenderData { - glm::vec3 pos; std::vector draw_face; unsigned block_id; + BlockRenderData() = default; + BlockRenderData(const BlockRenderData&) = default; + BlockRenderData& operator=(const BlockRenderData&) = default; + BlockRenderData(BlockRenderData&& data) : + draw_face(std::move(data.draw_face)), + block_id(data.block_id) + { + + } + BlockRenderData& operator=(BlockRenderData&& data) { + draw_face = std::move(data.draw_face); + block_id = data.block_id; + return *this; + } }; struct LookBlock { diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index a9e1218..554b35d 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -10,6 +10,9 @@ class World; class Chunk { private: + + bool m_is_gened = false; + bool m_dirty = false; ChunkPos m_chunk_pos; World& m_world; // the index is a array of block id @@ -31,7 +34,9 @@ public: GLuint get_vbo() const; const std::vector& get_vertex_data() const; void init_chunk(); - + bool is_dirty() const; + void mark_dirty(); + void clear_dirty(); void set_chunk_block(int index, unsigned id); }; \ No newline at end of file diff --git a/include/Cubed/gameplay/chunk_status.hpp b/include/Cubed/gameplay/chunk_status.hpp index 165c537..2e8e3ee 100644 --- a/include/Cubed/gameplay/chunk_status.hpp +++ b/include/Cubed/gameplay/chunk_status.hpp @@ -11,6 +11,16 @@ struct ChunkPos { return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2)); } }; + + ChunkPos operator+(const ChunkPos& pos) const{ + return ChunkPos{x + pos.x, z + pos.z}; + } + + ChunkPos& operator+=(const ChunkPos& pos) { + x += pos.x; + z += pos.z; + return *this; + }; }; diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 7427fc7..bf27241 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -16,7 +16,7 @@ private: std::unordered_map m_players; std::vector m_planes; - std::pair chunk_pos(int world_x, int world_z); + std::pair chunk_pos(int world_x, int world_z) const; void gen_chunks(); diff --git a/src/app.cpp b/src/app.cpp index 0748b26..0edba3d 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -130,7 +130,8 @@ void App::render() { } void App::run() { - + + last_time = glfwGetTime(); while(!glfwWindowShouldClose(m_window.get_glfw_window())) { update(); diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp index ee21c73..b184eed 100644 --- a/src/gameplay/chunk.cpp +++ b/src/gameplay/chunk.cpp @@ -29,7 +29,11 @@ int Chunk::get_index(int x, int y, int z) { void Chunk::gen_vertex_data() { m_vertexs_data.clear(); - glDeleteBuffers(1, &m_vbo); + if (m_vbo != 0) { + glDeleteBuffers(1, &m_vbo); + m_vbo = 0; + } + for (int x = 0; x < CHUCK_SIZE; x++) { for (int y = 0; y < WORLD_SIZE_Y; y++) { for (int z = 0; z < CHUCK_SIZE; z++) { @@ -68,6 +72,8 @@ void Chunk::gen_vertex_data() { glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); + + } GLuint Chunk::get_vbo() const{ @@ -107,14 +113,25 @@ void Chunk::init_chunk() { } } + m_is_gened = true; } +bool Chunk::is_dirty() const{ + return m_dirty; +} + +void Chunk::mark_dirty() { + m_dirty = true; +} + +void Chunk::clear_dirty() { + m_dirty = false; +} + void Chunk::set_chunk_block(int index ,unsigned id) { m_blocks[index] = id; - glDeleteBuffers(1, &m_vbo); - - gen_vertex_data(); + mark_dirty(); } diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 4ede531..b653422 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -291,6 +291,10 @@ void Player::update_lookup_block() { } void Player::update_move(float delta_time) { + // if frame rate less than 1 frame per second, don't update + if (delta_time > 1.0f) { + return; + } if (m_gait == Gait::RUN) { max_speed = RUN_SPEED; } @@ -347,10 +351,6 @@ void Player::update_move(float delta_time) { y_speed += -G * delta_time; } - - - - move_distance.y = y_speed * delta_time; // y update_y_move(); @@ -360,7 +360,8 @@ void Player::update_move(float delta_time) { update_z_move(); if (m_player_pos.y < -15.0f) { - m_player_pos = glm::vec3(0.0f, 100.0f, 0.0f); + Logger::warn("y is tow low"); + m_player_pos += glm::vec3(1.0f, 100.0f, 1.0f); } } diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index 766e05e..132e239 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -33,6 +33,7 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in y = world_y; x = world_x - chunk_x * CHUCK_SIZE; z = world_z - chunk_z * CHUCK_SIZE; + //BlockRenderData m_block_render_data; // block id m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)]; if (m_block_render_data.block_id == 0) { @@ -83,6 +84,7 @@ Player& World::get_player(const std::string& name){ void World::init_world() { + auto t1 = std::chrono::system_clock::now(); for (int s = 0; s < DISTANCE; s++) { for (int t = 0; t < DISTANCE; t++) { int ns = s - DISTANCE / 2; @@ -106,7 +108,9 @@ void World::init_world() { chunk.gen_vertex_data(); } - Logger::info("Chunk Block Init Finish"); + auto t2 = std::chrono::system_clock::now(); + auto d = std::chrono::duration_cast(t2 - t1); + Logger::info("Chunk Block Init Finish, Time Consuming: {}", d); // init players m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer")); Logger::info("TestPlayer Create Finish"); @@ -127,7 +131,7 @@ void World::render(const glm::mat4& mvp_matrix) { glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); - glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size() * 3); + glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size()); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -137,7 +141,7 @@ void World::render(const glm::mat4& mvp_matrix) { } -std::pair World::chunk_pos(int world_x, int world_z) { +std::pair World::chunk_pos(int world_x, int world_z) const{ int chunk_x, chunk_z; if (world_x < 0) { chunk_x = (world_x + 1) / CHUCK_SIZE - 1; @@ -155,7 +159,6 @@ std::pair World::chunk_pos(int world_x, int world_z) { } void World::gen_chunks() { - Logger::info("start gen chunks"); const auto& player = get_player("TestPlayer"); const auto& player_pos = player.get_player_pos(); @@ -172,7 +175,6 @@ void World::gen_chunks() { } } CUBED_ASSERT_MSG(!cur_chunks.empty(), "cur chunks is empty!!"); - Logger::info("Cur Chunk pos gen finish, size {}", cur_chunks.size()); for (auto it = m_chunks.begin(); it != m_chunks.end(); ) { if (cur_chunks.find(it->first) == cur_chunks.end()) { @@ -189,25 +191,26 @@ void World::gen_chunks() { pre_gen_chunks.push_back(pos); } } - - Logger::info("start to init new chunk"); - + if (pre_gen_chunks.empty()) { + return; + } + static const ChunkPos CHUNK_DIR[] { + {1, 0}, {-1, 0}, {0, -1}, {0, 1} + }; for (const auto& pos : pre_gen_chunks) { auto it = m_chunks.find(pos); CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk Don't find"); //Logger::info("Init Chunk {} {}", pos.x, pos.z); it->second.init_chunk(); - + it->second.mark_dirty(); + for (const auto& dir : CHUNK_DIR) { + auto it = m_chunks.find(pos + dir); + if (it != m_chunks.end()) { + it->second.mark_dirty(); + } + } } - // After block gen fininshed - for (auto& chunk_map : m_chunks) { - auto& [chunk_pos, chunk] = chunk_map; - - chunk.gen_vertex_data(); - - } - Logger::info("gen chunks finish"); } void World::need_gen() { @@ -228,24 +231,12 @@ bool World::is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_ex return true; } bool World::is_block(const glm::ivec3& block_pos) const{ - int chunk_x, chunk_z; + int world_x, world_y, world_z; world_x = block_pos.x; world_y = block_pos.y; world_z = block_pos.z; - - if (world_x < 0) { - chunk_x = (world_x + 1) / CHUCK_SIZE - 1; - } - if (world_x >= 0) { - chunk_x = world_x / CHUCK_SIZE; - } - if (world_z < 0) { - chunk_z = (world_z + 1) / CHUCK_SIZE - 1; - } - if (world_z >= 0) { - chunk_z = world_z / CHUCK_SIZE; - } + auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z); auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z}); @@ -271,24 +262,12 @@ bool World::is_block(const glm::ivec3& block_pos) const{ void World::set_block(const glm::ivec3& block_pos, unsigned id) { - int chunk_x, chunk_z; + int world_x, world_y, world_z; world_x = block_pos.x; world_y = block_pos.y; world_z = block_pos.z; - - if (world_x < 0) { - chunk_x = (world_x + 1) / CHUCK_SIZE - 1; - } - if (world_x >= 0) { - chunk_x = world_x / CHUCK_SIZE; - } - if (world_z < 0) { - chunk_z = (world_z + 1) / CHUCK_SIZE - 1; - } - if (world_z >= 0) { - chunk_z = world_z / CHUCK_SIZE; - } + auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z); auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z}); @@ -306,96 +285,21 @@ void World::set_block(const glm::ivec3& block_pos, unsigned id) { it->second.set_chunk_block(Chunk::get_index(x, y, z), id); + static const glm::ivec3 NEIGHBOR_DIRS[] = { + {1, 0, 0}, {-1, 0, 0}, {0, 0, -1}, {0, 0, 1} + }; - int adjacent_chunk_x; - int adjacent_chunk_z; - int adj_world_x; - int adj_world_z; - if (x == 0) { - adj_world_x = world_x - 1; - adj_world_z = world_z; - if (adj_world_x < 0) { - adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1; - } - if (adj_world_x >= 0) { - adjacent_chunk_x = adj_world_x / CHUCK_SIZE; - } - if (adj_world_z < 0) { - adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1; - } - if (adj_world_z >= 0) { - adjacent_chunk_z = adj_world_z / CHUCK_SIZE; - } - auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z}); - if (adjacent != m_chunks.end()) { - adjacent->second.gen_vertex_data(); - } - - } + for (const auto& dir : NEIGHBOR_DIRS) { + glm::ivec3 neighbor = block_pos + dir; + + auto [cx, cz] = chunk_pos(neighbor.x, neighbor.z); + auto it = m_chunks.find({cx, cz}); + if (it != m_chunks.end()) { + it->second.mark_dirty(); - if (x == CHUCK_SIZE - 1) { - adj_world_x = world_x + 1; - adj_world_z = world_z; - if (adj_world_x < 0) { - adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1; - } - if (adj_world_x >= 0) { - adjacent_chunk_x = adj_world_x/ CHUCK_SIZE; - } - if (adj_world_z < 0) { - adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1; - } - if (adj_world_z >= 0) { - adjacent_chunk_z = adj_world_z / CHUCK_SIZE; - } - auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z}); - if (adjacent != m_chunks.end()) { - adjacent->second.gen_vertex_data(); } } - if (z == 0) { - adj_world_x = world_x; - adj_world_z = world_z - 1; - if (adj_world_x < 0) { - adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1; - } - if (adj_world_x >= 0) { - adjacent_chunk_x = adj_world_x / CHUCK_SIZE; - } - if (adj_world_z < 0) { - adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1; - } - if (adj_world_z >= 0) { - adjacent_chunk_z = adj_world_z / CHUCK_SIZE; - } - - auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z}); - if (adjacent != m_chunks.end()) { - adjacent->second.gen_vertex_data(); - } - } - - if (z == CHUCK_SIZE - 1) { - adj_world_x = world_x; - adj_world_z = world_z + 1; - if (adj_world_x < 0) { - adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1; - } - if (adj_world_x >= 0) { - adjacent_chunk_x = adj_world_x/ CHUCK_SIZE; - } - if (adj_world_z < 0) { - adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1; - } - if (adj_world_z >= 0) { - adjacent_chunk_z = adj_world_z / CHUCK_SIZE; - } - auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z}); - if (adjacent != m_chunks.end()) { - adjacent->second.gen_vertex_data(); - } - } } @@ -407,4 +311,12 @@ void World::update(float delta_time) { gen_chunks(); need_gen_chunk = false; } + + // unified compute vertex data before rendering + for (auto& [key, chunk] : m_chunks) { + if (chunk.is_dirty()) { + chunk.gen_vertex_data(); + chunk.clear_dirty(); + } + } } \ No newline at end of file