• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# correct winding for a polygon

## Recommended Posts

I''ve looked through these pages trying to find the information I need, but have only found other related things I already knew, but I apologize if this is a repeatedly asked question. Given a set of three points and a desired orientation, is there an algorithm that can order these points so that when drawn in that order, the corresponding polygon would have the correct winding? I understand that you want them to be counter-clockwise by default, and that by hand it is easy to see which way is counter-clockwise for a variety of polygons, but I''m dealing with over a thousand polygons so it''s not feasable to check each one, I''m writing a program to calculate all of this, and everything looks great until I start lighting (normals are not properly calculated I''m assuming, along with the order I''m declaring the points). Any assistance would be appreciated. Elijah

##### Share on other sites
yes, there is a simple algorithm to see what order they are in. you would probably not want to change their order every frame, but rather do it once in initialization. this code is from The Black Art of 3D Game Programming.

  	if((x1==x2 && x2==x3) || (y1==y2 && y2==y3))		return;	//sort vertices	if(y2<y1)	{ //little swapping trick		x1^=x2^=x1^=x2;		y1^=y2^=y1^=y2;	}	if(y3<y1)	{		x1^=x3^=x1^=x3;		y1^=y3^=y1^=y3;	}	if(y3<y2)	{		x2^=x3^=x2^=x3;		y2^=y3^=y2^=y3;	}	if(y3<0 || y1>200 || (x1<0 && x2<0 && x3<0) ||		(x1>320 && x2>320 && x3>320))		return;

the x^x1^... craziness is just a bitwise swap technique I added.

My Homepage

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978696

• 21
• 14
• 12
• 10
• 12