You are missing a few key points.
After the application of the projection matrix, you have a 4-component vector in clip space (not screen space), which is a homogeneous coordinate system in which clipping will be performed (after your vertex shader).
After clipping, the surviving coordinates are divided by the w
component to get normalized device coordinates in (-1, 1). A transformation will then be applied to move from NDC space to window coordinates, where the X and Y coordinates are normalized based on the viewport provided to OpenGL and the Z coordinate is normalized based on the depth range, which is ultimately what gives you your (0, 1) range for depth (unless you use glDepthRange
to set a different range).
If you want to access this normalized Z value in your vertex shader, you will need to do the computation manually in the shader (based on the information above).
gl_FragCoord.z
should give the window-space (normalized) depth in the fragment shader. – Nathan Reed Nov 21 '13 at 02:51gl_Position.z
. (divided bygl_Position.w
) ;) and you wouldn't need near/far uniforms anyway. the projection matrix already has the computed values to project z. lol which again, is already done when you calculategl_Position
from the projection matrix. ;) – Puddle Dec 03 '19 at 01:55