From 105fff00299ccf0115eedb091c4eceb9cbd0dbdd Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Fri, 26 May 2023 23:05:29 +0200 Subject: [PATCH] write number of triangles to Chunk only when sending to GPU previously the current number of vertices was stored in the chunk, and the same variable got later reused for the number of triangles. This modification increases clarity and avoid glitches when blockpicking --- include/chunk.hpp | 2 +- include/chunkmesher.hpp | 2 ++ src/chunkmesher.cpp | 32 ++++++++++++++++---------------- src/renderer.cpp | 6 ++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index 569a006..e29f226 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -56,7 +56,7 @@ namespace Chunk std::unique_ptr getBlocksArray(int* len) { return (this->blocks.toArray(len)); } public: - GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, numVertices{0}; + GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, numTriangles{0}; std::atomic unload_timer{0}; private: diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index ff26f4d..ab5ea3e 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -15,6 +15,8 @@ namespace chunkmesher{ struct MeshData{ Chunk::Chunk* chunk; + GLuint numVertices{0}; + std::vector vertices; std::vector colors; std::vector indices; diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index b787d73..000111a 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -36,7 +36,7 @@ void mesh(Chunk::Chunk* chunk) */ // Cleanup previous data - chunk->numVertices = 0; + mesh_data->numVertices = 0; mesh_data->chunk = chunk; mesh_data->vertices.clear(); mesh_data->indices.clear(); @@ -202,7 +202,7 @@ void mesh(Chunk::Chunk* chunk) void sendtogpu(MeshData* mesh_data) { - if (mesh_data->chunk->numVertices > 0) + if (mesh_data->numVertices > 0) { if(mesh_data->chunk->VAO == 0) mesh_data->chunk->createBuffers(); @@ -234,7 +234,7 @@ void sendtogpu(MeshData* mesh_data) glBindVertexArray(0); // save the number of indices of the mesh, it is needed later for drawing - mesh_data->chunk->numVertices = (GLuint)(mesh_data->indices.size()); + mesh_data->chunk->numTriangles = (GLuint)(mesh_data->indices.size()); // once data has been sent to the GPU, it can be cleared from system RAM mesh_data->vertices.clear(); @@ -330,22 +330,22 @@ void quad(MeshData* mesh_data, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec if (backFace) { - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 2); - mesh_data->indices.push_back(mesh_data->chunk->numVertices); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 1); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 1); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 3); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 2); + mesh_data->indices.push_back(mesh_data->numVertices + 2); + mesh_data->indices.push_back(mesh_data->numVertices); + mesh_data->indices.push_back(mesh_data->numVertices + 1); + mesh_data->indices.push_back(mesh_data->numVertices + 1); + mesh_data->indices.push_back(mesh_data->numVertices + 3); + mesh_data->indices.push_back(mesh_data->numVertices + 2); } else { - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 2); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 3); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 1); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 1); - mesh_data->indices.push_back(mesh_data->chunk->numVertices); - mesh_data->indices.push_back(mesh_data->chunk->numVertices + 2); + mesh_data->indices.push_back(mesh_data->numVertices + 2); + mesh_data->indices.push_back(mesh_data->numVertices + 3); + mesh_data->indices.push_back(mesh_data->numVertices + 1); + mesh_data->indices.push_back(mesh_data->numVertices + 1); + mesh_data->indices.push_back(mesh_data->numVertices); + mesh_data->indices.push_back(mesh_data->numVertices + 2); } - mesh_data->chunk->numVertices += 4; + mesh_data->numVertices += 4; } }; diff --git a/src/renderer.cpp b/src/renderer.cpp index 84fb7a2..290bbe3 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -62,8 +62,6 @@ namespace renderer{ } for(auto& c : chunks_torender){ - if(! (c->getState(Chunk::CHUNK_STATE_MESHED))) continue; - float dist = glm::distance(c->getPosition(), cameraChunkPos); if(dist <= static_cast(RENDER_DISTANCE)){ if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue; @@ -95,7 +93,7 @@ namespace renderer{ if (!out) { - if(c->numVertices > 0) + if(c->numTriangles > 0) { // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode theShader->setMat4("model", model); @@ -103,7 +101,7 @@ namespace renderer{ theShader->setMat4("projection", theCamera.getProjection()); glBindVertexArray(c->VAO); - glDrawElements(GL_TRIANGLES, c->numVertices , GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, c->numTriangles , GL_UNSIGNED_INT, 0); glBindVertexArray(0); } }