Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Rendering

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

like image 763
Tiblemont Avatar asked Oct 18 '25 17:10

Tiblemont


1 Answers

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
}
like image 80
Tiblemont Avatar answered Oct 20 '25 07:10

Tiblemont