Originally posted by V-man:
How does that equation (2.6) work anyway?
It calculates the area of a polygon. If the area is negative, the vertices are winded the “wrong” way and it is considered back facing.
As an example, think of the simplest case; a triangle. To calculate the area of a triangle, you can use the cross product. The length of the cross product of two vectors is equal to the area of the parallelogram (think that’s the shape) spanend by the two vectors. The triangle is half that size, hence the factor 0.5.
Now, let’s say we have three vertices (X0, Y0), (X1, Y1) and (X2, Y2) forming a triangle (vertices are in screen space coordinates or a similar coordinate system, like normalized device coordinates). Let’s pick two edges to calculate the cross product. A zero is appended to form three dimensional vectors in order to calculate the cross product.
V = (X1, Y1, 0) - (X0, Y0, 0) cross (X2, Y2, 0) - (X0, Y0, 0)
Half the length of the resulting vector is the area of the triangle. So let’s calculate the cross product of the two vectors above.
V = (0, 0, (X1-X0)*(Y2-Y0)-(Y1-Y0)*(X2-X0))
Since the first two components of V are zero, the length of the vector will be equal to the third component.
Now we expand the third component and reorder the factors, and we get the following.
Area = Vz = (X0*Y1 - X1*Y0) + (X1*Y2 - X2*Y1) + (X2*Y0 - X0*Y2)
Now, try expanding the summation in equation (2.6) for three vertices and see what you get. Hopefully you get the exact same thing as Vz above.
So, together with the factor 0.5, that formula calculates the area of the triangle, given the screen space coordinates of the triangle. A positive area means it’s a front facing triangle, a negative are means a back facing triangle since the vertices are winded in the “wrong” way to produce a positive area.
It’s also easy to expand this to a polygon with an arbitrary amount of vertices. Concider a quad for example. The formula is applied for the two triangles building a quad, and the common edge will appear twice, but in different “directions”, canceling each other from the equation when summed together to calculate the area of the quad. This can then be applied for polygons with more vertices.
Hope that was correct. I just verified it quickly on paper before I wrote this, so I could be wrong somewhere 