depth testing with FBO

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…

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.

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.

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_Depth24,false);
    // Initialize the frame buffer object.
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);

	// attach color textures
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_2D,Textures[i]->TexID, 0);
	// attach depth texture
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,  GL_TEXTURE_2D, depthBuffer->TexID, 0);

    glBindTexture(GL_TEXTURE_2D,  0);

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

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

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.

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.

look at examples in the EXT_framebuffer_object 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.


SO to answer your question after my edit, :slight_smile: I don’t think that you need a depth buffer attached when writing the gl_FragDepth, but that need to be checked.

aah - ok. here’s hoping. thanks again. :slight_smile: