perf: optimize chunk generation

This commit is contained in:
2026-04-06 17:10:36 +08:00
parent 82f0e1deae
commit f37f458b42
8 changed files with 102 additions and 143 deletions

View File

@@ -17,9 +17,22 @@ struct Block : public BlockTexture{
};
struct BlockRenderData {
glm::vec3 pos;
std::vector<bool> 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 {

View File

@@ -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<Vertex>& 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);
};

View File

@@ -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;
};
};

View File

@@ -16,7 +16,7 @@ private:
std::unordered_map<std::size_t, Player> m_players;
std::vector<glm::vec4> m_planes;
std::pair<int, int> chunk_pos(int world_x, int world_z);
std::pair<int, int> chunk_pos(int world_x, int world_z) const;
void gen_chunks();