Hey,
I am trying to do culling using Frustum Culling. However what seems to be happening is nothing seems to be getting rendered. I get no models rendered at all when I use frustum culling. I am not sure if I am understanding it right. However this is the code I have for frustum culling.
The header file
class FrustrumCulling {
public :
vector3 m_vector;
vector4 m_FOVTopPlane;
vector4 m_FOVBottomPlane;
vector4 m_FOVRightPlane;
vector4 m_FOVLeftPlane;
float g_FOVFrontClip;
float g_FOVBackClip;
matrix44 m_viewTransform;
public :
void SetupFOVClipPlanes(float angle, float aspect, float frontClip, float backClip);
bool MeshFOVCheck(vector3 bSpherePos, float bSphereRadius, vector3 *pViewPos);
protected :
void PlaneFromPoints(vector3 *p1, vector3 *p2, vector3 *p3, vector4 *plane);
};
The source file
void FrustrumCulling ::PlaneFromPoints(vector3 *p1, vector3 *p2, vector3 *p3, vector4 *plane)
{
vector3 v0, v1, v2;
v0.x = p2->x - p1->x;
v0.y = p2->y - p1->y;
v0.z = p2->z - p1->z;
v1.x = p3->x - p1->x;
v1.y = p3->y - p1->y;
v1.z = p3->z - p1->z;
v2 = CrossProduct(v1, v2);
v2.normalize();
plane->x = v2.x;
plane->y = v2.y;
plane->z = v2.z;
plane->w = -(v2.x * p1->x + v2.y * p1->y + v2.z * p1->z);
}
void FrustrumCulling ::SetupFOVClipPlanes(float angle, float aspect, float frontClip, float backClip)
{
vector3 p0, p1, p2;
// calculate left plane
p0.set(0, 0, 0);
p1.x = -backClip * ((float)tan(angle * 0.5) / aspect);
p1.y = -backClip * ((float)tan(angle * 0.5));
p1.z = backClip;
p2.x = p1.x;
p2.y = -p1.y;
p2.z = p1.z;
this->PlaneFromPoints(&p0, &p1, &p2, &this->m_FOVLeftPlane);
// calculate right plane
p0.set(0, 0, 0);
p1.x = backClip * ((float)tan(angle * 0.5) / aspect);
p1.y = backClip * ((float)tan(angle * 0.5));
p1.z = backClip;
p2.x = p1.x;
p2.y = -p1.y;
p2.z = p1.z;
this->PlaneFromPoints(&p0, &p1, &p2, &this->m_FOVRightPlane);
// calculate top plane
p0.set(0, 0, 0);
p1.x = -backClip * ((float)tan(angle * 0.5) / aspect);
p1.y = backClip * ((float)tan(angle * 0.5));
p1.z = backClip;
p2.x = -p1.x;
p2.y = p1.y;
p2.z = p1.z;
this->PlaneFromPoints(&p0, &p1, &p2, &this->m_FOVTopPlane);
// calculate bottom plane
p0.set(0, 0, 0);
p1.x = backClip * ((float)tan(angle * 0.5) / aspect);
p1.y = -backClip * ((float)tan(angle * 0.5));
p1.z = backClip;
p2.x = -p1.x;
p2.y = p1.y;
p2.z = p1.z;
this->PlaneFromPoints(&p0, &p1, &p2, &this->m_FOVBottomPlane);
}
bool FrustrumCulling ::MeshFOVCheck(vector3 bSpherePos, float bSphereRadius, vector3 *pViewPos)
{
float Dist;
// transform z into view space
pViewPos->z = this->m_viewTransform.col[0][2] * bSpherePos.x +
this->m_viewTransform.col[1][2] * bSpherePos.y +
this->m_viewTransform.col[2][2] * bSpherePos.z +
this->m_viewTransform.col[3][2];
// behind front clip plane ?
if( (pViewPos->z + bSphereRadius) < this->g_FOVFrontClip)
return false;
// behind back clip plane ?
if( (pViewPos->z - bSphereRadius) > this->g_FOVBackClip)
return false;
// transform x into view space
pViewPos->x = this->m_viewTransform.col[0][0] * bSpherePos.x +
this->m_viewTransform.col[1][0] * bSpherePos.y +
this->m_viewTransform.col[2][0] * bSpherePos.z +
this->m_viewTransform.col[3][0];
// test against left clip plane
Dist = (pViewPos->x * this->m_FOVLeftPlane.x) + (pViewPos->z + this->m_FOVLeftPlane.z);
if(Dist > bSphereRadius) return false;
// test against right clip plane
Dist = (pViewPos->x * this->m_FOVRightPlane.x) + (pViewPos->z + this->m_FOVRightPlane.z);
if(Dist > bSphereRadius) return false;
// transform y into view space
pViewPos->y = this->m_viewTransform.col[0][1] * bSpherePos.x +
this->m_viewTransform.col[1][1] * bSpherePos.y +
this->m_viewTransform.col[2][1] * bSpherePos.z +
this->m_viewTransform.col[3][1];
// test against top clip plane
Dist = (pViewPos->y * this->m_FOVTopPlane.y) + (pViewPos->z + this->m_FOVTopPlane.z);
if(Dist > bSphereRadius) return false;
// test against right clip plane
Dist = (pViewPos->x * this->m_FOVTopPlane.x) + (pViewPos->z + this->m_FOVTopPlane.z);
if(Dist > bSphereRadius) return false;
return true;
}
I tried to debug the code and check. It always seems to fail on left clip plane and return false. I dont know why though.
I am not sure if I am doing calculations correctly. Also this is how I am calling it.
FrustrumCullingClass.SetupFOVClipPlanes(45.0f, 800/600, 1.0f, 100.0f);
for(int i = 0; i < this->m_numModels; i++) {
if(FrustrumCullingClass.MeshFOVCheck(this->m_pos[i], this->m_radius, &CameraClass.GetCameraView())) {
glTranslatef(this->m_pos[i].x, this->m_pos[i].y, this->m_pos[i].z);
glCallList(this->m_list);
this->nEnemies++;
}
}
I hope someone can point me into the right direction.
Thank you
The more applications I write, more I find out how less I know