Correcting the Winding Order

Given 3 vertices in a triangle, computing the surface normal will tell you in which direction the triangle is facing i.e. front facing (CW) or back facing (CCW).

If this is correct and I get a -z for the surface normal, how do I correct this? I basically want to ensure all the faces of an object I’m loading are facing front so that they are properly lit.

Thanks

Change the sequence of any two vertices of the triangle.
Ie, swap two indices or swap the whole vertices, depending on your data representation.

I was talking with a friend and he had recommend this. I tried it and it doesn’t seem to work. Maybe my implementation is flawed. Now, what if you want to perform this for any convex polygon? How do you go about doing that? I imagine the first half is the same, but then you have many points you might need to swap. Thanks for the response!

Well that was the special case for a triangle.
See this:

You have a triangle with points 1,2,3
So (1,2), (2,3), (3,1) are it’s edges. You can “rotate” the vertices and the triangle does not change: 1,2,3 = 2,3,1 = 3,1,2

Okay, now you want to change the winding, which would obviously be right for 3,2,1 (in case 1,2,3 was the original triangle).

Now let’s see the cases:

2,1,3 (first two flipped) => 3,2,1 with a right rotation
3,2,1 (first and third flipped) obviously right
1,3,2 (last two flipped) => 3,2,1 with a left rotation.

Now this does not work for an arbitrary polygon with points 1…n . There you can flip the first or last n-1 points, which yields (first n-1 flipped) n-1, n-2, … 1, n, which can be right-rotated again yielding n…1, or (last n-1 flipped) 1, n, n-1, …, 2 which can be left rotated to n…1.

Here you can see that the triangle case is just a special case of the last thoughts.

Thank you again for your reply. Do you think it possible to explain with some psuedo code? Thanks.

void poly::flip_winding()
{
   for( i=0; i<(num_vertices-1)/2; ++i ) {
       swap( vertices[i], vertices[num_vertices-2-i] );
   }
}

if i’m not mistaken

Okay, thanks again for the reply. I have another question though. Most of the time this whole winding thing counts on the polygon being flat and facing toward (or away) from you. What if the polygon is on angle or sits in only two planes. How do you account for these things?

Use triangles so that each polygon is a flat plane.

If you have quads, then cut them into triangles. If it’s a full polygon (at the gl sense), then you can compute the gravity center of it and use triangles all sharing the gravity as a vertex.

Originally posted by jide:
[b]Use triangles so that each polygon is a flat plane.

If you have quads, then cut them into triangles. If it’s a full polygon (at the gl sense), then you can compute the gravity center of it and use triangles all sharing the gravity as a vertex.[/b]
I’m not quite sure how to go about the either of the steps. I know first you would need to tesselate, but I haven’t really looked into that. I know glu provides this functionality but I haven’t fiddled with that yet. The second step I have no idea.

I think there should be some easier solution. Currently I load up some models and they look good EXCEPT I will get some:

  1. Rendering artifacts
  2. Polygons show through other polygons (for example, the blender monkey)

I think that 1 and 2 are possibly the same thing (meaning that the “artifacts” are single polygons showing through, not groups of them).

When I try some simple primitives like a cube, ico, cone etc it works fine no flicker at all (everything is culled and no artifacts). This leads me to believe some of the polygons are wound incorrectly (because if they were wound correctly they would be culled). If this is the case I would like some generic algorithm to detect this for any polygon with n vertices.