From 950c43b1639277f3c123e6b4934317601a6c2e8d Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 19 Jul 2023 12:56:55 +0200 Subject: [PATCH] refactor chunk meshing routine --- include/chunk.hpp | 2 +- include/chunkmesher.hpp | 5 ++-- src/chunk.cpp | 13 ++++----- src/chunkmanager.cpp | 2 +- src/chunkmesher.cpp | 61 +++++++++++++++-------------------------- src/main.cpp | 3 +- src/renderer.cpp | 4 +-- 7 files changed, 35 insertions(+), 55 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index ae547df..17f29fb 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}, normalsBuffer{0}, numTriangles{0}; + GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0}; std::atomic unload_timer{0}; private: diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index 97a834c..ff8a79a 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -18,9 +18,8 @@ namespace chunkmesher{ GLuint numVertices{0}; std::vector vertices; - std::vector normals; - std::vector colors; - std::vector indices; + std::vector extents; + std::vector texinfo; }; oneapi::tbb::concurrent_queue& getMeshDataQueue(); diff --git a/src/chunk.cpp b/src/chunk.cpp index fd868ed..b14fffc 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -28,20 +28,17 @@ namespace Chunk void Chunk::createBuffers(){ glGenVertexArrays(1, &(this->VAO)); - glGenBuffers(1, &(this->colorBuffer)); - glGenBuffers(1, &(this->normalsBuffer)); glGenBuffers(1, &(this->VBO)); - glGenBuffers(1, &(this->EBO)); + glGenBuffers(1, &(this->extentsBuffer)); + glGenBuffers(1, &(this->texinfoBuffer)); } void Chunk::deleteBuffers(){ - glDeleteBuffers(1, &(this->colorBuffer)); - glDeleteBuffers(1, &(this->normalsBuffer)); - glDeleteBuffers(1, &(this->VBO)); - glDeleteBuffers(1, &(this->EBO)); + glDeleteBuffers(1, &(this->VBO)); + glDeleteBuffers(1, &(this->extentsBuffer)); + glDeleteBuffers(1, &(this->texinfoBuffer)); glDeleteVertexArrays(1, &(this->VAO)); - } Block Chunk::getBlock(int x, int y, int z) diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 077f52a..05ed813 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -115,8 +115,8 @@ namespace chunkmanager if(x > 1023 || y > 1023 || z > 1023) continue; const uint32_t index = calculateIndex(x, y, z); - delete n; chunks.erase(index); + //delete n; nUnloaded++; } } diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 07d229b..4ddb2bb 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -41,9 +41,8 @@ void mesh(Chunk::Chunk* chunk) mesh_data->numVertices = 0; mesh_data->chunk = chunk; mesh_data->vertices.clear(); - mesh_data->normals.clear(); - mesh_data->indices.clear(); - mesh_data->colors.clear(); + mesh_data->extents.clear(); + mesh_data->texinfo.clear(); // Abort if chunk is empty if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){ @@ -180,13 +179,19 @@ void mesh(Chunk::Chunk* chunk) dv[2] = 0; dv[v] = h; - quad(mesh_data, glm::vec3(x[0], x[1], x[2]), - glm::vec3(x[0] + du[0], x[1] + du[1], x[2] + du[2]), - glm::vec3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], - x[2] + du[2] + dv[2]), - glm::vec3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]), - glm::vec3(backFace ? q[0] : -q[0], backFace ? q[1] : -q[1], backFace ? q[2] : -q[2] ), - mask[n], dim, backFace); + // bottom left + mesh_data->vertices.push_back(x[0]); //bottomLeft.x + mesh_data->vertices.push_back(x[1]); //bottomLeft.y + mesh_data->vertices.push_back(x[2]); //bottomLeft.z + + // extents, use normals for now + mesh_data->extents.push_back(du[0] + dv[0]); + mesh_data->extents.push_back(du[1] + dv[1]); + mesh_data->extents.push_back(du[2] + dv[2]); + + mesh_data->texinfo.push_back(backFace ? 0.0 : 1.0); + mesh_data->texinfo.push_back((int)(mask[n]) - 2); + mesh_data->numVertices++; } for (l = 0; l < h; ++l) @@ -229,9 +234,6 @@ void sendtogpu(MeshData* mesh_data) // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). glBindVertexArray(mesh_data->chunk->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_data->chunk->EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh_data->indices.size() * sizeof(GLuint), &(mesh_data->indices[0]), GL_STATIC_DRAW); - // position attribute glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->VBO); glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW); @@ -239,48 +241,29 @@ void sendtogpu(MeshData* mesh_data) glEnableVertexAttribArray(0); // normal attribute - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->normalsBuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_data->normals.size() * sizeof(GLfloat), &(mesh_data->normals[0]), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->extentsBuffer); + glBufferData(GL_ARRAY_BUFFER, mesh_data->extents.size() * sizeof(GLfloat), &(mesh_data->extents[0]), GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0)); glEnableVertexAttribArray(1); // texcoords attribute - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->colorBuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_data->colors.size() * sizeof(GLfloat), &(mesh_data->colors[0]), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->texinfoBuffer); + glBufferData(GL_ARRAY_BUFFER, mesh_data->texinfo.size() * sizeof(GLfloat), &(mesh_data->texinfo[0]), GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0); glBindVertexArray(0); // save the number of indices of the mesh, it is needed later for drawing - mesh_data->chunk->numTriangles = (GLuint)(mesh_data->numVertices); + mesh_data->chunk->numVertices = (GLuint)(mesh_data->numVertices); // once data has been sent to the GPU, it can be cleared from system RAM mesh_data->vertices.clear(); - mesh_data->normals.clear(); - mesh_data->indices.clear(); - mesh_data->colors.clear(); + mesh_data->extents.clear(); + mesh_data->texinfo.clear(); } // mark the chunk mesh has loaded on GPU mesh_data->chunk->setState(Chunk::CHUNK_STATE_MESH_LOADED, true); } - -void quad(MeshData* mesh_data, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, - glm::vec3 bottomRight, glm::vec3 normal, Block block, int dim, bool backFace) -{ - // bottom left - mesh_data->vertices.push_back(bottomLeft.x); - mesh_data->vertices.push_back(bottomLeft.y); - mesh_data->vertices.push_back(bottomLeft.z); - - // extents, use normals for now - mesh_data->normals.push_back(abs(topRight.x - bottomLeft.x)); - mesh_data->normals.push_back(abs(topRight.y - bottomLeft.y)); - mesh_data->normals.push_back(abs(topRight.z - bottomLeft.z)); - - mesh_data->colors.push_back(backFace ? 0.0 : 1.0); - mesh_data->colors.push_back(((int)block) - 2); - mesh_data->numVertices++; -} }; diff --git a/src/main.cpp b/src/main.cpp index fdb4efa..8769986 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,7 +58,8 @@ int main() glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetCursorPosCallback(window, mouse_callback); glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); //GL_BACK GL_CCW by default + //glEnable(GL_FRAMEBUFFER_SRGB); //gamma correction done in fragment shader + //glEnable(GL_CULL_FACE); //GL_BACK GL_CCW by default std::cout << "Using GPU: " << glGetString(GL_VENDOR) << " " << glGetString(GL_RENDERER) << "\n"; diff --git a/src/renderer.cpp b/src/renderer.cpp index 84a7006..6df7c4a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -96,14 +96,14 @@ namespace renderer{ if (!out) { - if(c->numTriangles > 0) + if(c->numVertices > 0) { theShader->setMat4("model", model); theShader->setMat4("view", theCamera.getView()); theShader->setMat4("projection", theCamera.getProjection()); glBindVertexArray(c->VAO); - glDrawArrays(GL_POINTS, 0, c->numTriangles); + glDrawArrays(GL_POINTS, 0, c->numVertices); glBindVertexArray(0); } }