From 19cd81b9fb9083536b6174d06f7cd4327ec21555 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Thu, 23 Mar 2023 16:19:25 +0100 Subject: [PATCH] chunkmesher/manager: move sending data to gpu in dedicated function --- include/chunkmesher.hpp | 3 ++- src/chunkmanager.cpp | 53 +++++++---------------------------------- src/chunkmesher.cpp | 34 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index 5e63dd5..f6ca427 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -13,6 +13,7 @@ namespace chunkmesher{ void mesh(Chunk::Chunk* chunk); +void sendtogpu(Chunk::Chunk* chunk); void draw(Chunk::Chunk* chunk, glm::mat4 model); void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace); @@ -20,4 +21,4 @@ void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec } -#endif \ No newline at end of file +#endif diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 30bf61f..00503f0 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -251,48 +251,13 @@ namespace chunkmanager } else { - if (!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) - { - if (c->vIndex > 0) - { - - // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). - glBindVertexArray(c->VAO); - - glBindBuffer(GL_ARRAY_BUFFER, c->VBO); - glBufferData(GL_ARRAY_BUFFER, c->vertices.size() * sizeof(GLfloat), &(c->vertices[0]), GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, c->EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, c->indices.size() * sizeof(GLuint), &(c->indices[0]), GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, c->colorBuffer); - glBufferData(GL_ARRAY_BUFFER, c->colors.size() * sizeof(GLfloat), &(c->colors[0]), GL_STATIC_DRAW); - - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); - - // glDisableVertexAttribArray(0); - // glDisableVertexAttribArray(1); - glBindVertexArray(0); - - c->vIndex = (GLuint)(c->indices.size()); - - c->vertices.clear(); - c->indices.clear(); - c->colors.clear(); - } - c->setState(Chunk::CHUNK_STATE_MESH_LOADED, true); - } - + if (!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) chunkmesher::sendtogpu(c); // Frustum Culling of chunk - total++; + total++; - glm::vec3 chunk = c->getPosition(); + glm::vec3 chunk = c->getPosition(); glm::vec4 chunkW = glm::vec4(chunk.x*static_cast(CHUNK_SIZE), chunk.y*static_cast(CHUNK_SIZE), chunk.z*static_cast(CHUNK_SIZE),1.0); - glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk); + glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk); bool out=false; // First test, check if all the corners of the chunk are outside any of the @@ -313,11 +278,11 @@ namespace chunkmanager } } - if (!out) - { - toGpu++; - chunkmesher::draw(c, model); - } + if (!out) + { + toGpu++; + chunkmesher::draw(c, model); + } } } c->mutex_state.unlock(); diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 89383fc..2b62a96 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -184,6 +184,40 @@ void mesh(Chunk::Chunk* chunk) } } +void sendtogpu(Chunk::Chunk* chunk) +{ + if (chunk->vIndex > 0) + { + + // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). + glBindVertexArray(chunk->VAO); + + glBindBuffer(GL_ARRAY_BUFFER, chunk->VBO); + glBufferData(GL_ARRAY_BUFFER, chunk->vertices.size() * sizeof(GLfloat), &(chunk->vertices[0]), GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, chunk->EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, chunk->indices.size() * sizeof(GLuint), &(chunk->indices[0]), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, chunk->colorBuffer); + glBufferData(GL_ARRAY_BUFFER, chunk->colors.size() * sizeof(GLfloat), &(chunk->colors[0]), GL_STATIC_DRAW); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + + glBindVertexArray(0); + + chunk->vIndex = (GLuint)(chunk->indices.size()); + + chunk->vertices.clear(); + chunk->indices.clear(); + chunk->colors.clear(); + } + chunk->setState(Chunk::CHUNK_STATE_MESH_LOADED, true); +} + void draw(Chunk::Chunk* chunk, glm::mat4 model) {