Ok I tried this:
(code may be inaccurate; I'm at school, and can't remember exactly what I did at home)
Code:
int comparedist(const void *a, const void *b)
{
objectdata *od1 (objectdata *) a;
objectdata *od2 (objectdata *) b;
GLfloat diff = od1->sortdist - od2->sortdist;
if (diff > 0)
return 1;
if (diff < 0)
return -1;
return 0;
}
void zsort()
{
for(int i=0; i<num_objects; i++)
{
vector3d dist = objectP[i].pos-pCam.m_vPosition;
float objectP[i].sortdist = (dist.x*pCam.m_vView.x)+(dist.y*pCam.m_vView.y)+(dist.z*pCam.m_vView.z);
}
qsort(objectP, num_objects, sizeof(objectP[0]), comparedist);
}
It works better than before, but it's really buggy. The biggest issue is that it only works in one direction (ie, if i'm looking down positive X, its sorted; if I look the other way, it dosen't seem to sort correctly). And yes, I am calling zsort() before the render loop.