2
\$\begingroup\$

I'm in the process of constructing a custom 3D triangle mesh. I found the vertices of the triangle in the 3D space and it's face normal. How do I find the acyclic order of vertices?

If I draw the vertices in the wrong order, I can't see some of the faces which should be visible when back face culling is enabled.

\$\endgroup\$
6
\$\begingroup\$

For a triangle with points p0, p1, and p2, and normal n, you’ll need to compare the vectors cross(p1 - p0, p2 - p0) and n. They should either point in the same direction, or in the opposite direction, for all triangles in your mesh.

Suppose your convention is that the vectors must point in the same direction.

The algorithm is simple. For each triangle, compute the following value:

dot(n, cross(p1 - p0, p2 - p0))

If the value is negative, swap p1 and p2.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Perfect and clean solution!! Thank you so much. Understood that during the dot product of opposite normals gives negative scalar :) \$\endgroup\$ – cegprakash Mar 4 '14 at 8:42
  • \$\begingroup\$ Hi, I'm getting NaN during my cross product. Any idea on how to fix this? \$\endgroup\$ – cegprakash Mar 4 '14 at 10:18
  • \$\begingroup\$ Given that a cross product is only additions and multiplications, and assuming the implementation is correct, if a cross product results in NaN values it means that one of the initial vectors had a NaN component. Maybe you have uninitialised data in your triangle lists? \$\endgroup\$ – sam hocevar Mar 4 '14 at 16:54
  • \$\begingroup\$ fixed.. my mistake :P \$\endgroup\$ – cegprakash Mar 4 '14 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.