Jump to content

  • Log In with Google      Sign In   
  • Create Account


Determining vertex winding order


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 irreversible   Crossbones+   -  Reputation: 1240

Like
0Likes
Like

Posted 13 January 2014 - 02:44 PM

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.



Sponsor:

#2 Jason Z   Crossbones+   -  Reputation: 4858

Like
0Likes
Like

Posted 13 January 2014 - 03:11 PM

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?



#3 irreversible   Crossbones+   -  Reputation: 1240

Like
0Likes
Like

Posted 13 January 2014 - 03:40 PM

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.



#4 tonemgub   Members   -  Reputation: 751

Like
0Likes
Like

Posted 14 January 2014 - 05:33 AM

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, 14 January 2014 - 05:37 AM.


#5 BitMaster   Crossbones+   -  Reputation: 3784

Like
0Likes
Like

Posted 14 January 2014 - 06:03 AM

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).

#6 irreversible   Crossbones+   -  Reputation: 1240

Like
0Likes
Like

Posted 17 January 2014 - 05:42 AM

 

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).



#7 BitMaster   Crossbones+   -  Reputation: 3784

Like
0Likes
Like

Posted 17 January 2014 - 06:38 AM

Just for the record, your last two quotations are wrong. tonemgub said that, not me.

#8 Álvaro   Crossbones+   -  Reputation: 12450

Like
0Likes
Like

Posted 17 January 2014 - 07:09 AM

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.

#9 Álvaro   Crossbones+   -  Reputation: 12450

Like
0Likes
Like

Posted 17 January 2014 - 07:14 AM

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.

#10 Jason Z   Crossbones+   -  Reputation: 4858

Like
0Likes
Like

Posted 17 January 2014 - 03:14 PM


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, 17 January 2014 - 03:14 PM.


#11 JohnnyCode   Members   -  Reputation: 194

Like
0Likes
Like

Posted 18 January 2014 - 12:03 AM

 

 

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.

Yes, without explicit normal info, the winding consistancy is just interpretation open.

Consider a mesh consisting of two planes, not connected, with one plane facing opposite than the other. Will you consider the winding broken? Or will you consider it a correct mesh?



#12 Jason Z   Crossbones+   -  Reputation: 4858

Like
0Likes
Like

Posted 18 January 2014 - 09:27 AM


Consider a mesh consisting of two planes, not connected, with one plane facing opposite than the other. Will you consider the winding broken? Or will you consider it a correct mesh?

Strictly speaking, if you don't specify normal vectors, then there is no way to tell if they are facing opposite directions :)  However, the test that the OP is doing would tell you if the windings were opposite of one another in that test case...



#13 tonemgub   Members   -  Reputation: 751

Like
0Likes
Like

Posted 20 January 2014 - 02:25 AM


Just for the record, your last two quotations are wrong. tonemgub said that, not me.

Yup -but  I was talking only about the 2d case, where the winding is relative to the 2d plane, so the normal.z value can be easily interpreted as winding.

Because in OP code snippet, it seems that only 2d values are used in the area calculationL p0x and p0y - there's no p0z.

And in that case, there is no need to use the area at all. He can just use the z value from the normal, assuming that the normal is computed using the dot product, and that the order of vertices used in the vector-subtraction operations to get the two vectors that are passed to the dot product is always the same, for each triangle.



#14 JohnnyCode   Members   -  Reputation: 194

Like
0Likes
Like

Posted 20 January 2014 - 03:50 AM

 

I'm getting opposite winding for opposite sides of a cube. This can't be right as the cube renders just fine

Cube is an enclosed mesh, and as for enclosed meshes, it makes sance to detect winding consistancy. If winding would be inconsistant, you would not have an enclosed mesh, but a spike-open edge mesh. Your algorithm returns different results becouse it projects the cube. You should go the way that Alvaro suggested, that would perfectly work for enclosed meshes.

 

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.

it does not matter then wheather you start with an hypothetical inverse triangle or correct one, since you check consistancy. This would be a robust function that would return surface validity, wheather it was a cube, or inverted cube-room. But for open meshes without normal info, it is pointless to check on winding.

 

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS