move chunkmeshdata into its own file

And let it be managed by chunkmesher instead of chunkmanager
pull/13/head
EmaMaker 2023-10-04 12:10:33 +02:00
parent 88abf21502
commit d0ddf2256f
7 changed files with 64 additions and 63 deletions

36
include/chunkmeshdata.hpp Normal file
View File

@ -0,0 +1,36 @@
#ifndef CHUNK_MESH_DATA_H
#define CHUNK_MESH_DATA_H
#include <oneapi/tbb/concurrent_queue.h>
#include "chunk.hpp"
enum class ChunkMeshDataType{
MESH_UPDATE
};
typedef struct ChunkMeshData{
chunk_index_t index;
glm::vec3 position;
int num_vertices = 0;
std::vector<GLfloat> vertices;
std::vector<GLfloat> extents;
std::vector<GLfloat> 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<ChunkMeshData*> ChunkMeshDataQueue;
#endif

View File

@ -5,10 +5,12 @@
#include <vector>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <GLFW/glfw3.h>
#include <oneapi/tbb/concurrent_queue.h>
#include "chunk.hpp"
#include "chunkmeshdata.hpp"
#include "globals.hpp"
#include "shader.hpp"
@ -21,13 +23,12 @@ namespace chunkmesher{
std::vector<GLfloat> extents;
std::vector<GLfloat> texinfo;
};
oneapi::tbb::concurrent_queue<MeshData*>& 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

View File

@ -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<chunkmesher::MeshData*>& getMeshDataQueue();
ChunkMeshDataQueue& getMeshDataQueue();
};
#endif

View File

@ -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);

View File

@ -11,15 +11,23 @@
#include "spacefilling.hpp"
#include "utils.hpp"
#define CHUNK_MESH_DATA_QUANTITY 100
namespace chunkmesher{
oneapi::tbb::concurrent_queue<MeshData*> MeshDataQueue;
ChunkMeshDataQueue MeshDataQueue;
oneapi::tbb::concurrent_queue<MeshData*>& 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);
}
};

View File

@ -69,6 +69,7 @@ int main()
SpaceFilling::initLUT();
controls::init();
chunkmanager::init();
chunkmesher::init();
debug::window::init(window);
renderer::init(window);

View File

@ -14,14 +14,14 @@
namespace renderer{
RenderSet chunks_torender;
oneapi::tbb::concurrent_vector<Chunk::Chunk*> render_todelete;
oneapi::tbb::concurrent_queue<chunkmesher::MeshData*> MeshDataQueue;
ChunkMeshDataQueue MeshDataQueue;
Shader* theShader, *quadShader;
GLuint chunkTexture;
Shader* getRenderShader() { return theShader; }
RenderSet& getChunksToRender(){ return chunks_torender; }
oneapi::tbb::concurrent_queue<chunkmesher::MeshData*>& 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);