I need to sort my polygons and draw them back to front because there will be cases where alpha transparent polygons (like windows in a house) will overlap each other in my software.
I have tried to take the nearest point of each polygon and calculate the distance from the camera using my calcdist function, but there are some cases where that doesn’t work such as when close up and two polygons share the same nearest point like in the case where two walls meet at a corner yet one is clearly behind the other in reference from camera position
I am not sure what to do now as I don’t think calculating the polygon distance from the camera is going to work. So my question is, is there a standard algorithm to determine which polygon from my code below (ppoints1 or ppoints2) is furthest and which one is nearest based on the current camera position and direction?
Thanks,
Greg
struct Point
{
float x;
float y;
float z;
};
float calcdist(Point v1,Point v2)
{
Point v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
GLdouble m[16];
float camera_pos[3];
float camera_dir[3];
glGetDoublev(GL_MODELVIEW_MATRIX, m);
camera_pos[0] = -(m[0] * m[12] + m[1] * m[13] + m[2] * m[14]);
camera_pos[1] = -(m[4] * m[12] + m[5] * m[13] + m[6] * m[14]);
camera_pos[2] = -(m[8] * m[12] + m[9] * m[13] + m[10] * m[14]);
camera_dir[0] = m[8];
camera_dir[1] = m[9];
camera_dir[2] = -m[10];
Point ppoints1[] = {{-10,10,40.0f}, {-10,10,0.0f}, {10,0.0f,0.0}, {-10,0.0f,40.0f}};
Point ppoints2[] = {{-10,10,0}, {10,10,0}, {10,0,0}, {-10,0,0}};