What would I see if I bind the depth buffer texture on to a quad?

I’m currently binding the Color Texture of my Color Frame Buffer on to a Quad. I see my scene inverted so I know it’s working. But when I render the depth buffer texture on this quad, it’s all red and I’m not sure if it’s that normal or not.

What are ways I can test if the depth buffer texture is right? With the color buffer I see the scene inverted if I bind the texture onto a quad, but I don’t know what to expect with the depth buffer texture. Thanks!

By the way, I’m using WebGL 1.

This is most probably (and can be) normal. The depth buffer is very precise near the near value of the projection matrix, and quickly looses its precision, producing values near 1.0.
Try to look at objects close to the near value of your projection matrix.
You can also relinearize or fake a linearization if that could be enough for your needs.

Edit: you can also try to reduce the far value of the perspective the most you can (see it to the farthest visible object for example). This might not help to vizualize everything. But will definitely help in having a more precise depth buffer.

This can be very useful for debugging, particularly when rendering shadow maps (which are depth buffers) for the first time.

However, the formula in this link you posted doesn’t look right. A formula which takes a 0…1 depth generated by a perspective projection and gives you back the eye-space Z value should result in: f(0) = -n, f(1) = -f. This doesn’t.

I think you’ll find what you want here is:


   eye.z = near * far / ((depth * (far - near)) - far);

Thanks Silence and Photon! I’ll give it a try. I’m trying to use the depth buffer for my water shader. We’ll see how that comes along. Thanks again :slight_smile:

[QUOTE=Dark Photon;1287370]This can be very useful for debugging, particularly when rendering shadow maps (which are depth buffers) for the first time.

However, the formula in this link you posted doesn’t look right. A formula which takes a 0…1 depth generated by a perspective projection and gives you back the eye-space Z value should result in: f(0) = -n, f(1) = -f. This doesn’t.

I think you’ll find what you want here is:


   eye.z = near * far / ((depth * (far - near)) - far);

[/QUOTE]

Thanks for the fix !
You can be at the top results of a google research and still be wrong… (Google)

[QUOTE=Silence;1287384]Thanks for the fix !
You can be at the top results of a google research and still be wrong… (Google)[/QUOTE]

The top search result that comes up there:

[ul]
[li]Getting the true z value from the depth buffer (stackoverflow) [/li][/ul]
appears to be correct. However this:

[ul]
[li]How To Linearize the Depth Value (JeGX’s Lab, Ozone3D) [/li][/ul]
is not.

Note that the stackoverflow link uses zNear = -near and zFar = -far, as the follow-up comment mentions.

another way would to visualize it, make a “depth shader” and draw a screen-wide textured rectangle:

float depth = texture(depthtexture, texcoords).r;
vec4 color = vec4(depth, depth, depth, 1);