View Full Version : depth testing with FBO

10-07-2008, 01:45 PM
This is hopefully a very quick one to answer. I thought I read somewhere (of course now I can't find it) that in order to be able to depth test when rendering to texture attached to a framebuffer object, that it's necessary to attach a renderbuffer with internal format set to something like GL_DEPTH_COMPONENT.. this sounds a bit wierd though - tell me this isn't the case!?

I hope I just misread - and that they meant in order to visualise the depth buffer, this is how you would do it..

10-07-2008, 02:22 PM
in order to be able to depth test when rendering to texture attached to a framebuffer object, that it's necessary to attach a renderbuffer with internal format set to something like GL_DEPTH_COMPONENT

I can't find a documentation that confirms that too, but I want to say that it is true. To enable depth testing, you need to allocate a memory area to write depth values (like a renderbuffer) which size is the texture one if you are rendering to texture. Where do you think, opengl would write depth values otherwise?

Note that you can use a 2D texture instead of a renderbuffer for the depth buffer and attach it to a fbo in the same way.

10-07-2008, 02:42 PM
I had hoped that as long as my viewport (in ortho2D) was the same size as the texture, I could get away with using the standard depth buffer.

If I want to use a texture as a depth buffer (fits in better with my framework) - how do I do it? If I'm rendering to a texture attached to COLOR_ATTACHMENT0, where do I attach my "depth texture"? Is there an equivalent to glDrawBuffer for depth?

By the way, I'm trying to set the depth values by writing to glFragDepth - if you know of any issues with this I'd be most grateful for your input.

Ilian Dinev
10-07-2008, 02:50 PM
Depth texture is:
internal format: GL_DEPTH_COMPONENT24
data-type: GL_FLOAT

glGenFramebuffersEXT(1, &frameBuffer);

// create depth texture
depthBuffer = CTexture::CreateNew(width,height,CTexture::fmt_Dep th24,false);
// Initialize the frame buffer object.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);

// attach color textures
// attach depth texture

glBindTexture(GL_TEXTURE_2D, 0);

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
TerminalError("Framebuffer not complete");

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

10-07-2008, 02:54 PM
If you are writing glFragDepth in a fragment shader, why do you need a depth buffer?

To allocate a pdeth buffer in 2D texture just create a classic 2D texture with the appropriate internal format like: GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24_EXT, GL_DEPTH_COMPONENT32_EXT (you need the ARB_shadow extension).

Then attach it to the framebuffer like you already do for color attachment but set GL_DEPTH_ATTACHMENT_EXT is instead of color attachment enums.

10-07-2008, 02:57 PM
I thought that I still need a seperate depth buffer even if I set the depth myself. Is this incorrect?

Thanks for the info about using a texture rather than renderbuffer.

10-07-2008, 02:59 PM
look at examples in the EXT_framebuffer_object (http://www.opengl.org/registry/specs/EXT/framebuffer_object.txt) spec. They are interesting. There is one where they just render to a depth buffer which a 2D texture with no color attachment.


I can't find it in the glsl spec, but in my opinion there is no need to write the gl_FragDepth (written in the fragment shader) to the depth buffer since the depth buffer values are only used when to do not write gl_FragDepth. AFAIK, it would an unecessary operation since after fragment processing, the corresponding depth would potentially change in the next frame.

10-07-2008, 03:05 PM

10-07-2008, 03:11 PM
SO to answer your question after my edit, :) I don't think that you need a depth buffer attached when writing the gl_FragDepth, but that need to be checked.

10-07-2008, 03:21 PM
aah - ok. here's hoping. thanks again. :)