perf: optimize multi-chunk rendering logic

This commit is contained in:
2026-03-12 16:43:20 +08:00
parent b4f41e9e69
commit 123fbf4fe4
10 changed files with 315 additions and 198 deletions

View File

@@ -4,4 +4,60 @@ constexpr int WORLD_SIZE_Z = 32;
constexpr int WORLD_SIZE_Y = 16;
constexpr int MAX_BLOCK_NUM = 2;
constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 1;
constexpr int DISTANCE = 2;
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====
-0.5f, -0.5f, 0.5f, // bottom left
-0.5f, 0.5f, 0.5f, // top left
0.5f, 0.5f, 0.5f, // top right
0.5f, 0.5f, 0.5f, // top right
0.5f, -0.5f, 0.5f, // bottom right
-0.5f, -0.5f, 0.5f, // bottom left
// ===== right (x = +1) =====
0.5f, -0.5f, 0.5f, // bottom front
0.5f, -0.5f, -0.5f, // bottom back
0.5f, 0.5f, -0.5f, // top back
0.5f, 0.5f, -0.5f, // top back
0.5f, 0.5f, 0.5f, // top front
0.5f, -0.5f, 0.5f, // bottom front
// ===== back (z = -1) =====
-0.5f, -0.5f, -0.5f, // bottom left
0.5f, -0.5f, -0.5f, // bottom right
0.5f, 0.5f, -0.5f, // top right
0.5f, 0.5f, -0.5f, // top right
-0.5f, 0.5f, -0.5f, // top left
-0.5f, -0.5f, -0.5f, // bottom left
// ===== left (x = -1) =====
-0.5f, -0.5f, -0.5f, // bottom back
-0.5f, -0.5f, 0.5f, // bottom front
-0.5f, 0.5f, 0.5f, // top front
-0.5f, 0.5f, 0.5f, // top front
-0.5f, 0.5f, -0.5f, // top back
-0.5f, -0.5f, -0.5f, // bottom back
// ===== top (y = +1) =====
-0.5f, 0.5f, -0.5f, // back left
0.5f, 0.5f, -0.5f, // back right
0.5f, 0.5f, 0.5f, // front right
0.5f, 0.5f, 0.5f, // front right
-0.5f, 0.5f, 0.5f, // front left
-0.5f, 0.5f, -0.5f, // back left
// ===== bottom (y = -1) =====
-0.5f, -0.5f, 0.5f, // front left
0.5f, -0.5f, 0.5f, // front right
0.5f, -0.5f, -0.5f, // back right
0.5f, -0.5f, -0.5f, // back right
-0.5f, -0.5f, -0.5f, // back left
-0.5f, -0.5f, 0.5f // front left
};
constexpr float TEX_COORDS[6][6][2] = {
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
};

View File

@@ -1,23 +0,0 @@
#pragma once
#include <cstdint>
#include <Cubed/config.hpp>
#include <Cubed/gameplay/chuck_status.hpp>
#include <Cubed/gameplay/block.hpp>
class Chuck {
private:
// the index is a array of block id
std::vector<uint8_t> m_blocks;
public:
Chuck();
~Chuck();
const std::vector<uint8_t>& get_chuck_blocks() const;
void init_chuck();
static int get_index(int x, int y, int z);
};

View File

@@ -1,15 +0,0 @@
#pragma once
#include <functional>
struct ChuckPos {
int x;
int y;
bool operator==(const ChuckPos&) const = default;
struct Hash {
std::size_t operator()(const ChuckPos& pos) const{
std::size_t h1 = std::hash<int>{}(pos.x);
std::size_t h2 = std::hash<int>{}(pos.y);
return h1 ^ (h2 << 1);
}
};
};

View File

@@ -0,0 +1,32 @@
#pragma once
#include <cstdint>
#include <Cubed/config.hpp>
#include <Cubed/gameplay/chunk_status.hpp>
#include <Cubed/gameplay/block.hpp>
class World;
class Chunk {
private:
ChunkPos m_chunk_pos;
World& m_world;
// the index is a array of block id
std::vector<uint8_t> m_blocks;
GLuint m_vbo;
std::vector<Vertex> m_vertexs_data;
public:
Chunk(World& world, ChunkPos chunk_pos);
~Chunk();
const std::vector<uint8_t>& get_chunk_blocks() const;
static int get_index(int x, int y, int z);
void gen_vertex_data();
GLuint get_vbo() const;
const std::vector<Vertex>& get_vertex_data() const;
void init_chunk();
};

View File

@@ -0,0 +1,20 @@
#pragma once
#include <functional>
struct ChunkPos {
int x;
int z;
bool operator==(const ChunkPos&) const = default;
struct Hash {
std::size_t operator()(const ChunkPos& pos) const{
std::size_t h1 = std::hash<int>{}(pos.x);
std::size_t h2 = std::hash<int>{}(pos.z);
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
}
};
};
struct Vertex {
float x, y, z;
float s, t;
float layer;
};

View File

@@ -1,12 +1,14 @@
#pragma once
#include <unordered_map>
#include <Cubed/gameplay/chuck.hpp>
#include <Cubed/gameplay/chunk.hpp>
class World {
private:
BlockRenderData m_block_render_data;
std::unordered_map<ChuckPos , Chuck, ChuckPos::Hash> m_chucks;
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
public:
@@ -17,5 +19,6 @@ public:
void init_world();
void render();
};