I'm working on a 3d engine and implementing a triangle sorting system by distance between the triangle's normal + the triangle's position and <0,0,0>. But the problem is that some triangles are counter-clockwise and others clockwise, so I made two normals that are on opposite direction. But now I have to determine which normal to use.
Here is the necessary code:
// executes this code for every triangle that has to be drawn
for (int i = 0; i < tridrawcount; i++)
{
triangle &listtri = tridrawlist[i];
vec3 pnorm;
vec3 norm;
vec3 norm2;
vec3 line;
vec3 line2;
vec3 apos;
vec3 ppos;
vec3 ppos2;
line = listtri.vpos2 - listtri.vpos;
line2 = listtri.vpos3 - listtri.vpos;
norm = line.cross(line2);
norm2 = line2.cross(line);
apos = (listtri.vpos + listtri.vpos2 + listtri.vpos3)/3; // basically the center of the triangle
float length = norm.mag();
float length2 = norm2.mag();
norm = norm / length;
norm2 = norm2 / length2;
ppos = norm + apos;
ppos2 = norm2 + apos;
float dist = sqrt(pow(0 - ppos.x,2) + pow(0 - ppos.y, 2) + pow(0 - ppos.z, 2));
float dist2 = sqrt(pow(0 - ppos2.x, 2) + pow(0 - ppos2.y, 2) + pow(0 - ppos2.z, 2));
}