recently i learned the stencil algorithm. While studying the algorithm, i came across a question, which i cant answer by myself.
The question is about the extrusion of edges to infinity. For this purpose an infinite projection matrix is used, which looks like this:
2n/(r-l) 0 (r+l)/(r-l) 0
0 2n/(t-b) (t+b)/(t-b) 0
0 0 -1 -2n
0 0 -1 0
Obviously a point (or vector) (x, y, z, w) with w = 0 gets projected by this matrix to a point in clipspace with a maximum z value of 1. Thus this point is somewhere at infinity. Ok so far, so good. My problem is, that i dont understand why the extrusion to infinity is done like in the following code:
in vec4 vertexPosition; // The object-space vertex position.
uniform vec3 mvpMatrix[4]; // The model-view-projection matrix.
uniform vec3 lightPosition; // The object-space light position
void main()
{
float t = (vertexPosition.w < 0.5) ? 1.0 : 0.0;
vec4 extrudedPosition = vec4(vertexPosition.xyz - lightPosition * t, vertexPosition.w);
gl_Position = vec4(dot(mvpMatrix[0], extrudedPosition),
dot(mvpMatrix[1], extrudedPosition),
dot(mvpMatrix[2], extrudedPosition),
dot(mvpMatrix[3], extrudedPosition) );
}
The code is copied from the book
Mathematics for 3D Programming and Computer Graphics. To understand it you must know, that the vertices which shall get extruded to infinity are marked by a w coordinate of 0.
My question is: Why do we calculate the direction from the light to the vertice and multiply it with the MVP matrix to extrude the vertice.
Ad hoc i would extrude it by calculating.:
vertexPosition.xyz + (vertexPosition.xyz - lightPosition) * BIG_NUMBER
I dont see the extrusion happening. And there is something i dont understand either. As shown above, points with a w coordinate of w = 0 get projected to inifity with a z coordinate of 1. But what if the shadow must be extruded in the direction of the camera, for example if the light is in front of the camera and the object casting a shadow is between the camera and the light. Shouldn't the z coordinate be -1 then?
I hope i could make clear my thoughts to you. I spend hours to find an answer for that, but i could not found anything.
Hope you guys can help me out and make things clear to me.