Render to depth texture


glGenFramebuffersEXT(1, &id);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, TextureID, 0/*mipmap level*/);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextureID, 0/*mipmap level*/);

is the above code correct?

I want to render to a depth texture but all I saw was black. How is that possible if I clear depth to 1.0?
If I make a render buffer and attach to the FBO instead of using a depth texture, I render and I glCopyTexSubImage2D to the depth texture and then my depth texture contains valid values (white with varying degrees).
The color texture looks fine in both cases.

glGetError returns no errors and the framebuffer status is ok.

this code is correct. but how did you set up your depth texture?


glBindTexture(GL_TEXTURE_2D, depthTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT,	GL_FLOAT, 0);

try to use this for setting up your texture (before usage :wink: ).
Also make sure, you set glDrawBuffer and glReadBuffer correct;

then


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
glBindTexture(GL_TEXTURE_2D, targetTextureID);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0,0 width, height,0);

can copy your texture from the depth buffer/texture currently bound to another.
or


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
glDrawBuffer(GL_BACK);
glBindTexture(GL_TEXTURE_2D, depthTextureID);
drawNiceTexturedQuad();

will give you a Quad with the depth texture on it.

I found out, that on some drivers you won’t get a valid depth texture in your FBO when you don’t bind a color buffer (GL_COLOR_ATTACHMENT0_EXT) to the FBO (even when disabling color writes). So, for e.g. shadow maps I always bind some dummy color texture to my FBO.
and always use GL_NEAREST as filter for your depth texture. GL_LINEAR will convert it to 8bit. (at least on nVidia drivers)

greetings
Sebastian

It’s more like this

glBindTexture(GL_TEXTURE_2D, depthTextureID);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT,	GL_FLOAT, 0);

Color texture has the same settings.

Actually I haven’t called glDrawBuffer and glReadBuffer cause it works without them.
And I don’t want to use glCopyTexSubImage2D. THat’s the whole point of binding a depth texture to the FBO.

First major mistake is using GL_LINEAR. use GL_NEAREST in MIN_FILTER and MAG_FILTER.
GL_LINEAR “converts” the depth texture down to 8bit when doing depth buffer lookups.

if you don’t want to use glCopyTex(Sub)Image2D, like I said, use the 2nd method I described.


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
glBindTexture(GL_TEXTURE_2D, depthTextureID);
drawNiceTexturedQuad();

will use the depth-buffer texture as a regular texture.
just make sure, you don’t have the FBO bound to which the depthTextureID is attached to when using it as a texture.

“I found out, that on some drivers you won’t get a valid depth texture in your FBO when you don’t bind a color buffer (GL_COLOR_ATTACHMENT0_EXT) to the FBO (even when disabling color writes).”

Oh, man. This has been bugging me for months. I have been able to encapuslate and abstract RTT functionality, but in every case I only needed depth (i.e shadows), it would stop working. I have settled on creating a dummy colour texture anyways not knowing why it worked when having only depth didn’t.

CD

Changing filter to GL_NEAREST doesn’t help.
It’s not likely I have made a mistake since the glCopyTexSubImage2D path is working fine. I’ll have to play around with the texture formats and perhaps I’ll get lucky.

>> want to render to a depth texture but all I saw was black.
r u sure youre rendering the texture correctly?

GL_TEXTURE_COMPARE_MODE == GL_COMPARE_R_TO_TEXTURE
should be GL_NONE (i think), also in the shaders youre using texture2D instead of shadow2D etc.

Using GL_LINEAR is not a problem, at least it isn’t on my GeForce 7600 Go. Drawing to an FBO with an RGBA8 internal color format and a GL_DEPTH_COMPONENT32 internal depth format works for both GL_NEAREST and GL_LINEAR. If your texture isn’t mipmap-complete the default texture filtering generates the GL_FRAMEBUFFER_UNSUPPORTED_EXT error, so you have to specify GL_LINEAR or GL_NEAREST for the minification filters.
If it is mipmap-complete e.g. mipmaps generated with glGenerateMipmapEXT(GL_TEXTURE_2D) it even works with GL_LINEAR_MIPMAP_LINEAR filtering state.

I simple set a clearcolor and cleardepth and cleared the color and depth buffer before reading back the color and depth values with readpixels and they were correct.

It could be a driver related problem. With earlier drivers I had this weird bug where I got black textures when specifying GL_CLAMP_TO_EDGE for a GL_DEPTH_STENCIL_NV texture…

N.