diff --git a/include/renderer.hpp b/include/renderer.hpp index 2b560c9..c0cdcba 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -39,6 +39,7 @@ namespace renderer{ } } RenderInfo; + typedef oneapi::tbb::concurrent_queue IndexQueue; void init(GLFWwindow* window); void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info); @@ -51,6 +52,8 @@ namespace renderer{ Shader* getRenderShader(); ChunkMeshDataQueue& getMeshDataQueue(); + IndexQueue& getDeleteIndexQueue(); + }; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index b54c3d4..1addbce 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -112,6 +112,7 @@ namespace chunkmanager // Using the key doesn't work if(chunks.erase(a)){ nUnloaded++; + renderer::getDeleteIndexQueue().push(index); delete c; } else { c->setState(Chunk::CHUNK_STATE_IN_DELETING_QUEUE, false); diff --git a/src/renderer.cpp b/src/renderer.cpp index ed20533..5daa398 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -17,12 +17,14 @@ namespace renderer{ RenderTable ChunksToRender; ChunkMeshDataQueue MeshDataQueue; + IndexQueue MeshDataToDelete; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + IndexQueue& getDeleteIndexQueue(){ return MeshDataToDelete; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; @@ -139,6 +141,7 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); + /* Process incoming mesh data */ ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ RenderTable::accessor a; @@ -161,7 +164,18 @@ namespace renderer{ chunkmesher::getMeshDataQueue().push(m); } - // TODO: implement removal of chunks from rendering + /* Process chunks to be removed */ + chunk_index_t queue_index; + while(MeshDataToDelete.try_pop(queue_index)){ + RenderTable::accessor a; + + if(ChunksToRender.find(a, queue_index)){ + RenderInfo* render_info = a->second; + render_info->deallocateBuffers(); + delete render_info; + ChunksToRender.erase(a); + } + } /* Render the chunks */ // parallel_for cannot be used since all the rendering needs to happen in a single thread