How to use the Depth texture

A couple of weeks ago I tried to use the depth texture in a 2d shader and all it produced(unless i was in orthographic) was a white screen.

Can someone show me how to use it?

I know this isn’t a bug because other 2d shaders that use it work.

I dont know much of glsl programming, just some little things.

I believe that is not a bug, when more lower are “start” value of the camera more white goeing to be the depth texture.

Try this simple shader:


uniform sampler2D bgl_DepthTexture;


void main()
{
    vec4 depth = texture2D(bgl_DepthTexture,gl_TexCoord[0].st).rgba;
    gl_FragColor = depth;
}

And put the camera values to: start = 1, end=100.

About the use of it, I’m lost in it…The only example that I could do is this(put to the camera the last values):


uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;


void main()
{
    vec4 color = texture2D(bgl_RenderedTexture,gl_TexCoord[0].st);
    vec4 depth = texture2D(bgl_DepthTexture,gl_TexCoord[0].st);
    if (depth.r > 0.5)
    {
    gl_FragColor = depth;
    }
    else
    gl_FragColor = color;
}

I think you have to do something to scale the depth texture’s values by the difference between the start and end values of the camera. I’m not sure though - maybe someone with a bit more knowledge can be of assistance. You could try checking filters that use the depth texture, like martinsh’s SSAO filter.

IIRC the depth value is 0.0 to 1.0 with respect to the camera clipping. If it appears uniform white, then unless OpenGL uses 0-255 for its colours (which I don’t believe it does), you may not be getting a valid input from the depth buffer.

The depth buffer works if the camera is in orthographic perspective though. I guess I’ll try to find a couple of filters that use the depth texture, and see what they do to get it to work.

When dealing with the depth buffer, it is important to keep in mind the non-linear nature of it. There is more precision closer to the camera. This means depth values will vary more for objects closer to the camera, but will not vary much for objects far away from the camera. If you aren’t seeing any variation in your depth buffer, try getting near and far clip values as close together as possible.

Here is an example shader for linearizing the depth buffer to get a better visualization:
http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/