# Triangles Winding...

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

## Recommended Posts

I am converting an Models Database... Found that some models got some Triangles with no CCW vertex order, so, I have to fix them... is there a way ( knowing the plane Equation of the Triangle ) to figure out if the vertices are CW or CCW winding ordered...? tnx [R]ed

##### Share on other sites
A mesh that has consistent winding satisfies the property that each edge is used by 2 triangles and each triangle refers to the edge in the opposite direction (p0p1 in triangle T0 and p1p0 in triangle T1, for example). So you could start with one triangle that is wound correctly and use a floodfill algorithm to go around all triangles that share an edge with the current one, ensure that they wind correctly (correcting any edges that don't), and then visit all their neighbors.

Christer Ericson had an article about this in Real-Time Collision Detection I think. The important part was first generating connectivity info so that you can quickly identify the other triangle that shares an edge with the current one.

##### Share on other sites
Tnx... but not what I am looking for...
I have no meshes... I have some triangles having no collision matter...
I have to reverse them if ( badly ) CW winded...

nothing else...

tnx the sane
[R]ed

##### Share on other sites
Oh, you know the plane equation! Yeah, just take the cross product: (p1-p0)X(p2-p0) and dot that with the plane normal. the positive or negative result will consistently indicate CW or CCW winding.

##### Share on other sites
If i understand the question correctly, you've got a bunch of triangles specified as three verticies and the plane equation (including normal direction). Different triangles may have different windings. Triangles do not necissarily form a manifold mesh.

If this is the case, for each tri call the verticies v0, v1 and v2 (in the order they are specified in the source data). Calculate M = (v2-v0) X (v1-v0), this gives you the normal direction of the tri, if it has CW winding. Let N be the normal from the tri's plane equation.

M.N > 0 => CW winding
M.N < 0 => CCW winding

Does this answer the original question?

##### Share on other sites
Beat me to it ajas95 :)

##### Share on other sites

Vertices V0, V1, V2

// get vectors
x1=V1.x-V0.x
y1=V1.y-V0.y
z1=V1.z-V0.z

x2=V2.x-V0.x
y2=V2.y-V0.y
z2=V2.z-V0.z

// The normal by cross product
nx=y1*z2-z1*y2
ny=x1*z2-z1*x2
nz=x1*y2-y1*x2

// Normalize it
r=sqrt(nx*nx+ny*ny+nz*nz)
nx/=r;
ny/=r;
nz/=r;

// Dot product with plane A, B, C
r=nx*A+ny*B+nz*C // tried also adding D

then check CCW or CW based on 'r'

this have already done but seems not to work... is it the right procedure...?
if yes I suppose the DB got some more problems...

tnx

[R]ed

##### Share on other sites
Bug in calculating cross product, should be
ny=z1*x2-x1*z2

Also there is no need to normalise it as all you care about is the sign of r, not it's magnitude.

If r > 0 the winding is CCW otherwise it's CW.

##### Share on other sites
Tnx Luke...u r right...
however... seems not to work....
I will check better... if u confirm me this is the right procedure...
not sure then about some equations in the DB, and if it's sure this is the right procedure, I will ask DB creators...

tnx again

[R]ed

##### Share on other sites
Assuming that the plane equation is
(A,B,C) = plane normal
D = distance from origin
then yep the procedure looks correct to me.

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 21
• 10
• 33
• ### Forum Statistics

• Total Topics
633438
• Total Posts
3011882
• ### Who's Online (See full list)

There are no registered users currently online

×