# tguen_03

Member

6

1. ## stencil shadows infinite extrusion

Hi, 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.
2. ## stencil shadows infinite extrusion

Thanks for your reply Eric. I finally got it. I didn't consider the hardware which will create the correct vertice if clipping is needed because -w_c <= z_c <= w_c is not fulfilled. For people who are curious, here is my solution: The hardware will find the last Vertice V_l in the direction given by a vector v = (x,y,z,0) starting from a Vertice V_o which is in the view frustrum and will take this Vertice to create the primitive. The Vertice V_l will have a positive w coordinate if v is pointing to the camera, since only then -w_c <= z_c <= w_c can be fulfilled, thus allowing the found point to have a negative z coordinate after the projective division. The Vertice V_l is found by interpolating the values of x,y,z,w on the line from P*V_o and P*v, where P is the projection matrix. The x,y,z values can be interpolated linearly, while the w coordinate gets interpolated by interpolating its reciprocal linearly because it stores the depth. The last Point on the line fullfiling -w_c <= x_c <= w_c -w_c <= y_c <= w_c -w_c <= z_c <= w_c is then taken. As a result the extrusion in clipspace is not like in my sketches, but rather like in viewspace and thus correctly done. I hope its correct what i just wrote : ) Thanks again Eric for your patience and helping me to solve my problem.
3. ## stencil shadows infinite extrusion

Hi, thank your for your responses. Yey, im the first one : ) Yes i realy thought this way, thanks for making this clear. But wouldn't this be false either. The shadow volume would enclose more objects then it should (Ok, in my sketch there is only one object, but if there would be more, then the shadow volume would enclose objects which were originally not shadowed) The situation gets even worse, when you think about a shadow volume perpendicular to the z axis in eye space. [sharedmedia=gallery:images:1777] [sharedmedia=gallery:images:1776] I wonder, why this behaviour is never mentioned in texts describing stencil shadows. Sketches i saw are just extruding the shadow volumes in eyespace where everything looks fine, but in clipspace i got this mess, where everything goes to the far plane.

7. ## stencil shadows infinite extrusion

Thanks for your reply Goldberg, but these slides doesn't answer my question. Furthermore they dont use the zFail method with a inifite projection matrix. The slides describe another way, where cliping is done at the near plane and conventional zPass method is used. But there is one thing in common with my problem. They use vectors with w = 0 to extend vertices in one direction. The slides just say, OpenGL is required to draw homogenous coordinates with w = 0 correctly. But what shall this mean. Isn't it only the projection matrix i use, which determines if my vertices are drawed correctly. With the standard projection matrix, homogenous coordinates with w = 0 get mapped to a z coordinate of (f+n)/(f-n), which is >1, thus the vertices shouldn't get drawed correctly. So i don't understand why the slides state they would. Additionally, at the slides, directions ( vectors with w = 0 ) get extruded in the direction the direction points, but how you can see in my sketches, all directions get only extruded to the far plane and never in the direction to the near plane. Obviously i am wrong, could someone tell me where ?
8. ## stencil shadows infinite extrusion

Thanks for your reply! I refreshed my knowledge in projetive geometry and i finally see, why every direction has the similar point in homogenous coordinates. For people who are curious, here is the math: Point Po(x_0,y_0) is a fixed point of the line a. Suppose, the vector v(a,b) is a direction vector of that line. Then, for point P different from Po, we have: P(x,y) is on line a <=> There is a real number r such that x = x_0 + r * a and y = y_0 + r * b <=> P has homogeneous coordinates (x_0 + r * a, y_0 + r * b, 1) with r not 0. <=> P has homogeneous coordinates (x_0 / r + a, y_0 / r + b, 1/r) If r grows infinitely, P recedes indefinitely along that line and the homogeneous coordinates of P approach to (a,b,0). But (a,b,0) are not the homogeneous coordinates of a point! Now, we add to the line a one extra point. We call that point 'the point at infinity of the line a' or 'the ideal point of a'. That special point receives, by definition, (a,b,0) and each real multiple of (a,b,0) as homogeneous coordinates. [/quote] Its copied from this great homepage http://home.scarlet.be/math/homog.htm Then its realy like i thought, but why does the zFail method work if the points at infinity are all mapped to the far plane. I made two sketches to make clear my thoughts. The first picture shows a scene with a light in front of the camera, a triangle between the camera and the light and a square which gets shadow by the triangle. I also drew the shadow volume how it should be. [sharedmedia=gallery:images:1775] The next picture shows the final result in clip space. Thus the point at infinity are mapped to the far plane, the shadow volume is flipped and the square is not shadowed anymore. Where is my error in reasoning? [sharedmedia=gallery:images:1774] I would realy appreciate if you or someone else could give me the last peaces of the puzzle. Thanks!

11. ## stencil shadows infinite extrusion

Hi, no responses yet. Is there something unclear about my question ? I thought it would be no problem to explain this to me, since everyone is using stencil shadows. So let me know if i something is unclear, i realy must know this : ) Thanks