This page will explain how to recompute eye-space vertex positions given window-space vertex positions. This will be shown for two cases. Case 1 uses gl_FragCoord​ in its entirety. Case 2 uses only gl_FragCoord.xyz​. Both require access to the projection matrix.

This assumes the presence of a uniform called viewport​, which is a vec4​, matching the parameters to glViewport​, in the order passed to that function. Also, this assumes that invPersMatrix​ is the inverse of the perspective projection matrix (it is a really bad idea to compute this in the fragment shader). Note that gl_DepthRange​ is a built-in variable available to the fragment shader.

From XYZ of gl_FragCoord

This case is mostly useful for deferred rendering techniques. In deferred rendering, we render the material parameters of our objects to images. Then, we make several passes over these images, loading those material parameters and performing lighting computations on them.

In the light pass, we need to reconstruct the eye-space vertex position in order to do lighting. However, we do not actually havegl_FragCoord​; not for the fragment that produced the material parameters. Instead, we have the window-space X and Y position, from gl_FragCoord.xy​, and we have the window-space depth, sampled by accessing the depth buffer, which was also saved from the deferred pass.

What we are missing is the original window-space W coordinate.

Therefore, we must find a way to compute it from the window-space XYZ coordinate and the perspective projection matrix. This discussion will assume your perspective projection matrix is of the following form:

[ xx xx xx xx ]
[ xx xx xx xx ]
[ 0 0 T1 T2 ]
[ 0 0 E1 0 ]

The xx​ mean "anything;" they can be any value you use in your projection. The 0's must be zeros in your projection matrix. T1​, T2​, and E1​ can be any arbitrary terms, depending on how your projection matrix works.

If your projection matrix does not fit this form, then the following code will get a lot more complicated.

From window to ndc

We have the XYZ of window space:

Computing the NDC space from window space is the same as the above:

Just remember: the viewport and depth range parameters are, in this case, the parameters that were used to render the original scene. The viewport should not have changed of course, but the depth range certainly could (assuming you even have a depth range in the lighting pass of a deferred renderer).

From NDC to clip

For the sake of simplicity, here are the equations for going from NDC space to clip space:

Derivation

Deriving those two equiations is very non-trivial; it's a pretty big stumbling block. Let's start with what we know.

We can convert from clip space to NDC space, so we can go back:

The problem is that we don't have Cw. We were able to use gl_FragCoord.w​ to compute it before, but that's not available when we're doing this after the fact in a deferred lighting pass.

So how do we compute it? Well, we know that the clip space position was originally computed like this:

Therefore, we know that Cw was computed by the dot-product of P with the fourth row of M. And given our above definition of the fourth row of M, we can conclude:

Of course, this just trades one unknown for another. But we can use this. It turns out that Nz has something in common with this:

It's interesting to look at where Cz comes from. As before, we know that it was computed by the dot-product of P with the third row of M. And again, given our above definition for M, we can conclude:

We still have two unknown values here, Pz and Pw. However, we can assume that Pw is 1.0, as this is usually the case for eye space positions. Given that assumption, we only have one unknown, Pz, which we can solve for: