From 123fbf4fe49468ccae84a4db2c623a1c8079d13e Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Thu, 12 Mar 2026 16:43:20 +0800 Subject: [PATCH] perf: optimize multi-chunk rendering logic --- include/Cubed/config.hpp | 58 ++++++++++- include/Cubed/gameplay/chuck.hpp | 23 ---- include/Cubed/gameplay/chuck_status.hpp | 15 --- include/Cubed/gameplay/chunk.hpp | 32 ++++++ include/Cubed/gameplay/chunk_status.hpp | 20 ++++ include/Cubed/gameplay/world.hpp | 7 +- src/gameplay/chuck.cpp | 28 ----- src/gameplay/chunk.cpp | 83 +++++++++++++++ src/gameplay/world.cpp | 133 ++++++++++++++++++++---- src/main.cpp | 114 ++------------------ 10 files changed, 315 insertions(+), 198 deletions(-) delete mode 100644 include/Cubed/gameplay/chuck.hpp delete mode 100644 include/Cubed/gameplay/chuck_status.hpp create mode 100644 include/Cubed/gameplay/chunk.hpp create mode 100644 include/Cubed/gameplay/chunk_status.hpp delete mode 100644 src/gameplay/chuck.cpp create mode 100644 src/gameplay/chunk.cpp diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index 2cde977..b734e79 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -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; \ No newline at end of file +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, + + }; \ No newline at end of file diff --git a/include/Cubed/gameplay/chuck.hpp b/include/Cubed/gameplay/chuck.hpp deleted file mode 100644 index 6ed83a9..0000000 --- a/include/Cubed/gameplay/chuck.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - - - -class Chuck { -private: - - // the index is a array of block id - std::vector m_blocks; - -public: - Chuck(); - ~Chuck(); - const std::vector& get_chuck_blocks() const; - void init_chuck(); - static int get_index(int x, int y, int z); -}; \ No newline at end of file diff --git a/include/Cubed/gameplay/chuck_status.hpp b/include/Cubed/gameplay/chuck_status.hpp deleted file mode 100644 index 6ff204a..0000000 --- a/include/Cubed/gameplay/chuck_status.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include -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{}(pos.x); - std::size_t h2 = std::hash{}(pos.y); - return h1 ^ (h2 << 1); - } - }; -}; - diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp new file mode 100644 index 0000000..f61c9cf --- /dev/null +++ b/include/Cubed/gameplay/chunk.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include +#include +#include + +class World; + +class Chunk { +private: + ChunkPos m_chunk_pos; + World& m_world; + // the index is a array of block id + std::vector m_blocks; + GLuint m_vbo; + std::vector m_vertexs_data; + +public: + Chunk(World& world, ChunkPos chunk_pos); + ~Chunk(); + const std::vector& 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& get_vertex_data() const; + void init_chunk(); + +}; \ No newline at end of file diff --git a/include/Cubed/gameplay/chunk_status.hpp b/include/Cubed/gameplay/chunk_status.hpp new file mode 100644 index 0000000..0a8bd18 --- /dev/null +++ b/include/Cubed/gameplay/chunk_status.hpp @@ -0,0 +1,20 @@ +#pragma once +#include +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{}(pos.x); + std::size_t h2 = std::hash{}(pos.z); + return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2)); + } + }; +}; + +struct Vertex { + float x, y, z; + float s, t; + float layer; +}; diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index a3c55c9..75f1abb 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -1,12 +1,14 @@ #pragma once #include -#include +#include + + class World { private: BlockRenderData m_block_render_data; - std::unordered_map m_chucks; + std::unordered_map m_chunks; public: @@ -17,5 +19,6 @@ public: void init_world(); void render(); + }; \ No newline at end of file diff --git a/src/gameplay/chuck.cpp b/src/gameplay/chuck.cpp deleted file mode 100644 index aa61a51..0000000 --- a/src/gameplay/chuck.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -Chuck::Chuck() { - init_chuck(); -} - -Chuck::~Chuck() { - -} - -const std::vector& Chuck::get_chuck_blocks() const{ - return m_blocks; -} - -void Chuck::init_chuck() { - m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0); - for (int x = 0; x < CHUCK_SIZE; x++) { - for (int y = 0; y < 2; y++) { - for (int z = 0; z < CHUCK_SIZE; z++) { - m_blocks[get_index(x, y, z)] = 1; - } - } - } -} - -int Chuck::get_index(int x, int y, int z) { - return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z; -} diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp new file mode 100644 index 0000000..8113a4f --- /dev/null +++ b/src/gameplay/chunk.cpp @@ -0,0 +1,83 @@ +#include +#include +Chunk::Chunk(World& world, ChunkPos chunk_pos) : + m_world(world), + m_chunk_pos(chunk_pos) +{ + +} + +Chunk::~Chunk() { + +} + +const std::vector& Chunk::get_chunk_blocks() const{ + return m_blocks; +} + + +int Chunk::get_index(int x, int y, int z) { + return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z; +} + +void Chunk::gen_vertex_data() { + for (int x = 0; x < CHUCK_SIZE; x++) { + for (int z = 0; z < CHUCK_SIZE; z++) { + for (int y = 0; y < CHUCK_SIZE; y++) { + int world_x = x + m_chunk_pos.x * CHUCK_SIZE; + int world_z = z + m_chunk_pos.z * CHUCK_SIZE; + int world_y = y; + const auto& block_render_data = m_world.get_block_render_data(world_x, world_y, world_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)world_x * 1.0f, + VERTICES_POS[face][i][1] + (float)world_y * 1.0f, + VERTICES_POS[face][i][2] + (float)world_z * 1.0f, + TEX_COORDS[face][i][0], + TEX_COORDS[face][i][1], + static_cast(block_render_data.block_id * 6 + face) + + }; + m_vertexs_data.emplace_back(vex); + } + } + } + + } + } +} + +GLuint Chunk::get_vbo() const{ + return m_vbo; +} + +const std::vector& Chunk::get_vertex_data() const{ + return m_vertexs_data; +} + +void Chunk::init_chunk() { + m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0); + for (int x = 0; x < CHUCK_SIZE; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < CHUCK_SIZE; z++) { + m_blocks[get_index(x, y, z)] = 1; + } + } + } + gen_vertex_data(); + + glGenBuffers(1, &m_vbo); + glBindBuffer(GL_ARRAY_BUFFER, m_vbo); + glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + + diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index e505406..a5409fc 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -7,70 +7,161 @@ World::World() { World::~World() { } -static int chuck_x, chuck_z; +static int chunk_x, chunk_z; const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) { - chuck_x = world_x / CHUCK_SIZE; - chuck_z = world_z / CHUCK_SIZE; - - auto it = m_chucks.find((ChuckPos){chuck_x, chuck_z}); - CUBED_ASSERT_MSG(it != m_chucks.end(), "Chuck not find"); + chunk_x = world_x / CHUCK_SIZE; + chunk_z = world_z / CHUCK_SIZE; + //LOG::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z); + auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z}); + CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find"); - const auto& chuck_blocks = it->second.get_chuck_blocks(); + const auto& chunk_blocks = it->second.get_chunk_blocks(); int x, y, z; y = world_y; - x = world_x - chuck_x * CHUCK_SIZE; - z = world_z - chuck_z * CHUCK_SIZE; + x = world_x - chunk_x * CHUCK_SIZE; + z = world_z - chunk_z * CHUCK_SIZE; // block id - m_block_render_data.block_id = chuck_blocks[Chuck::get_index(x, y, z)]; + m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)]; // draw_face m_block_render_data.draw_face.assign(6, true); if (x > 0 ) { - if (x > 0 && chuck_blocks[Chuck::get_index(x - 1, y, z)]) { + if (x > 0 && chunk_blocks[Chunk::get_index(x - 1, y, z)]) { m_block_render_data.draw_face[3] = false; } } if (x < CHUCK_SIZE - 1) { - if (x < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x + 1, y, z)]) { + if (x < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x + 1, y, z)]) { m_block_render_data.draw_face[1] = false; } } if (z > 0 ) { - if (z > 0 && chuck_blocks[Chuck::get_index(x, y, z - 1)]) { + if (z > 0 && chunk_blocks[Chunk::get_index(x, y, z - 1)]) { m_block_render_data.draw_face[2] = false; } } if (z < CHUCK_SIZE - 1) { - if (z < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y, z + 1)]) { + if (z < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y, z + 1)]) { m_block_render_data.draw_face[0] = false; } } if (y > 0 ) { - if (y > 0 && chuck_blocks[Chuck::get_index(x, y - 1, z)]) { + if (y > 0 && chunk_blocks[Chunk::get_index(x, y - 1, z)]) { m_block_render_data.draw_face[5] = false; } } if (y < CHUCK_SIZE - 1) { - if (y < CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y + 1, z)]) { + if (y < CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y + 1, z)]) { m_block_render_data.draw_face[4] = false; } } + if (x == 0 && world_x - 1 > 0) { + int adjacent_chunk_x = (world_x - 1) / CHUCK_SIZE; + int adjacet_chunk_z = world_z / CHUCK_SIZE; + auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); + if (adjacent != m_chunks.end()) { + int adjacent_x, adjacent_z; + const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); + int x, y, z; + y = world_y; + x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE; + z = world_z - adjacet_chunk_z * CHUCK_SIZE; + if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { + m_block_render_data.draw_face[3] = false; + } + } + + } + + if (x == CHUCK_SIZE - 1) { + int adjacent_chunk_x = (world_x + 1) / CHUCK_SIZE; + int adjacet_chunk_z = world_z / CHUCK_SIZE; + auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); + if (adjacent != m_chunks.end()) { + int adjacent_x, adjacent_z; + const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); + int x, y, z; + y = world_y; + x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE; + z = world_z - adjacet_chunk_z * CHUCK_SIZE; + if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { + m_block_render_data.draw_face[1] = false; + } + } + } + + if (z == 0 && world_z - 1 > 0) { + int adjacent_chunk_x = world_x / CHUCK_SIZE; + int adjacet_chunk_z = (world_z - 1) / CHUCK_SIZE; + + auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); + if (adjacent != m_chunks.end()) { + int adjacent_x, adjacent_z; + const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); + int x, y, z; + y = world_y; + x = world_x - adjacent_chunk_x * CHUCK_SIZE; + z = world_z - 1 - adjacet_chunk_z * CHUCK_SIZE; + if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { + m_block_render_data.draw_face[2] = false; + } + } + } + + if (z == CHUCK_SIZE - 1) { + int adjacent_chunk_x = world_x / CHUCK_SIZE; + int adjacet_chunk_z = (world_z + 1) / CHUCK_SIZE; + auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); + if (adjacent != m_chunks.end()) { + int adjacent_x, adjacent_z; + const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); + int x, y, z; + y = world_y; + x = world_x - adjacent_chunk_x * CHUCK_SIZE; + z = world_z + 1 - adjacet_chunk_z * CHUCK_SIZE; + if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { + m_block_render_data.draw_face[0] = false; + } + } + } return m_block_render_data; } + void World::init_world() { for (int s = 0; s < DISTANCE; s++) { for (int t = 0; t < DISTANCE; t++) { - ChuckPos pos{s, t}; - Chuck chuck; - m_chucks[pos] = chuck; + ChunkPos pos{s, t}; + LOG::info("Chunk Pos in init_world X: {} Z: {}", pos.x, pos.z); + m_chunks.emplace(pos, Chunk(*this, pos)); } } - + LOG::info("World init finfish"); + + for (auto& chunk_map : m_chunks) { + auto& [chunk_pos, chunk] = chunk_map; + chunk.init_chunk(); + } + } void World::render() { - + for (const auto& chunk_map : m_chunks) { + const auto& [pos, chunk] = chunk_map; + + glBindBuffer(GL_ARRAY_BUFFER, chunk.get_vbo()); + 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); + + glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size() * 3); + glBindBuffer(GL_ARRAY_BUFFER, 0); + //LOG::info("Chunk {} {} render finished", pos.x, pos.z); + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d473651..8bcea7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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_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(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(); + }