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

@@ -20,15 +20,10 @@
constexpr int NUM_VAO = 1;
constexpr int NUM_VBO = 1;
struct Vertex {
float x, y, z;
float s, t;
float layer;
};
GLuint rendering_program;
GLuint vao[NUM_VAO];
GLuint vbo[NUM_VBO];
GLuint mv_loc, proj_loc;
int width ,height;
float aspect;
@@ -42,104 +37,17 @@ Player player;
Camera camera;
TextureManager texture_manager;
World world;
std::vector<Vertex> vertex_data;
void setup_vertices(void) {
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
};
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,
};
// every block
for (int x = 0; x < DISTANCE * CHUCK_SIZE; x++) {
for (int z = 0; z < DISTANCE * CHUCK_SIZE; z++) {
for (int y = 0; y < CHUCK_SIZE; y++) {
const auto& block_render_data = world.get_block_render_data(x, y, z);
// air
if (block_render_data.block_id == 0) {
continue;
}
for (int face = 0; face < 6; face++) {
if (!block_render_data.draw_face[face]) {
continue;
}
for (int i = 0; i < 6; i++) {
Vertex vex = {
vertices_pos[face][i][0] + (float)x * 1.0f,
vertices_pos[face][i][1] + (float)y * 1.0f,
vertices_pos[face][i][2] + (float)z * 1.0f,
tex_coords[face][i][0],
tex_coords[face][i][1],
static_cast<float>(block_render_data.block_id * 6 + face)
};
vertex_data.emplace_back(vex);
}
}
}
}
}
glGenVertexArrays(NUM_VAO, vao);
glBindVertexArray(vao[0]);
glGenBuffers(NUM_VBO, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, vertex_data.size() * sizeof(Vertex), vertex_data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@@ -247,26 +155,16 @@ void display(GLFWwindow* window, double current_time) {
glUseProgram(rendering_program);
glBindVertexArray(vao[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, s));
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, layer));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
v_mat = camera.get_camera_lookat();
mv_mat = v_mat * m_mat;
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
glDrawArrays(GL_TRIANGLES, 0, vertex_data.size() * 3);
world.render();
}