how to render depth value to texture using Fbo

I can successfully render depth value to texture under the default framebuffer. But when I try that under a framebuffer object, nothing can be found in the texture.

I setup the texture and Fbo like the following:


glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
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_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); 

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

and called


glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);

before rendering the geometry.

Is there anything wrong here?

There were examples in the Wiki. Not sure where they went.
If you don’t have a color buffer attached to the FBO, I think you need to call glDrawBuffer(GL_NONE), then glBindFramebuffer(GL_FRAMEBUFFER, fbo)

There were examples in the Wiki. Not sure where they went.

They went to a page more appropriately named: “Framebuffer Object Examples”. Of course, since he’s using the core functionality, not the EXT stuff, he’ll have to translate them.

I added glDrawBuffer(GL_NONE) and ran again, still found nothing in the texture (I use gDEbugger to check the texture content).

so I changed to use render buffer object for testing purpose.

here is the code segment:


glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);

glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 512, 512);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);

this time, nothing can be found in the render buffer, but after I changed the code to:


glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 512, 512);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);

I can find a depth image in the rbo.

why the depth component can be rendered to rbo while the color component cannot?

In addition, if I use the following code to write GL_COLOR_ATTACHMENT0 to a texture, it did work.


glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &testTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_FLOAT, NULL);
...
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, testTexture, 0);

So, the problem for me is that I can write Color buffer to texture, while cannot write depth buffer to texture. I can write depth buffer to render buffer object, while cannot write color buffer to RBO.