From d0ddf2256f00d8c6e62df0533db39351b51aedfe Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 12:10:33 +0200 Subject: [PATCH] move chunkmeshdata into its own file And let it be managed by chunkmesher instead of chunkmanager --- include/chunkmeshdata.hpp | 36 +++++++++++++++++++++ include/chunkmesher.hpp | 9 +++--- include/renderer.hpp | 4 ++- src/chunkmanager.cpp | 4 --- src/chunkmesher.cpp | 67 ++++++++++----------------------------- src/main.cpp | 1 + src/renderer.cpp | 6 ++-- 7 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 include/chunkmeshdata.hpp diff --git a/include/chunkmeshdata.hpp b/include/chunkmeshdata.hpp new file mode 100644 index 0000000..74fd74d --- /dev/null +++ b/include/chunkmeshdata.hpp @@ -0,0 +1,36 @@ +#ifndef CHUNK_MESH_DATA_H +#define CHUNK_MESH_DATA_H + +#include +#include "chunk.hpp" + +enum class ChunkMeshDataType{ + MESH_UPDATE +}; + +typedef struct ChunkMeshData{ + chunk_index_t index; + glm::vec3 position; + int num_vertices = 0; + + std::vector vertices; + std::vector extents; + std::vector texinfo; + + ChunkMeshDataType message_type; + + void clear(){ + vertices.clear(); + texinfo.clear(); + extents.clear(); + index = 0; + position = glm::vec3(0); + num_vertices = 0; + } + +}ChunkMeshData; +typedef oneapi::tbb::concurrent_queue ChunkMeshDataQueue; + + +#endif + diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index ff8a79a..cbfe4f9 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -5,10 +5,12 @@ #include #include +#include #include #include #include "chunk.hpp" +#include "chunkmeshdata.hpp" #include "globals.hpp" #include "shader.hpp" @@ -21,13 +23,12 @@ namespace chunkmesher{ std::vector extents; std::vector texinfo; }; - oneapi::tbb::concurrent_queue& getMeshDataQueue(); + ChunkMeshDataQueue& getMeshDataQueue(); + void init(); void mesh(Chunk::Chunk* chunk); - void sendtogpu(MeshData* mesh_data); - 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); } #endif + diff --git a/include/renderer.hpp b/include/renderer.hpp index d772c77..58907f7 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -6,6 +6,7 @@ #include "chunk.hpp" #include "chunkmesher.hpp" +#include "chunkmeshdata.hpp" #include "shader.hpp" namespace renderer{ @@ -21,8 +22,9 @@ namespace renderer{ Shader* getRenderShader(); RenderSet& getChunksToRender(); - oneapi::tbb::concurrent_queue& getMeshDataQueue(); + ChunkMeshDataQueue& getMeshDataQueue(); }; #endif + diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 1eb021d..e2e1505 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -53,10 +53,6 @@ namespace chunkmanager index++; } - // Also init mesh data queue - for(int i = 0; i < 10; i++) - chunkmesher::getMeshDataQueue().push(new chunkmesher::MeshData()); - should_run = true; update_thread = std::thread(update); gen_thread = std::thread(generate); diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index d87a6e2..770aeae 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -11,15 +11,23 @@ #include "spacefilling.hpp" #include "utils.hpp" +#define CHUNK_MESH_DATA_QUANTITY 100 + namespace chunkmesher{ -oneapi::tbb::concurrent_queue MeshDataQueue; +ChunkMeshDataQueue MeshDataQueue; -oneapi::tbb::concurrent_queue& getMeshDataQueue(){ return MeshDataQueue; } +ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + +void init() +{ + for(int i = 0; i < CHUNK_MESH_DATA_QUANTITY; i++) + MeshDataQueue.push(new ChunkMeshData{}); +} void mesh(Chunk::Chunk* chunk) { - MeshData* mesh_data; + ChunkMeshData* mesh_data; if(!MeshDataQueue.try_pop(mesh_data)) return; /* @@ -38,11 +46,10 @@ void mesh(Chunk::Chunk* chunk) */ // Cleanup previous data - mesh_data->numVertices = 0; - mesh_data->chunk = chunk; - mesh_data->vertices.clear(); - mesh_data->extents.clear(); - mesh_data->texinfo.clear(); + mesh_data->clear(); + mesh_data->message_type = ChunkMeshDataType::MESH_UPDATE; + mesh_data->index = chunk->getIndex(); + mesh_data->position = chunk->getPosition(); // Abort if chunk is empty if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){ @@ -191,7 +198,7 @@ void mesh(Chunk::Chunk* chunk) mesh_data->texinfo.push_back(backFace ? 0.0 : 1.0); mesh_data->texinfo.push_back((int)(mask[n]) - 2); - mesh_data->numVertices++; + mesh_data->num_vertices++; } for (l = 0; l < h; ++l) @@ -223,46 +230,4 @@ void mesh(Chunk::Chunk* chunk) chunk->setState(Chunk::CHUNK_STATE_MESHED, true); renderer::getMeshDataQueue().push(mesh_data); } - -void sendtogpu(MeshData* mesh_data) -{ - if (mesh_data->numVertices > 0) - { - if(mesh_data->chunk->VAO == 0) mesh_data->chunk->createBuffers(); - - // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). - glBindVertexArray(mesh_data->chunk->VAO); - - // 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); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); - glEnableVertexAttribArray(0); - - // normal attribute - 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->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->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->extents.clear(); - mesh_data->texinfo.clear(); - } - - // mark the chunk mesh has loaded on GPU - mesh_data->chunk->setState(Chunk::CHUNK_STATE_MESH_LOADED, true); -} }; diff --git a/src/main.cpp b/src/main.cpp index 6aafb51..7bc51f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -69,6 +69,7 @@ int main() SpaceFilling::initLUT(); controls::init(); chunkmanager::init(); + chunkmesher::init(); debug::window::init(window); renderer::init(window); diff --git a/src/renderer.cpp b/src/renderer.cpp index 0dcd3d2..ddd2b45 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -14,14 +14,14 @@ namespace renderer{ RenderSet chunks_torender; oneapi::tbb::concurrent_vector render_todelete; - oneapi::tbb::concurrent_queue MeshDataQueue; + ChunkMeshDataQueue MeshDataQueue; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } RenderSet& getChunksToRender(){ return chunks_torender; } - oneapi::tbb::concurrent_queue& getMeshDataQueue(){ return MeshDataQueue; } + ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; @@ -138,7 +138,7 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); - chunkmesher::MeshData* m; + ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ //chunkmesher::sendtogpu(m); chunkmesher::getMeshDataQueue().push(m);