chunkmesher/manager: move sending data to gpu in dedicated function

vertex-deduplication
EmaMaker 2023-03-23 16:19:25 +01:00
parent 19b3c68015
commit 19cd81b9fb
3 changed files with 45 additions and 45 deletions

View File

@ -13,6 +13,7 @@
namespace chunkmesher{ namespace chunkmesher{
void mesh(Chunk::Chunk* chunk); void mesh(Chunk::Chunk* chunk);
void sendtogpu(Chunk::Chunk* chunk);
void draw(Chunk::Chunk* chunk, glm::mat4 model); 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); void quad(Chunk::Chunk* chunk, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace);

View File

@ -251,42 +251,7 @@ namespace chunkmanager
} }
else else
{ {
if (!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) if (!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) chunkmesher::sendtogpu(c);
{
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);
}
// Frustum Culling of chunk // Frustum Culling of chunk
total++; total++;

View File

@ -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) void draw(Chunk::Chunk* chunk, glm::mat4 model)
{ {