0
votes

I have created three depth buffers. First buffer, I have drawn using GL_LESS. How can I pass this information to second buffer and third buffer and draw the second buffer and third buffer such that secondbuffer.z <= thirdBuffer.z?

Below is the code I have written. I am newbie to OpenGL. Can someone please suggest other efficient algorithm if any

I have initialised three framebuffers, one for each depth buffers. First buffer i have drawn using GL_LESS. How can I pass this drawn buffer to subsequent buffers?

GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);

GLuint depthrenderbuffer1;
glGenRenderbuffers(1, &depthrenderbuffer1);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer1);
glClear(GL_DEPTH_BUFFER_BIT);
glClearDepth(1.);
glDepthFunc(GL_LESS);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer1);
GLuint DepthTex1Location = glGetUniformLocation(programID_normalmap, "DepthTex1");
glUniform1i(DepthTex1Location, 0);
GLenum DrawBuffers[1] = {GL_DEPTH_ATTACHMENT};
glDrawBuffers(1, DrawBuffers);


GLuint FramebufferName2 = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);

GLuint depthrenderbuffer2;
glGenRenderbuffers(1, &depthrenderbuffer2);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer2);
glClear(GL_DEPTH_BUFFER_BIT);
glClearDepth(1.);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer2);
GLuint DepthTex2Location = glGetUniformLocation(programID_normalmap, "DepthTex2");
glUniform1i(DepthTex2Location, 1);
//GLenum DrawBuffers2[1] = {GL_DEPTH_ATTACHMENT};
//glDrawBuffers(1, DrawBuffers2);

GLuint FramebufferName3 = 0;
glGenFramebuffers(1, &FramebufferName3);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName3);

GLuint depthrenderbuffer3;
glGenRenderbuffers(1, &depthrenderbuffer3);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer3);
glClear(GL_DEPTH_BUFFER_BIT);
glClearDepth(1.);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer3);
 GLuint DepthTex3Location = 
 glGetUniformLocation(programID_normalmap, "DepthTex3");
 glUniform1i(DepthTex3Location, 2);
    //GLenum DrawBuffers3[1] = {GL_DEPTH_ATTACHMENT};
    //glDrawBuffers(1, DrawBuffers3);

Below is the fragment Shader:

version 330

uniform samplerRect DepthTex1;
uniform samplerRect DepthTex2;
uniform samplerRect DepthTex3;

out vec4 outputColor;
in vec3 fragmentColor;


void main(void)
{
    // Bit-exact comparison between FP32 z-buffer and fragment depth
    float frontDepth1 = texture(DepthTex1, gl_FragCoord.xy).r;
    float frontDepth2 = texture(DepthTex2, gl_FragCoord.xy).r;
    float frontDepth3 = texture(DepthTex3, gl_FragCoord.xy).r;
    if (frontDepth2 <= frontDepth1) {
            discard;
    }
    if (frontDepth3 <= frontDepth2) {
            discard;
    }
    

    // Shade all the fragments behind the z-buffer
    vec4 outputColor = fragmentColor;
}

Thanks for any help.

1
Could you explain what you are trying to do and what you mean by passing information to another buffer? - Nico Schertler
I am trying to calculate the depths of polygons (in ascending order) that are mapped onto the pixel. To be precise, multi layered depth map - user3048926
Multi-layered depth maps are usually achieved by setting an appropriate znear/zfar range in the projection matrix and rendering multiple times. Are you trying to render the layers all at the same time? - Nico Schertler
Yes, I am trying to render all the layers at the same time except for the very first layer which is essentially output of z-buffer algorithm - user3048926
Something like the following could work: Since you can only bind one depth attachment at a time, your different depth buffers need to be color targets. Set the blend equation to GL_MIN. Then, in the fragment shader, set the output "color" for each buffer to infinity, except for the buffer, into whose range the fragment falls. For this buffer, set the actual depth. You will need a separate render pass for the actual colors. You might also want to take a look at other ways to achieve oder-independent transparency - Nico Schertler

1 Answers

0
votes

Everything here seems a little bit strange. You are creating 3 renderbuffers for the depth outputs from a framebuffer, which by their very nature means they will be destroyed once the framebuffer rendering is complete. Attempting to use a renderbuffer as an input to another rendering stage simply can't work - the renderbuffer is a temp object. I think what you are wanting to do, is create 3 textures for each of the depth buffers, and instead bind it to the framebuffer with glFramebufferTexture2D (instead of glFramebufferRenderbuffer).

This will leave you with 3 texture objects, which you can bind to DepthTex2Location et al.