PDA

View Full Version : How is depth stored?

qnoper
04-10-2016, 07:22 AM
Hi.

Implementing a deferred shading, I'd like to don't store myself position to avoid to saturate the memory bandwith.

If I understand well, after the multiplication by the perspective matrix, the z part should be between -1 (near plane) and 1 (far plane).
Until there, it is okay, my problem is the depth buffer contains value between 0 and 1. So there is two possibilities : The first one is : value are converted from [-1, 1] to [0, 1]. The second one values are clamped (and there we lose a lot of information (the range between -1 and 0)...)

When I tried to store value with gl_FragDepth (-1 until 0 give me the same value : 0. I thought i'd have 0 and 0.5), it seems that the second solution was the good one. So I don't understand why OpenGL wants to lose this information...

Thanks !!!

GClements
04-10-2016, 07:52 AM
If I understand well, after the multiplication by the perspective matrix, the z part should be between -1 (near plane) and 1 (far plane).
Until there, it is okay, my problem is the depth buffer contains value between 0 and 1. So there is two possibilities : The first one is : value are converted from [-1, 1] to [0, 1]. The second one values are clamped (and there we lose a lot of information (the range between -1 and 0)...)

After clipping, NDC Z will be between -1 at the near plane and 1 at the far plane. NDC Z is converted to depth based upon the depth range set by glDepthRange(). The default values are 0 and 1, so the transformation is
depth = (Z+1)/2.

When I tried to store value with gl_FragDepth (-1 until 0 give me the same value : 0. I thought i'd have 0 and 0.5), it seems that the second solution was the good one. So I don't understand why OpenGL wants to lose this information...

gl_FragDepth is the depth value, not the NDC Z value. Likewise for gl_FragCoord.z.

qnoper
04-10-2016, 08:07 AM
Hmmmm, okay !

Thanks a lot :).