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,37 +158,38 @@ namespace renderer{
distz >= -RENDER_DISTANCE && distz < RENDER_DISTANCE){ distz >= -RENDER_DISTANCE && distz < RENDER_DISTANCE){
if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue; if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue;
// Increase total vertex count
vertices += c->numVertices;
// reset out-of-vision and unload flags // reset out-of-vision and unload flags
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false); c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false); c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
// Perform frustum culling and eventually render if(c->numVertices > 0)
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);
glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk);
// Check if all the corners of the chunk are outside any of the planes
// TODO (?) implement frustum culling as per (Inigo Quilez)[https://iquilezles.org/articles/frustumcorrect/], and check each
// plane against each corner of the chunk
bool out=false;
int a{0};
for(int p = 0; p < 6; p++){
a = 0;
for(int i = 0; i < 8; i++) a += glm::dot(frustumPlanes[p], glm::vec4(chunkW.x + ((float)(i & 1))*CHUNK_SIZE, chunkW.y
+ ((float)((i & 2) >> 1))*CHUNK_SIZE, chunkW.z + ((float)((i & 4) >> 2))*CHUNK_SIZE, 1.0)) < 0.0;
if(a==8){
out=true;
break;
}
}
if (!out)
{ {
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);
glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk);
// Check if all the corners of the chunk are outside any of the planes
// TODO (?) implement frustum culling as per (Inigo Quilez)[https://iquilezles.org/articles/frustumcorrect/], and check each
// plane against each corner of the chunk
bool out=false;
int a{0};
for(int p = 0; p < 6; p++){
a = 0;
for(int i = 0; i < 8; i++) a += glm::dot(frustumPlanes[p], glm::vec4(chunkW.x + ((float)(i & 1))*CHUNK_SIZE, chunkW.y
+ ((float)((i & 2) >> 1))*CHUNK_SIZE, chunkW.z + ((float)((i & 4) >> 2))*CHUNK_SIZE, 1.0)) < 0.0;
if(a==8){
out=true;
break;
}
}
if (!out)
{ {
theShader->setMat4("model", model); theShader->setMat4("model", model);
theShader->setMat4("view", theCamera.getView()); theShader->setMat4("view", theCamera.getView());
@ -204,7 +205,7 @@ namespace renderer{
}else{ }else{
// When the chunk is outside render distance // When the chunk is outside render distance
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){ /*if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
oof++; oof++;
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){ if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
// If chunk was already out and enough time has passed // 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_OUTOFVISION, true);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false); c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
c->unload_timer = glfwGetTime(); 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_deleted", (int) (render_todelete.size()));
debug::window::set_parameter("render_chunks_vertices", vertices); debug::window::set_parameter("render_chunks_vertices", vertices);
for(auto& c : render_todelete){ /*for(auto& c : render_todelete){
c->deleteBuffers();
// we can get away with unsafe erase as access to the container is only done by this // we can get away with unsafe erase as access to the container is only done by this
// thread // thread
chunks_torender.unsafe_erase(c); chunks_torender.unsafe_erase(c);
c->setState(Chunk::CHUNK_STATE_UNLOADED, true); c->setState(Chunk::CHUNK_STATE_UNLOADED, true);
//c->deleteBuffers();
} }
render_todelete.clear(); render_todelete.clear();*/
/* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */ /* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */
// Now to render the quad, with the texture on top // Now to render the quad, with the texture on top