Search code examples
opengltexture2dfbo

OpenGL FBO depth format and type confusion


I'm a bit confused about the internal format, format and type. So what's about the depth attachment point?

If I'm using a RenderBuffer, I think this is the valid code if I don't want to use stencil:

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32F, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRB);

However if I want to be able to read the depth values, I have to attach a texture to the depth attachment point. So I have to call a glTexImage2D function with parameters "internal format", "format" and "type".

In this case which internal format, format and type should I choose? Can I use the following combinations for a depth attachment? (in the order of: internal format, format and type)

  • GL_R32F, GL_RED, GL_FLOAT
  • GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT
  • GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT

Is the GL_UNSIGNED_INT type valid for the 2nd case? What does that really mean? Will it allocate 4 bytes per fragment? In some tutorials they are using GL_UNSIGNED_BYTE for the type parameter. Which is the correct one?

Thanks

Edit:

Clarified my question about which parameters I'm interested in.


Solution

  • Depth values are not color values. As such, if you want to store depth values in a texture, the texture must use an internal format that contains depth information.

    The pixel transfer format/type parameters, even if you're not actually passing data, must still be reasonable with respect to the internal format. Since the internal format contains depth information, your pixel transfer format must specify depth information: GL_DEPTH_COMPONENT.

    As for the pixel transfer type, you should read 32F back as GL_FLOAT.