# Determining vertex winding order

This topic is 2098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

bool GetFacetWinding(IN IFacet * facet)
{
TReal area = 0;
TReal p0x, p0y, p1x, p1y;
IMesh* m = facet->ownermesh;

IVector3D vNormal = CalculateFacetNormal(facet);

int iAxis0, iAxis1;

//project the triangle
IMath::GetMinorAxes(vNormal, iAxis0, iAxis1);

for(int i = 0; i < facet->indexes.size() - 1; i++)
{
p0x = m->vertices[facet->indexes[i    ]][iAxis0] - m->vertices[0][iAxis0];
p0y = m->vertices[facet->indexes[i    ]][iAxis1] - m->vertices[0][iAxis1];

p1x = m->vertices[facet->indexes[i + 1]][iAxis0] - m->vertices[0][iAxis0];
p1y = m->vertices[facet->indexes[i + 1]][iAxis1] - m->vertices[0][iAxis1];

area += (p0x * -p1y) + (p1x * p0y);
}

return(area < 0);
}



I'm getting opposite winding for opposite sides of a cube. This can't be right as the cube renders just fine. Either I'm missing something super obvious here or I need to get some rest.

##### Share on other sites

Are the windings opposite with respect to the face normal, or what do you mean?  Can you give more detail about what you are seeing that is not working?

##### Share on other sites

Sorry for being vague.

After building a box from faces that should be winding the same way (or else they'd get culled), I would expect all faces to have the same winding (either clockwise or counter-clockwise). This function should return either true or false for all of them. It returns true for faces facing in one direction and false for faces facing in the opposite direction. Here's an example:

Normal         Winding

(0, 0, -1)            1

(0, 0, +1)            0

(-1, 0, 0)            1

(+1, 0, 0)            0

(0, -1, 0)             0

(0, +1, 0)            1

Looking at my code now, I think I see why the results are wrong - the faces facing in opposite directions aren't being affected by the normal while physically the order indeed is opposite. I'm not sure how to fix it, though.

##### Share on other sites

The way you determine the normal already depends on the winding (order of vertices), since the two triangle-edge vectors you use to get the normal already have a specific order in your "facet", so the normal has nothing to do with it.

Also, I think (please correct me if I'm wrong) the formula you're using determine the winding only works for 2d triangles but your code also allows higher-order polygons, so your m->vertices should already be transformed into projection space or reference plane, and it should contain only triangles.

In fact, in your case you don't even need to use the area formula, as the sign of the z from the normal will give you the winding (assuming that the original vectors used in the normal cross-product calculation had x and y set to the pre-transformed 2d coordinates and z=0) - i.e, the normal will be pointing either towards positive z or negative z, and the x and y components of the normal will be 0.

If you do use transformed vertices, keep in mind that using a perspective projection matrix will give you the same winding for left& right faces of of a cube when the cube's front/back faces are perfectly parallel to the projection plane. The same goes for the top and bottom faces. Only the front and back faces will have different winding. In an orthogonal projection, all faces that are opposite to each other will have opposite windings, unless the faces are perpendicular to the projection plane, in which case you can't determine the winding (because the z value of the normal will be 0).

Edited by tonemgub

##### Share on other sites

bool GetFacetWinding(IN IFacet * facet)

This does not make sense. There is no 'right' winding order for any triangle or polygon.

You could write a function which tests for a facet and a camera whether the facet is visible using that particular camera.

You could also test whether all facets of a whole object are winded consistently (for example because you know the object should be watertight).

##### Share on other sites

bool GetFacetWinding(IN IFacet * facet)
This does not make sense. There is no 'right' winding order for any triangle or polygon.

You're right - the name of the function should read as IsFacetWindingClockwise().

You could also test whether all facets of a whole object are winded consistently (for example because you know the object should be watertight).

This is exactly what I'm trying to accomplish. Point in case - the faces of a cube should be wound consistently. My test fails to show that. Note that I'm aiming to determine the winding of a 3D polygon.

In short - if I pass in a set of polygons taken from a closed mesh or polytope - even if it's concave -, I want them all to consistently return the same winding.

Also, I think (please correct me if I'm wrong) the formula you're using determine the winding only works for 2d triangles but your code also allows higher-order polygons, so your m->vertices should already be transformed into projection space or reference plane, and it should contain only triangles.

The lines:

int iAxis0, iAxis1;

//project the triangle

IMath::GetMinorAxes(vNormal, iAxis0, iAxis1);

do exactly that. They use the normal to identify the minor axes (eg the two non-major axes) and the rest of the code uses the triangle that is projected onto this plane as baseline.

In fact, in your case you don't even need to use the area formula, as the sign of the z from the normal will give you the winding (assuming that the original vectors used in the normal cross-product calculation had x and y set to the pre-transformed 2d coordinates and z=0) - i.e, the normal will be pointing either towards positive z or negative z, and the x and y components of the normal will be 0.

Hm - unless I'm misunderstanding you, you're suggesting the following code should basically give me the winding?

bClockwise = vNormal[iMajorAxis] > 0;

Where iMajorAxis is the predominant axis of the vector, eg z for (0, 0, -1). I'll be honest, my brain's a little garbled at the moment but at least one case I can see where it fails is for faces that are diagonal with respect to all major planes, eg faces with (unnormalized) normals like (1, 1, 1), (-1, 1, -1) etc. That is, cases where no major axis exists (come to think of it my original code doesn't account for these either).

##### Share on other sites
Just for the record, your last two quotations are wrong. tonemgub said that, not me.

##### Share on other sites

You're right - the name of the function should read as IsFacetWindingClockwise().

That function could only make sense if the facet comes with a normal, so it would check if the normal and the winding are consistent or not. If you think that given three points in space there is a notion of "clockwise", you need to revisit your thinking.

One way to convince yourself of this is that you can start with an isosceles triangle and rotate it around the bisector of the angle that's different. When you rotate 180 degrees, you'll get to the same vertices but with different winding. The question is, at what point did the winding change? There is no good answer: You need a normal vector to answer it, which tells you something like where you are looking at the situation from.

##### Share on other sites

In short - if I pass in a set of polygons taken from a closed mesh or polytope - even if it's concave -, I want them all to consistently return the same winding.

That is somewhat more promising: You can determine if two polygons that share an edge have the same winding or not, just by checking that the common edge is mentioned in reverse order. You can then start with a given winding for one of the polygons and propagate it to neighboring polygons. If your polygons form a closed mesh, you'll get your desired consistency.

##### Share on other sites

That function could only make sense if the facet comes with a normal, so it would check if the normal and the winding are consistent or not. If you think that given three points in space there is a notion of "clockwise", you need to revisit your thinking.

It looks like he is projecting the triangles onto the xy-plane, so in that sense you could say that one winding order or the other is "clockwise" - but in general I agree with you that you can't define a winding order with just three points.  The usual way to find the winding is to calculate the normal using the cross product of the first two edges and compare that with the face normal vector.  Depending on if they point in the same direction, you can figure out what the winding order is.

Since you apparently don't have normal vectors in your facets, you will have to make some assumptions.  If you can guarantee that the mesh is watertight and that all vertex locations that occupy the same position will be the same vertex (i.e. not allow multiple vertices in the same place) then you can check the order of the vertices on each *edge* and compare it to the vertex edge order of the other primitives that share those vertices.  If it is a watertight mesh, you can assume that only one other face will share the vertices of an edge, and that they should be the opposite order in the other face.

I think that would give you a good indication of the "sameness" of the winding of the faces, but no information about an absolute winding order.  Hope that helps...

Edited by Jason Z

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 20
• 20
• 11
• 9