Jump to content

  • Log In with Google      Sign In   
  • Create Account

stencil shadows infinite extrusion


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 09:54 AM

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.

Sponsor:

#2 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 16 February 2012 - 04:45 AM

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

#3 Eric Lengyel   Crossbones+   -  Reputation: 2489

Like
0Likes
Like

Posted 17 February 2012 - 03:52 AM

First, you must realize the homogeneous vector (x, y, z, 0) corresponds to the point at infinity in the 3D direction (x, y, z).

The extrusion happens in object space, and then both extruded and unextruded vertices are transformed in exactly the same way by the same MVP matrix to get the positions in homogeneous clip space.

When the value of t is 0.0 (because the w coordinate of the vertex is 1.0), then the extruded vertex position does not change, and the new object-space vertex position is (x, y, z, 1.0). When the value of t is 1.0 (because the w coordinate of the vertex is 0.0), then the new object-space vertex position becomes (x - lightx, y - lighty, z - lightz, 0.0), which represents the point at infinity in the direction from the light source to the vertex position. It does not matter that the xyz part of this is not normalized.

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?


Points at infinity in front of the camera and points at infinity behind the camera both get a z coordinate of 1.0 in normalized device coordinates (NDC), after division by the w coordinate. Everything that was behind the camera before projection is actually in front of the camera after projection, but beyond the far plane. See slide 6 of my GDC 2007 talk "Projection Matrix Tricks" for a diagram. You can grab the PDF on this page:

http://www.terathon.com/lengyel/

With an infinite projection matrix, the yellow area above the green NDC box doesn't exist, and the red area above that corresponds to the red area behind the camera before projection. The plane z = 1 in NDC (or z = (f+n)/(f-n) for finite projections) represents both positive infinity and negative infinity at the same time. Think of space as being round and that moving through infinity takes you from positive to negative (or vice-versa) just as moving through zero does.

#4 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 February 2012 - 09:38 AM

Thanks for your reply!

First, you must realize the homogeneous vector (x, y, z, 0) corresponds to the point at infinity in the 3D direction (x, y, z).


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.


Its copied from this great homepage http://home.scarlet.be/math/homog.htm

Points at infinity in front of the camera and points at infinity behind the camera both get a z coordinate of 1.0 in normalized device coordinates (NDC), after division by the w coordinate.


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.
ViewSpace


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?

ClipSpace


I would realy appreciate if you or someone else could give me the last peaces of the puzzle.
Thanks!

#5 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
0Likes
Like

Posted 17 February 2012 - 09:47 AM

See these slides

#6 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 February 2012 - 01:15 PM

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 ?

#7 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
0Likes
Like

Posted 17 February 2012 - 08:43 PM

Thanks for your reply Goldberg

You're the first to address to my be my last name only :)
And I've been in these forums since 2006 ;)

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.

Didn't do the math, but even if the Z coordinate > 1; this means the vertex will be behind the far plane; however the interpolated values between v0 (which has w = 1) and v1 & v2 (which have w=0) will be inside the viewing frustum.
In other words, think of a giant triangle that extents so far that it reaches some sort of magical portal or black hole at which it stops growing any longer. But, since you can't see anything past that point, you don't care.

Z testing is done at pixel level not vertex level, therefore as long as one vertex is on screen and the other 2 have z > 1; it is most likely that you will still see some sort of triangle. You seem to think that if one of the vertices has z > 1 then the triangle can't be rasterized on screen, it doesn't work like that. First the triangle is interpolated; and if the interpolated pixel has z > 1; only then will the pixel be discarded.
(I'm assuming you're not using the unreliable SetClipPlane method; which shouldn't be needed to get stencil shadowing to work btw)

#8 Eric Lengyel   Crossbones+   -  Reputation: 2489

Like
0Likes
Like

Posted 17 February 2012 - 10:03 PM

I would realy appreciate if you or someone else could give me the last peaces of the puzzle.


In your last drawing, "ClipSpace", the shaded region should actually be the inverse of what you have. The shadow volume covers everything except what you've shaded. Think about the normal vectors of the shadow volume's boundary. The normals point out of the shadow volume, but when the shadow volume gets flipped around like that because the camera is inside the extrusion, the normals point into your shaded region, meaning that the shaded region is outside the shadow volume.

#9 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2012 - 06:37 AM

Hi,
thank your for your responses.

You're the first to address to my be my last name only Posted Image
And I've been in these forums since 2006 ;)


Yey, im the first one : )

You seem to think that if one of the vertices has z > 1 then the triangle can't be rasterized on screen, it doesn't work like that.


Yes i realy thought this way, thanks for making this clear.

The shadow volume covers everything except what you've shaded.


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.

ViewSpace2

ClipSpace2


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.

#10 Eric Lengyel   Crossbones+   -  Reputation: 2489

Like
0Likes
Like

Posted 18 February 2012 - 08:38 PM

Remember that clipping occurs before the division by the w coordinate. So the hardware will see extruded vertices with z = -1 (in NDC) for anything extending to infinity behind the camera.

#11 t_guen03   Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 February 2012 - 03:18 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS