I have many meshes to render, some quite big. So I want to use Conditional Rendering.
here is an excerpt of the code
glColorMaski(0, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // not written in the framebuffer
glBeginQuery(GL_SAMPLES_PASSED, QueryName); // Beginning of the samples count query
// render simple bounding box
glEndQuery(GL_SAMPLES_PASSED);
// render only if a sample pass the occlusion query.
glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // written in the framebuffer
glBindBufferRange(..);
glBeginConditionalRender(QueryName, GL_QUERY_WAIT);
glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f)[0]); // ****** all other meshes will be erased *******
// render complex and heavy mesh
glEndConditionalRender();
It works but the problem is that Clearing the frame buffer before the second rendering will erase any previous meshes.
So how can I use a different depth buffer or any buffer binding to use Occlusion Query on ALL the meshes?
I am using OpenGL core 4.5
Following Botje's advice, I use a "depth pre-pass" to determine which meshes are to be fully rendered. Each mesh has a boolean (bOcclusion) to keep track of the depth test result It works just fine. some peudo-code
// Main rendering loop
// setup matrix and options...........
// ** not writing in the framebuffer
glColorMaski(0, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
foreach(mesh,vectorMesh){ // first pass
mesh->render(glm::dmat4,uint optionOcclusion);
}
glClearDepth(1.0f);
// ** writing in the framebuffer
glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
foreach(mesh,vectorMesh){ // second pass
mesh->render(glm::dmat4,uint optionRender);
}
// ************** mesh rendering *************
// mesh has a boolean (bOcclusion) showing depth test result
void mesh::render(glm::dmat4,uint options)
{
if(options & RENDER_MODE_OCCLUSION){
glClearDepth(1.0f);
glBeginQuery(GL_ANY_SAMPLES_PASSED, queryOcclusion);
// ....setup VAO and buffers for bounding box
// render bounding box
glEndQuery(GL_ANY_SAMPLES_PASSED);
glFinish(); // make sure GL_QUERY_RESULT is available
uint nb;
glGetQueryObjectuiv(queryOcclusion,GL_QUERY_RESULT,&nb);
bOcclusion = (nb>0);
return;
}
else if(!bOcclusion)
return;
// normal complete rendering
// ....setup VAO and buffers for full mesh
// render full mesh
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With