Automatically fixing vertex order for backface culling

Started by
1 comment, last by intransigent-seal 19 years, 6 months ago
I have a set of models with pretty much random vertex order, and I want to write a tool to go through them and fix them all so that the vertex order is all clockwise (or counter-clockwise) so that I can turn on backface culling. But to do that I'll obviously need some algorithm for it. I've thought of one that I think should work, but I'm hoping there might be a better way (or maybe there's a standard algorithm for it that I don't know about). The way I've thought of so far is this: - Go through each triangle in the model. Calculate a face normal, and a triangle mid-point, and from those produce a ray from the centre of the triangle, and perpendicular to it. - Do an intersection test of that ray against every other triangle in the model. Count how many other triangles it intersects. - If the number is odd, then flip the vertex order. If it's 0 or even, then don't flip the vertex order. (The last point may be the wrong way round; ie, it may be flip if the count is even). However, as you can see, that's a fairly brute-force way of doing it. It would still be fast enough, since the models are low poly and there aren't too many of them, but even so I'd like to know if there's a better algorithm I could use. John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Advertisement
Ok here comes the pseudo code for something i wrote a while back...

what you need is the origin of the triangle / polygon so sum the vertecies and div by count

if (vertex count < 3)  return;vector3 va = firstVertex;vector3 vb = secondVertex;vector3 vz = lastVertex;    // of the triangle/polygonvector3 ma = origin - va;   // origin is the center/middle vecvector3 mb = origin - va;vector3 mz = origin - va;vector3 crossAB = CrossProduct(ma, mb).Normalize();vector3 crossAZ = CrossProduct(ma, mz).Normalize();vector3 x = origin - crossAB;vector3 z = origin - crossAZ;if (Length(x) > Length(z)){  // reorder them to CCW, were CW (I think, might be other way)}


I hope thats correct, sorry for the short var names and little comments :D a bit short on time here


Hope that helped, if wrong(not understandable etc) pm me or reply, sorry gotta run
Ok, I'll try that. Thanks :-)

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement