From 553955841bb0e5e3f2e8ef42e778d6be4af5d4da Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 19 Apr 2026 17:49:08 +0800 Subject: [PATCH] fix: missing face culling for old chunks during generation --- include/Cubed/gameplay/chunk.hpp | 14 ++- include/Cubed/gameplay/world.hpp | 14 ++- src/gameplay/chunk.cpp | 41 ++++++-- src/gameplay/world.cpp | 169 +++++++++++++++++++------------ 4 files changed, 161 insertions(+), 77 deletions(-) diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index b98320f..7045eea 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -13,7 +13,10 @@ class World; class Chunk { private: std::atomic m_dirty {false}; - + std::atomic m_need_upload{true}; + std::atomic m_is_on_gen_vertex_data {false}; + std::atomic m_vertex_sum = 0; + std::mutex m_vertexs_data_mutex; static constexpr int SIZE_X = CHUCK_SIZE; static constexpr int SIZE_Y = WORLD_SIZE_Y; static constexpr int SIZE_Z = CHUCK_SIZE; @@ -39,8 +42,8 @@ public: ~Chunk(); Chunk(const Chunk&) = delete; Chunk& operator=(const Chunk&) = delete; - Chunk(Chunk&&); - Chunk& operator=(Chunk&&); + Chunk(Chunk&&) noexcept; + Chunk& operator=(Chunk&&) noexcept; Biome get_biome() const; @@ -58,11 +61,14 @@ public: void upload_to_gpu(); GLuint get_vbo() const; - const std::vector& get_vertex_data() const; + size_t get_vertex_sum() const; bool is_dirty() const; void mark_dirty(); + bool is_need_upload() const; + void need_upload(); + void set_chunk_block(int index, unsigned id); }; \ No newline at end of file diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index e1d1115..5545e7a 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -5,7 +5,7 @@ #include #include #include - +#include #include #include @@ -20,7 +20,11 @@ struct ChunkRenderSnapshot { class Player; class World { -private: +private: + using ChunkPtrUpdateList = std::vector>; + using ChunkUpdateList = std::vector>; + using ConstChunkMap = std::unordered_map; + using ChunkPosSet = std::unordered_set; glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f}; std::unordered_map m_chunks; std::unordered_map m_players; @@ -43,7 +47,11 @@ private: std::vector> m_new_chunk_queue; void gen_chunks_internal(); - + void sync_player_pos(glm::vec3& player_pos); + void compute_required_chunks(ChunkPosSet& required_chunks); + void sync_and_collect_missing_chunks(std::vector& , const ChunkPosSet&); + void build_neighbor_context_for_new_chunks(ConstChunkMap& new_chunks_neighbor, ChunkPtrUpdateList& affected_neighbor, const ChunkUpdateList& new_chunks); + void build_neighbor_context_for_affected_neighbors(ChunkPtrUpdateList&, ConstChunkMap&); void start_gen_thread(); void stop_gen_thread(); diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp index fdd6c11..b667f7d 100644 --- a/src/gameplay/chunk.cpp +++ b/src/gameplay/chunk.cpp @@ -23,19 +23,22 @@ Chunk::~Chunk() { } -Chunk::Chunk(Chunk&& other) : +Chunk::Chunk(Chunk&& other) noexcept : m_vbo(other.m_vbo), m_chunk_pos(std::move(other.m_chunk_pos)), m_world(other.m_world), m_blocks(std::move(other.m_blocks)), m_dirty(other.is_dirty()), m_vertexs_data(std::move(other.m_vertexs_data)), - m_biome(other.m_biome) + m_biome(other.m_biome), + m_is_on_gen_vertex_data(other.m_is_on_gen_vertex_data.load()), + m_need_upload(other.m_need_upload.load()), + m_vertex_sum(other.m_vertex_sum.load()) { other.m_vbo = 0; } -Chunk& Chunk::operator=(Chunk&& other) { +Chunk& Chunk::operator=(Chunk&& other) noexcept { m_vbo = other.m_vbo; other.m_vbo = 0; m_chunk_pos = std::move(other.m_chunk_pos); @@ -43,6 +46,9 @@ Chunk& Chunk::operator=(Chunk&& other) { m_dirty = other.is_dirty(); m_vertexs_data = std::move(other.m_vertexs_data); m_biome = other.m_biome; + m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load(); + m_need_upload = other.m_need_upload.load(); + m_vertex_sum = other.m_vertex_sum.load(); return *this; } @@ -68,6 +74,11 @@ int Chunk::get_index(const glm::vec3& pos) { } void Chunk::gen_vertex_data(const std::array*, 4>& neighbor_block) { + if (m_is_on_gen_vertex_data) { + return; + } + m_is_on_gen_vertex_data = true; + std::lock_guard lk(m_vertexs_data_mutex); m_vertexs_data.clear(); static const glm::ivec3 DIR[6] = { @@ -174,15 +185,20 @@ void Chunk::gen_vertex_data(const std::array*, 4>& ne } } - + m_vertex_sum = m_vertexs_data.size(); + m_need_upload = true; + m_is_on_gen_vertex_data = false; } GLuint Chunk::get_vbo() const{ return m_vbo; } -const std::vector& Chunk::get_vertex_data() const{ - return m_vertexs_data; +size_t Chunk::get_vertex_sum() const { + if (m_vertex_sum == 0) { + Logger::warn("m_vertex_sum is 0"); + } + return m_vertex_sum.load(); } void Chunk::init_chunk() { @@ -192,16 +208,17 @@ void Chunk::init_chunk() { void Chunk::upload_to_gpu() { - CUBED_ASSERT(is_dirty()); + CUBED_ASSERT(is_need_upload()); if (m_vbo == 0) { glGenBuffers(1, &m_vbo); } + std::lock_guard lk(m_vertexs_data_mutex); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // after fininshed it, can use clear_dirty(); - + m_need_upload = false; } bool Chunk::is_dirty() const{ @@ -216,6 +233,14 @@ void Chunk::clear_dirty() { m_dirty = false; } +bool Chunk::is_need_upload() const { + return m_need_upload.load(); +} + +void Chunk::need_upload() { + m_need_upload = true; +} + void Chunk::set_chunk_block(int index ,unsigned id) { m_blocks[index] = id; mark_dirty(); diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index e36e090..dbd9459 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -8,7 +8,6 @@ #include #include -#include static constexpr ChunkPos CHUNK_DIR[] { {1, 0}, {-1, 0}, {0, 1}, {0, -1} @@ -202,81 +201,39 @@ ChunkPos World::chunk_pos(int world_x, int world_z) { } void World::gen_chunks_internal() { - glm::vec3 player_pos; - { - std::lock_guard lk(m_gen_player_pos_mutex); - player_pos = m_gen_player_pos; - } + ChunkPosSet required_chunks; + compute_required_chunks(required_chunks); - int x = std::floor(player_pos.x); - int z = std::floor(player_pos.z); - auto [chunk_x, chunk_z] = chunk_pos(x, z); - std::unordered_set cur_chunks; - std::vector pre_gen_chunks; - cur_chunks.reserve(DISTANCE * DISTANCE); - int half = DISTANCE / 2; - for (int u = chunk_x - half; u <= chunk_x + half; ++u) { - for (int v = chunk_z - half; v <= chunk_z + half; ++v) { - cur_chunks.emplace(u, v); - } - } - CUBED_ASSERT_MSG(!cur_chunks.empty(), "cur chunks is empty!!"); - std::vector> new_chunks; - { - std::lock_guard lk(m_chunks_mutex); - for (auto it = m_chunks.begin(); it != m_chunks.end(); ) { - if (cur_chunks.find(it->first) == cur_chunks.end()) { - it = m_chunks.erase(it); - } else { - ++it; - } - } - - for (auto pos: cur_chunks) { - auto it = m_chunks.find(pos); - if (it == m_chunks.end()) { - pre_gen_chunks.push_back(pos); - } - } - } - - Logger::info("New Gen Chunks Sum: {}", pre_gen_chunks.size()); - if (pre_gen_chunks.empty()) { + CUBED_ASSERT_MSG(!required_chunks.empty(), "required chunks is empty!!"); + + std::vector need_gen_chunks_pos; + sync_and_collect_missing_chunks(need_gen_chunks_pos, required_chunks); + + Logger::info("New Gen Chunks Sum: {}", need_gen_chunks_pos.size()); + if (need_gen_chunks_pos.empty()) { return; } - - for (auto& pos : pre_gen_chunks) { + ChunkUpdateList new_chunks; + for (auto& pos : need_gen_chunks_pos) { new_chunks.push_back({pos, Chunk(*this, pos)}); } - std::unordered_map neighbor; - - { - std::lock_guard lk(m_chunks_mutex); - for (auto& [pos, chunk] : new_chunks) { - for (const auto& dir : CHUNK_DIR) { - auto it = m_chunks.find(pos + dir); - if (it != m_chunks.end()) { - neighbor.insert({it->first, &(it->second)}); - } - } - } - } + ConstChunkMap new_chunks_neighbor; + // affected neighbor + ChunkPtrUpdateList affected_neighbor; + build_neighbor_context_for_new_chunks(new_chunks_neighbor, affected_neighbor,new_chunks); std::array*, 4> neighbor_block; - + // build new chunk, but the neighbor in m_chunks also need to re-build for (auto& [pos, chunk] : new_chunks) { - chunk.init_chunk(); - neighbor.insert({pos, &chunk}); - } for (auto& [pos, chunk] : new_chunks) { for (int i = 0; i < 4; i++) { - auto it = neighbor.find(pos + CHUNK_DIR[i]); - if (it != neighbor.end()) { + auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]); + if (it != new_chunks_neighbor.end()) { neighbor_block[i] = &(it->second->get_chunk_blocks()); } else { neighbor_block[i] = nullptr; @@ -284,6 +241,20 @@ void World::gen_chunks_internal() { } chunk.gen_vertex_data(neighbor_block); } + build_neighbor_context_for_affected_neighbors(affected_neighbor, new_chunks_neighbor); + + for (auto& [pos, chunk] : affected_neighbor) { + for (int i = 0; i < 4; i++) { + auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]); + if (it != new_chunks_neighbor.end()) { + neighbor_block[i] = &(it->second->get_chunk_blocks()); + } else { + neighbor_block[i] = nullptr; + } + } + chunk->gen_vertex_data(neighbor_block); + chunk->need_upload(); + } { std::lock_guard lk(m_new_chunk_queue_mutex); @@ -295,6 +266,77 @@ void World::gen_chunks_internal() { } +void World::sync_player_pos(glm::vec3& player_pos) { + std::lock_guard lk(m_gen_player_pos_mutex); + player_pos = m_gen_player_pos; +} + +void World::compute_required_chunks(ChunkPosSet& required_chunks) { + glm::vec3 player_pos; + sync_player_pos(player_pos); + + int x = std::floor(player_pos.x); + int z = std::floor(player_pos.z); + auto [chunk_x, chunk_z] = chunk_pos(x, z); + + + required_chunks.reserve(DISTANCE * DISTANCE); + int half = DISTANCE / 2; + for (int u = chunk_x - half; u <= chunk_x + half; ++u) { + for (int v = chunk_z - half; v <= chunk_z + half; ++v) { + required_chunks.emplace(u, v); + } + } +} + +void World::sync_and_collect_missing_chunks(std::vector& need_gen_chunks_pos, const ChunkPosSet& required_chunks) { + std::lock_guard lk(m_chunks_mutex); + for (auto it = m_chunks.begin(); it != m_chunks.end(); ) { + if (required_chunks.find(it->first) == required_chunks.end()) { + it = m_chunks.erase(it); + } else { + ++it; + } + } + + for (auto pos: required_chunks) { + auto it = m_chunks.find(pos); + if (it == m_chunks.end()) { + need_gen_chunks_pos.push_back(pos); + } + } +} + +void World::build_neighbor_context_for_new_chunks(ConstChunkMap& new_chunks_neighbor, ChunkPtrUpdateList& affected_neighbor,const ChunkUpdateList& new_chunks) { + { + std::lock_guard lk(m_chunks_mutex); + for (auto& [pos, chunk] : new_chunks) { + for (auto& dir : CHUNK_DIR) { + auto it = m_chunks.find(pos + dir); + if (it != m_chunks.end()) { + new_chunks_neighbor.insert({it->first, &(it->second)}); + affected_neighbor.push_back({it->first, &(it->second)}); + } + } + } + } + for (auto& [pos, chunk] : new_chunks) { + new_chunks_neighbor.insert({pos, &chunk}); + } +} + +void World::build_neighbor_context_for_affected_neighbors(ChunkPtrUpdateList& affected_neighbor, ConstChunkMap& new_chunks_neighbor) { + std::lock_guard lk(m_chunks_mutex); + for (auto& [pos, chunk] : affected_neighbor) { + for (auto& dir : CHUNK_DIR) { + auto it = m_chunks.find(pos + dir); + if (it != m_chunks.end()) { + new_chunks_neighbor.insert({it->first, &(it->second)}); + } + } + } +} + void World::start_gen_thread() { m_gen_running = true; Logger::info("Gen Thread Started"); @@ -484,9 +526,12 @@ void World::update(float delta_time) { chunk.upload_to_gpu(); } if (!chunk.is_dirty()) { + if (chunk.is_need_upload()) { + chunk.upload_to_gpu(); + } m_render_snapshots.push_back({ chunk.get_vbo(), - chunk.get_vertex_data().size(), + chunk.get_vertex_sum(), glm::vec3(static_cast(pos.x * CHUCK_SIZE) + static_cast(CHUCK_SIZE / 2), static_cast(WORLD_SIZE_Y/ 2), static_cast(pos.z * CHUCK_SIZE) + static_cast(CHUCK_SIZE / 2)), glm::vec3(static_cast(CHUCK_SIZE / 2), static_cast(WORLD_SIZE_Y / 2), static_cast(CHUCK_SIZE / 2)) }