OpenGL face ordering

Hello all,

I’m writing a suite of export scripts that will take art that I create in Blender and get it into a format that my engine can read. Problems pop up however when it comes to vertex index ordering (some people call it vertex winding). I must say this first before I go on; my engine only works with lists of triangles but I do convert all polygons in my mesh to triangles before exporting with CTRL+T. The problem is it seems that about half the triangles are in the proper order.

After doing some googling I found that if edges that are shared are ordered oppositely then that should guarantee that they are all ordered in the same [anti]clockwise direction. In the code at the end of the post if I detect that they are ordered in the same direction (this detection algorithm is suspect) then I swap the first and third vertex index which effectively switches the ordering around.

Even though I’ve implemented code that forces this property from the meshes that I read in (freshly exported from Blender) only half the triangles are showing up.

Does anyone have expierence with the perks of OpenGL to lend a hand?

Here’s the code that I’m currently using:


void sortVertices(size_t *indices, size_t numFaces)
{
	//The ordering for shared edges must be opposite
	for(int i=1;i<numFaces;i++)
	{
		size_t first = 3*indices[3*i];
		size_t second = 3*indices[3*i+1];
		size_t third = 3*indices[3*i + 2];

		//Now look for the same edge, only processed
		for(int j=0;j<i;j++)
		{
			if(indices[3*j] == first && indices[3*j+1] == second)
			{
				cout << "Found an instance where v1 and v2 were specified in the same order" << endl;
				SWAP(indices[3*j], indices[3*j+2]);
			}
			else if(indices[3*j+1] == second && indices[3*j+2] == third)
			{
				cout << "Found an instance where v2 and v3 were specified in the same order" << endl;
				SWAP(indices[3*j], indices[3*j+2]);
			}
			else if(indices[3*j] == first && indices[3*j+2] == third)
			{
				cout << "Found an instance where v1 and v3 were specified in the same order" << endl;
				SWAP(indices[3*j], indices[3*j+2]);
			}
			//else
			
		}
		cout << "Ordered face #" << i << endl;
	}
}