I'm trying to get view frustrum culling working. I found a neat, cheap way in an article to test if an AABB intersects the view frustrum: https://fgiesen.wordpress.com/2010/10/17/view-frustum-culling/ method5
I don't particullarly care if a model is partially inside the view frustrum (not yet anyway - maybe its more important later on?) so it just tests if its in or not.
Also I keep the AABB in object space and use world-view-projection matrix to retrieve the planes.
The issue I have is that it never reports that the AAB isn't in the frustrum - no matter how I orient myself, the AABB is always inside or partially inside. Are there any obvious bugs?
float signum(float val)
{
if (val > 0.0f) return 1.0f;
if (val < 0.0f) return -1.0f;
return 0.0f;
}
FrustrumPlanes GetFrustrumPlanes(const Mat4& WVPMatrix)
{
FrustrumPlanes ret;
ret[0] = WVPMatrix[3] + WVPMatrix[0];
ret[1] = WVPMatrix[3] - WVPMatrix[0];
ret[2] = WVPMatrix[3] - WVPMatrix[1];
ret[3] = WVPMatrix[3] + WVPMatrix[1];
ret[4] = WVPMatrix[2];
ret[5] = WVPMatrix[3] - WVPMatrix[2];
for (Plane& plane : ret)
plane = glm::normalize(plane);
return ret;
}
bool DoesAABBIntersectViewFrustrum(const Vec3& aabbCenter, const Vec3& aabbExtent, const Mat4& WVPMatrix)
{
FrustrumPlanes planes = GetFrustrumPlanes(WVPMatrix);
for (const Plane& plane : planes)
{
Vec3 signFlip(signum(plane.x), signum(plane.y), signum(plane.z));
if (glm::dot(aabbCenter + aabbExtent * signFlip, Vec3(plane)) > -plane.w)
return true;
}
return false;
}