refactor chunk meshing routine

pull/5/head
EmaMaker 2023-07-19 12:56:55 +02:00
parent 54c7fa172f
commit 950c43b163
7 changed files with 35 additions and 55 deletions

View File

@ -56,7 +56,7 @@ namespace Chunk
std::unique_ptr<Block[]> getBlocksArray(int* len) { return (this->blocks.toArray(len)); }
public:
GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, normalsBuffer{0}, numTriangles{0};
GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0};
std::atomic<float> unload_timer{0};
private:

View File

@ -18,9 +18,8 @@ namespace chunkmesher{
GLuint numVertices{0};
std::vector<GLfloat> vertices;
std::vector<GLfloat> normals;
std::vector<GLfloat> colors;
std::vector<GLuint> indices;
std::vector<GLfloat> extents;
std::vector<GLfloat> texinfo;
};
oneapi::tbb::concurrent_queue<MeshData*>& getMeshDataQueue();

View File

@ -28,20 +28,17 @@ namespace Chunk
void Chunk::createBuffers(){
glGenVertexArrays(1, &(this->VAO));
glGenBuffers(1, &(this->colorBuffer));
glGenBuffers(1, &(this->normalsBuffer));
glGenBuffers(1, &(this->VBO));
glGenBuffers(1, &(this->EBO));
glGenBuffers(1, &(this->extentsBuffer));
glGenBuffers(1, &(this->texinfoBuffer));
}
void Chunk::deleteBuffers(){
glDeleteBuffers(1, &(this->colorBuffer));
glDeleteBuffers(1, &(this->normalsBuffer));
glDeleteBuffers(1, &(this->VBO));
glDeleteBuffers(1, &(this->EBO));
glDeleteBuffers(1, &(this->VBO));
glDeleteBuffers(1, &(this->extentsBuffer));
glDeleteBuffers(1, &(this->texinfoBuffer));
glDeleteVertexArrays(1, &(this->VAO));
}
Block Chunk::getBlock(int x, int y, int z)

View File

@ -115,8 +115,8 @@ namespace chunkmanager
if(x > 1023 || y > 1023 || z > 1023) continue;
const uint32_t index = calculateIndex(x, y, z);
delete n;
chunks.erase(index);
//delete n;
nUnloaded++;
}
}

View File

@ -41,9 +41,8 @@ void mesh(Chunk::Chunk* chunk)
mesh_data->numVertices = 0;
mesh_data->chunk = chunk;
mesh_data->vertices.clear();
mesh_data->normals.clear();
mesh_data->indices.clear();
mesh_data->colors.clear();
mesh_data->extents.clear();
mesh_data->texinfo.clear();
// Abort if chunk is empty
if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){
@ -180,13 +179,19 @@ void mesh(Chunk::Chunk* chunk)
dv[2] = 0;
dv[v] = h;
quad(mesh_data, glm::vec3(x[0], x[1], x[2]),
glm::vec3(x[0] + du[0], x[1] + du[1], x[2] + du[2]),
glm::vec3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1],
x[2] + du[2] + dv[2]),
glm::vec3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]),
glm::vec3(backFace ? q[0] : -q[0], backFace ? q[1] : -q[1], backFace ? q[2] : -q[2] ),
mask[n], dim, backFace);
// bottom left
mesh_data->vertices.push_back(x[0]); //bottomLeft.x
mesh_data->vertices.push_back(x[1]); //bottomLeft.y
mesh_data->vertices.push_back(x[2]); //bottomLeft.z
// extents, use normals for now
mesh_data->extents.push_back(du[0] + dv[0]);
mesh_data->extents.push_back(du[1] + dv[1]);
mesh_data->extents.push_back(du[2] + dv[2]);
mesh_data->texinfo.push_back(backFace ? 0.0 : 1.0);
mesh_data->texinfo.push_back((int)(mask[n]) - 2);
mesh_data->numVertices++;
}
for (l = 0; l < h; ++l)
@ -229,9 +234,6 @@ void sendtogpu(MeshData* mesh_data)
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(mesh_data->chunk->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_data->chunk->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh_data->indices.size() * sizeof(GLuint), &(mesh_data->indices[0]), GL_STATIC_DRAW);
// 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);
@ -239,48 +241,29 @@ void sendtogpu(MeshData* mesh_data)
glEnableVertexAttribArray(0);
// normal attribute
glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->normalsBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh_data->normals.size() * sizeof(GLfloat), &(mesh_data->normals[0]), GL_STATIC_DRAW);
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->colorBuffer);
glBufferData(GL_ARRAY_BUFFER, mesh_data->colors.size() * sizeof(GLfloat), &(mesh_data->colors[0]), GL_STATIC_DRAW);
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->numTriangles = (GLuint)(mesh_data->numVertices);
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->normals.clear();
mesh_data->indices.clear();
mesh_data->colors.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);
}
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)
{
// bottom left
mesh_data->vertices.push_back(bottomLeft.x);
mesh_data->vertices.push_back(bottomLeft.y);
mesh_data->vertices.push_back(bottomLeft.z);
// extents, use normals for now
mesh_data->normals.push_back(abs(topRight.x - bottomLeft.x));
mesh_data->normals.push_back(abs(topRight.y - bottomLeft.y));
mesh_data->normals.push_back(abs(topRight.z - bottomLeft.z));
mesh_data->colors.push_back(backFace ? 0.0 : 1.0);
mesh_data->colors.push_back(((int)block) - 2);
mesh_data->numVertices++;
}
};

View File

@ -58,7 +58,8 @@ int main()
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); //GL_BACK GL_CCW by default
//glEnable(GL_FRAMEBUFFER_SRGB); //gamma correction done in fragment shader
//glEnable(GL_CULL_FACE); //GL_BACK GL_CCW by default
std::cout << "Using GPU: " << glGetString(GL_VENDOR) << " " << glGetString(GL_RENDERER) << "\n";

View File

@ -96,14 +96,14 @@ namespace renderer{
if (!out)
{
if(c->numTriangles > 0)
if(c->numVertices > 0)
{
theShader->setMat4("model", model);
theShader->setMat4("view", theCamera.getView());
theShader->setMat4("projection", theCamera.getProjection());
glBindVertexArray(c->VAO);
glDrawArrays(GL_POINTS, 0, c->numTriangles);
glDrawArrays(GL_POINTS, 0, c->numVertices);
glBindVertexArray(0);
}
}