3
\$\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\$

1 Answer 1

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.

\$\endgroup\$
4
  • \$\begingroup\$ Perfect and clean solution!! Thank you so much. Understood that during the dot product of opposite normals gives negative scalar :) \$\endgroup\$ Commented Mar 4, 2014 at 8:42
  • \$\begingroup\$ Hi, I'm getting NaN during my cross product. Any idea on how to fix this? \$\endgroup\$ Commented Mar 4, 2014 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\$ Commented Mar 4, 2014 at 16:54
  • \$\begingroup\$ fixed.. my mistake :P \$\endgroup\$ Commented Mar 4, 2014 at 19:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.