diff --git a/include/chunkmeshdata.hpp b/include/chunkmeshdata.hpp new file mode 100644 index 0000000..200abc1 --- /dev/null +++ b/include/chunkmeshdata.hpp @@ -0,0 +1,33 @@ +#ifndef CHUNK_MESH_DATA_H +#define CHUNK_MESH_DATA_H + +enum class ChunkMeshDataType{ + MESH_UPDATE +}; + +typedef struct ChunkMeshData{ + int32_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/chunkmesher.cpp b/src/chunkmesher.cpp index d87a6e2..4cb58e6 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -13,13 +13,19 @@ 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 +44,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 +196,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 +228,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/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);