Original Post
Hi! I'm trying to emulate GL_NV_depth_clamp behavior via GLSL when it's not supported. My problem is that if all 3 of the triangle's vertices fall outside of the near / far clipping region, the triangle is discarded. So I won't have a chance to clamp the Z values in the fragment shader. What I came up with is to save the Z and W values to varyings in the vertex shader and substitute the Z value in the gl_Position with a fake one that's not going to be clipped. Then in the fragment shader I do the perspective division and offset with the saved (and interpolated) Z and W varyings. So I can get back the correct position and do the clamp if needed. The problem is I lose the GL_POLYGON_OFFSET_FILL functionality this way. Because that happens after the vertex shader but before the fragment shader. So it looks I'll have to implement the Polygon Offset functionality in the fragment shader. Can anyone help me with that? I already found out that:
offset = m * factor + r * units
where 'factor' and 'units' are user-given parameters,
dx = dFdx (depth);
dy = dFdy (depth);
'm' = sqrt (dx*dx + dy*dy)
'r' = "The smallest value guaranteed to produce a resolvable difference in window coordinate depth values. The value r is an implementation-specific constant." Is there a way to retrieve or calculate 'r'? Is 'depth' (the value used for derivation) the Z before or after the perspective division? Anyone knows any code example on Polygon Offset implementation in shaders? Or is there a way to disable clipping with the far plane? Thank you!