|See what's going on with flipcode!|
Polygon Winding Order
Question submitted by (29 May 2001)
|Return to The Archives|
|How do you determine the winding order of a polygon? I have a mesh, which I exported from 3D Studio Max, that when drawn solid does not draw some of the polygons. I am wondering if it might be because the winding order of the polygons is not consistent from max and some of the polygons are getting culled out that shouldn't be.|
The short answer is: you don't. Pick your jaw up off of your chest and hear
me out. =)
Consider a domed room where the top of the dome is built using polygons that form a hemisphere. Now consider a bowl sitting on the table, turned upside down; again, another hemisphere of polygons. The difference between these two (other than hopefully their scale) is that the polygons for the bowl point outward, away from the center of the hemisphere, and the dome's polygons point inward.
As you can imagine, by simply analyzing the geometry of the object, you can't determine which way is inside or out. Instead, it's your job to define a standard. If you get all of your data from a single source, then figure out what orientation that source uses. Remember, that if you're using an export plugin (for Max) that you'll need to know the winding order of the exported data. This usually isn't too hard to determine. Simply try it one way, and if that doesn't work, try the other way. If you find that neither way works best all the time, then your bug is elsewhere. Either your culling code has a few problems that need to be worked out, or your data source doesn't maintain a constant standard (i.e. always clockwise or always counter clockwise.)
If your data comes from multiple sources, using both winding orders, then you'll need to pick one and stick to it. When loading data from a source that doesn't follow the standard you've chosen, it's up to you to change the winding order (either when loading data from that source, or when exporting it.)
Note that for performance reasons many software renderers require a specific winding order. If your input data set doesn't match this winding order, you're in for trouble.
If your data comes from a single source, and you know the winding order of that data source, but you still have some polygons disappearing, there are a few things you can try. First, disable all culling (if you can) and make sure that solves the problem. If this doesn't solve the problem, then your problem isn't a culling issue. If this does solve the problem, then it's either the renderer requiring a specific orientation (hardware drivers can support both winding orders, provided you've properly set the render state.)
If you're doing the culling yourself, make sure you're doing it right. There's a common mistake that many people make when they first learn 3D: take the camera's direction vector and DOT that with the polygon's normal. This simply won't work. Why? Consider a cube directly in front of the camera. The left-side face of this cube is not visible (only the front face of the cube is visible.) Now translate the cube to the right, until you can see the right-side face. Notice how the camera's direction didn't change, and neither did the normal for the left-side face. Yet the culling is different. In order to solve this problem, you must generate a vector from the camera to one of the vertices on the polygon, and DOT that with the polygon's normal. Another option is to use a half-plane calculation (i.e. decide which side of the polygon's plane the camera is on.)
In situations like this, consistency is your friend.
Response provided by Paul Nettle
This article was originally an entry in flipCode's Ask Midnight, a Question and Answer column with Paul Nettle that's no longer active.