Search code examples
openglperspectivecamera

Z value bigger than 1 after w division


What I am doing in vertex shader is:

shadowCoord = shadowVP * mMatrix *  vec4(vertex_position,1.0);

Now to get it back in the range [-1, 1] I did this in the fragment shader:

vec3 proj = shadowCoord.xyz / shadowCoord.w;

But if I test the z value of such point I get a value bigger than 1.

The perspective matrix I use is obtained via:

glm::perspective(FOV, aspectRatio, near, far); 

And it results in:

[2.4142  0      0      0 
 0     2.4142   0      0
 0       0    -1.02   -1
 0       0    -0.202   0]

and the shadowVP is:

shadow_Perp * shadow_View

Shouldn't proj.z be in the range [-1,1]?


Solution

  • Shouldn't proj.z be in the range [-1,1]?

    No. It is in the range [-1,1] if the point lies inside the frustum. And the frustum is defined as -w <= x,y,z <= w for any vetrex in clip space (and that w varies per vertex). But you don't do any clipping, so any value can result here. Note two things:

    1. While I said the implication "v inside the frustum" => "NDC coords in [-1,1]" holds true, the opposite does not. That means you can get the NDC coords inside [-1,1] for points which lie outside of the frusutm (that might even lie behind the "viewing position").

    2. You might also get the division by 0 here.