Read depth from FBO and renderbuffer

Hi,

I am writing depth component to framebuffer which a render buffer attaches to. Then I am using glReadPixels to retrieve the depth from buffers. But gl gave me error ‘null’ from the call of glReadPixels

Here is the set up code snippet:


glGenFramebuffersEXT(1, &depth_fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depth_fb);
glDrawBuffer(GL_NONE);	

// create a renderbuffer object to store depth info
  glGenRenderbuffersEXT(1, &depth_rb);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,
    depth_size, depth_size);  

// attach the renderbuffer to depth attachment point
  glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
    GL_RENDERBUFFER_EXT, depth_rb);

  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

  // check FBO status
  CheckFramebufferStatus();

The CheckFramebufferStatus() shows the framebuffer setup is o.k.

The following is the code snippet in the rendering loop to read depth from the buffer:


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depth_fb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,
    depth_size, depth_size);

// attach the renderbuffer to depth attachment point
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
    GL_RENDERBUFFER_EXT, depth_rb);

glClear(GL_DEPTH_BUFFER_BIT);
DrawGeometry();

// read from frame buffer
int dataSize = depth_size* depth_size;
float* data = new float[dataSize];    
glReadPixels(0, 0, depth_size, depth_size, GL_DEPTH_COMPONENT24, GL_FLOAT, data);


I searched for the answers but got no luck. Where am I doing wrong here?

Thanks.

For glReadPixels, the format should be GL_DEPTH_COMPONENT instead of GL_DEPTH_COMPONENT_24. It’s a bit confusing at first time, but the symbolic constants for formats in client memory (so the readback format) and the internal formats used by GL for buffers/textures are different.

I tried the format change from GL_DEPTH_COMPONENT24 to GL_DEPTH_COMPONENT, but it did not solve the problem. I still got the same error.

If you call CheckFramebufferStatus on your FBO, instead of zero (the system drawable) it should tell you that your FBO is incomplete.

That’s because the default glReadBuffer state is COLOR0.
You should set glReadBuffer in addition to glDrawBuffer.

See example (7) “Render to depth texture with no color attachments” in the EXT_fbo spec.

I added glReadBuffer(GL_NONE) after glDrawBuffer(GL_NONE), plus, I changed from GL_DEPTH_COMPONENT24 to GL_DEPTH_COMPONENT in glReadPixels call. Now I seem to get some results for my data. But all the values are 1. That does not seem right… I will keep looking into it…

If you find that you can read depth on AMD on non-default framebuffer, I would be interested to hear about that. I have recently had issues with that (works fine on NVidia).