Could you tell me what is the correct calculation from a z in post-perspective space (Zp) to a gl_FragDepth in a shader to obtain the same value as fixed function ?
First transform Zp:
// Zp is in [-1,1] transform it to [0,1] => g
float g = Zp * 0.5 + 0.5 ;
I found several explanations here and there:
gl_FragDepth = (gl_DepthRange.far * gl_DepthRange.near) / ( gl_DepthRange.far - g *( gl_DepthRange.far - gl_DepthRange.near )) ;
Or
gl_FragDepth = gl_DepthRange.diff / gl_DepthRange.far * g ;
Or
gl_FragDepth = (1/gl_DepthRange.near - 1/g) / (1/gl_DepthRange.near - 1/gl_DepthRange.far)
Or ...
And also, where to add a constant offset to simulate a glPolygonOffset ?