Rendering to depth buffer with FBO

Hi all.

Does anyone know about an example, preferably using glut that uses the FBO to render depth to a texture which is then rendered onto the framebuffer? (attached to a quad for example, just to see the content of the depth buffer).

I can get the COLOR buffer result, but when I also attach a depth buffer, all I get in the render target (texture) for the GL_DEPTH_ATTACHMENT_EXT is black texture.

I have tried with setting up the depth target texture as: GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_2D etc…
but nothing works…
Im using a Geforce 6200 FX go so it should support all of the required functionality.

So, does anyone know of working code for this?

there are examples of this in the spec

Did anyone get this to work ? I followed spec’s example #7 and i also get a black texture.

are your depthbuffer 24bit i know at least on gffx 16bit depth doesnt work with color attachment

Don’t forget to release the RTT texture bind and disable texturing for the target, before you draw your depth texture. I get some deep space blackness–sometimes even a crash–if I don’t.

Make sure that you have your depth texture color mode and your depth texture compare mode set correctly. I’ve been using ‘render to depth texture’ on NVidia for several months and it works great. Make sure you have your render states correct for your quad as well… perhaps try with a color texture filled to a specific to verify this?

Kevin

hi all!

im having exacly the same problem!

here is a starter:

// Init()

GLuint m_DepthBufferId;
GLuint m_FrameBufferId;

glGenTextures((GLsizei) 1, &m_FrameBuffer->GetId());
glBindTexture(GL_TEXTURE_2D, m_FrameBuffer->GetId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

glGenTextures((GLsizei) 1, &m_DepthBuffer->GetId());
glBindTexture(GL_TEXTURE_2D, m_DepthBuffer->GetId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_Width, m_Height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

// Initialize Frame Buffer
glGenFramebuffersEXT(1, &m_FrameBufferId);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FrameBufferId);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_FrameBuffer->GetId(), 0);
CheckStatus();

// Initialize Depth Buffer
glGenRenderbuffersEXT(1, &m_DepthBufferId);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_DepthBufferId);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, m_Width, m_Height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_DepthBuffer->GetId());
CheckStatus();

// Render()

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FrameBufferId);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_DepthBufferId);
//Bind textures and Render…
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

can anyone help clear this?

Make sure that you have your depth texture color mode and your depth texture compare mode set correctly

is this correct?

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_Width, m_Height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);

I’d suggest changing GL_COMPARE_R_TO_TEXTURE to GL_NONE. This will allow you to visualize the contents of your depth buffer. This should let you determine whether or not the contents of your depth texture make sense. If you actually get what looks like valid depths, then perhaps your texture coordinates are incorrect.

Kevin B