I am trying to do the surface based convex volume clipping(Source: Klaus book). Convex clipping approach are using three render passes.
First render pass: front-facing parts of the clipping object are rendered into the first depth image. The front faces are selectively drawn by using back-face culling (with glCullFace(GL BACK)).
Second render pass: the second render pass uses the back-facing geometry to fill the second depth image (culling with glCullFace(GL FRONT)).
Third render pass: the actual volume rendering via texture slicing—a fragment program accesses both depth textures and compares the depth of a fragment with the depth structure.
I am confusing whether I am doing this approach in my code correctly. In my main. First I have created frame buffer, and two depth textures for pass 1 and pass 2. Then the following code how I am doing two passes in the main.cpp file:
///////////////////////////////////////////////////
// Begin pass 1: render proxy geometry back faces to texture.
///////////////////////////////////////////////////
//Set pass uniform variable.
glUniform1i(UniformLoc::Pass, 1);
glBindFramebuffer(GL_FRAMEBUFFER, fbo); // Render to FBO.
glBindVertexArray(cubeVAOID); //Bind cube
glEnable(GL_DEPTH_TEST); //enable depth test
glDrawBuffer(GL_COLOR_ATTACHMENT0); //Out variable in frag shader will be written to the texture attached to GL_COLOR_ATTACHMENT0.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the back buffer to preset values ; buffers currently enabled for color writing.
glCullFace(GL_FRONT); //Culls only the front faces.
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);//Draw the cube
//Bind Front texture so we can read the texture in the shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo_texture_DepthFront);
int fbo_texture_DepthFront_loc = glGetUniformLocation(shader_program, "SamplerDepthFront");
if (fbo_texture_DepthFront_loc != -1)
{
glUniform1i(fbo_texture_DepthFront_loc, 0); // binding the depth texture to texture unit 0
}
///////////////////////////////////////////////////
// Begin pass 2: render proxy geometry back faces to screen
///////////////////////////////////////////////////
glUniform1i(UniformLoc::Pass, 2);
glBindVertexArray(cubeVAOID); //Bind cube
glBindFramebuffer(GL_FRAMEBUFFER, 0); // back to default
glClear(GL_COLOR_BUFFER_BIT); //Clear the FBO attached texture.
glDrawBuffer(GL_BACK); // specify which color buffers are to be drawn into; Render to back color buffer.
glDisable(GL_DEPTH_TEST);
//Bind Back texture so we can read the texture in the shader
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fbo_texture_DepthBack);
int fbo_texture_DepthBack_loc = glGetUniformLocation(shader_program, "SamplerDepthBack");
if (fbo_texture_DepthBack_loc != -1)
{
glUniform1i(fbo_texture_DepthBack_loc, 1); // binding the depth texture to texture unit 1
}
glCullFace(GL_BACK); //Culls only the back faces.
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
I am confusing what should I do in the fragment shader.
For the pass I: update the depth value for Zfront and save the updated value in depth buffer.
For pass 2: update the depth value for Zback and save the updated value in depth buffer.
Pass 3: I am doing volume probing If a vertex position outside the clip plane discard.
If the above Pass I and Pass 2 process are correct, how can I update the depth value in the fragment shader?