PDA

View Full Version : What would I see if I bind the depth buffer texture on to a quad?



hashbrown
06-05-2017, 11:10 PM
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.

Silence
06-06-2017, 01:21 AM
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 (http://www.ozone3d.net/blogs/lab/20090206/how-to-linearize-the-depth-value/) 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.

Dark Photon
06-06-2017, 06:57 AM
You can also relinearize (http://www.ozone3d.net/blogs/lab/20090206/how-to-linearize-the-depth-value/)...

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);

hashbrown
06-06-2017, 09:34 PM
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 :)

Silence
06-06-2017, 11:52 PM
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 for the fix !
You can be at the top results of a google research and still be wrong... (https://www.google.fr/?gws_rd=ssl#q=opengl+linearize+depth+buffer)

Dark Photon
06-07-2017, 06:05 AM
Thanks for the fix !
You can be at the top results of a google research and still be wrong... (https://www.google.fr/?gws_rd=ssl#q=opengl+linearize+depth+buffer)

The top search result that comes up there:


Getting the true z value from the depth buffer (https://stackoverflow.com/questions/6652253/getting-the-true-z-value-from-the-depth-buffer) (stackoverflow)

appears to be correct. However this:


How To Linearize the Depth Value (http://www.ozone3d.net/blogs/lab/20090206/how-to-linearize-the-depth-value/) (JeGX's Lab, Ozone3D)

is not.

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

john_connor
06-07-2017, 09:50 AM
What are ways I can test if the depth buffer texture is right?

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);