Get depth value from a texture

Hi,
I have created a texture to get the depth buffer datas :

  
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, texWidth, texHeight, 0,GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_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);
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, texWidth, texHeight );

When I display the texture on screen it works. But now I would like to get depth datas of the texture in the fragment shader and display them, so i used this :

varying vec2 TexCoord;
uniform sampler2D BaseImageDepth;
void main (void)
{
   gl_FragColor = texture2D( BaseImageDepth,TextCoord);
}
  

But the result is in RGB, I don’t get the depth value. I also try another way but i have exactly the same result (in RGB):

 
varying vec3 TexCoord;
uniform sampler2DShadow BaseImageDepth;
void main (void)
{
	gl_FragColor = shadow2D( BaseImageDepth , TextCoord);
}
 

So do you know where can be the problem ?
thanks by advance!

From the spec (section 8.7)–
If a non-shadow texture call is made to a sampler that represents a depth texture with depth comparisons
turned on, then results are undefined. If a shadow texture call is made to a sampler that represents a depth
texture with depth comparisons turned off, then results are undefined. If a shadow texture call is made to
a sampler that does not represent a depth texture, then results are undefined.

If a non-shadow texture call is made to a sampler that represents a depth texture with depth comparisons
turned on, then results are undefined. If a shadow texture call is made to a sampler that represents a depth
texture with depth comparisons turned off, then results are undefined. If a shadow texture call is made to
a sampler that does not represent a depth texture, then results are undefined.
Thanks, but i have already read this. My depth function is correct : glDepthFunc(GL_LESS);
and i use a shadow texture (“sampler2DShadow” and “shadow2D”)and i get my depth texture as in my first post.
So i don’t know why it doesn’t want to work.

Your first version of the shader (using sampler2D and texture2D()) is the correct one. Using sampler2DShadow and shadow2D() implies that depth comparisons are switched on, and thus will not allow you to read the depth values themselves. The result of shadow2D() will be 0 or 1 if depth comparisons are on, and undefined if they are off (as quoted from the spec by Dodo).

What exactly were the results you were getting with texture2D()? All black? All white? Are you sure it’s not a range problem, e.g. all your depth values are very similar and the result just looks like a single flat color?

– Tom

in fact, with texture2D, the result is the same than if I didn’t use shaders it doens’t display grayscale result.
althought if I display the texture without shaders I got a correct result

I had similar problem too. Im using pbuffer for shadowmap and one depth texture. So, if I put following code while setup depth texture it works in fixed function pipeline, but in FS I got “white” pixels. If, I remove this code from texture setup I can use depth texture from FS with sampler2D and texture2D.

  
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

yooyo

Hey pantxua,

I have exact the same problem you’ve written about here. Do you still know the solution (if there was one)?

Thx

On NVIDIA GPUs you need to setup your depth texture like so to get full precision depth in the fragment shader:

  • use GL_DEPTH_COMPONENT24 for internalFormat
  • use GL_LUMINANCE as the GL_DEPTH_TEXTURE_MODE
  • use GL_NEAREST/GL_NEAREST filtering
  • set GL_TEXTURE_COMPARE_MODE to GL_NONE

In your fragment shader you would use sampler2D/texture2D to fetch from this texture instead of using sampler2DShadow and shadow2DRect.

I am able to render the depth texture with my frag shader correctly. I have realized, the real problem is that I am just not able to access the depth texture of my multitexture, because my TEXTURE0 unit is of the type RGBA.

See new post SORRY, AGAIN MULTITEXTURE and thx for your help.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.