I just started using a reverse depth buffer. For some of my materials, I need to write to gl_FragDepth, but I can’t seem to come up with the proper equation for gl_FragDepth with a reverse depth buffer.
The range of my reverse depth buffer is 1 to 0 instead of -1 to 1. I tried using
What do you mean by “reverse depth buffer”? You use glDepthRange(1,0)? Or the projection matrix was constructed to map Zd=near to +1 and Zd=far to -1? Or … what?
Vertex positions written to gl_Position are in clip coordinates. Rasterisation interpolates these to generate a position in clip coordinates (Xc,Yc,Zc,Wc) for each fragment. Normalised device coordinates are obtained by dividing by W: Zd=Zc/Wc. This is then converted to window coordinates using either Zw=Zd*(f-n)/2+(f+n)/2 (for glClipControl(GL_NEGATIVE_ONE_TO_ONE), which is the default), or Zw=Zd*(f-n)+n (for glClipControl(GL_ZERO_TO_ONE)). The resulting Zd is the value of gl_FragCoord.z in the fragment shader, and is the value written to the depth buffer if the shader doesn’t statically assign to gl_FragDepth.
If you’re using a floating-point depth buffer and are mapping the far plane to a depth of zero so that the decreasing accuracy with increasing distance resulting from homogeneous division is counteracted by the increasing precision of floating-point representation closer to zero, then the reversal needs to be incorporated into the projection matrix. It can’t be performed later, because the precision will have already been lost in the clip-space Z value.
Thanks for your response. I don’t have the code in front of me, but by reverse depth I mean I modify the projection matrix and I call glClipControl( GL_ONE_TO_ZERO ). Thanks for your explanation of zd. I’ll give it a shot tomorrow.