Value of gl_FragDepth in vertex shader

Started by
1 comment, last by MJP 1 year, 6 months ago

Is there a way to get (or calculate) what the value of gl_FragDepth will eventually be, in the vertex shader? I'm using perspective, so it's non-linear. How can I figure out what openGL is going to eventually send as gl_Fragdepth?

Advertisement

gl_FragDepth is a value output by the fragment shader, it has whatever value you calculate for it.

Are you asking what the value will be that's used for normally used depth testing and eventually stored in the depth buffer if you don't output gl_FragDepth? There can potentially be many fragment depths for a single triangle, since the depth is interpolated from the 3 vertices for each fragment/pixel location. So it doesn't make sense to know what the exact value of the pixel depth will be in a vertex shader. You can compute the depth value at the exact location of the vertex easily enough: just divide the z component of your calculated gl_Position by the w component. However that your vertex depth is not going to exactly match any particular fragment/pixel depth unless the vertex lies exactly on a pixel center, or if the triangle is exactly perpendicular to the camera and therefore has a single “flat” depth value for a fragments.

This topic is closed to new replies.

Advertisement