renderer: try frustum culling only if chunk has vertices

fix-multithread
EmaMaker 2023-09-22 09:06:48 +02:00
parent a65fc44069
commit 00cdd22e10
1 changed files with 36 additions and 32 deletions

View File

@ -158,13 +158,16 @@ namespace renderer{
distz >= -RENDER_DISTANCE && distz < RENDER_DISTANCE){
if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue;
// Increase total vertex count
vertices += c->numVertices;
// reset out-of-vision and unload flags
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
if(c->numVertices > 0)
{
// Increase total vertex count
vertices += c->numVertices;
// Perform frustum culling and eventually render
glm::vec3 chunk = c->getPosition();
glm::vec4 chunkW = glm::vec4(chunk.x*static_cast<float>(CHUNK_SIZE), chunk.y*static_cast<float>(CHUNK_SIZE), chunk.z*static_cast<float>(CHUNK_SIZE),1.0);
@ -187,8 +190,6 @@ namespace renderer{
}
if (!out)
{
if(c->numVertices > 0)
{
theShader->setMat4("model", model);
theShader->setMat4("view", theCamera.getView());
@ -204,7 +205,7 @@ namespace renderer{
}else{
// When the chunk is outside render distance
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
/*if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
oof++;
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
// If chunk was already out and enough time has passed
@ -217,8 +218,11 @@ namespace renderer{
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
c->unload_timer = glfwGetTime();
}
}*/
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
c->setState(Chunk::CHUNK_STATE_UNLOADED, true);
//render_todelete.push_back(c);
oof++;
}
}
@ -230,14 +234,14 @@ namespace renderer{
debug::window::set_parameter("render_chunks_deleted", (int) (render_todelete.size()));
debug::window::set_parameter("render_chunks_vertices", vertices);
for(auto& c : render_todelete){
c->deleteBuffers();
/*for(auto& c : render_todelete){
// we can get away with unsafe erase as access to the container is only done by this
// thread
chunks_torender.unsafe_erase(c);
c->setState(Chunk::CHUNK_STATE_UNLOADED, true);
//c->deleteBuffers();
}
render_todelete.clear();
render_todelete.clear();*/
/* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */
// Now to render the quad, with the texture on top