I've got some issues trying to see if my AABBs are inside the view frustum or not using AABB-plane intersection. No matter what, atleast some plane always returns 'back' during testing of a AABB in clear view.
Here's how I construct a plane and test against AABB:
Plane::Plane(const Vec3& point1, const Vec3& point2, const Vec3& point3) : mNormal(glm::normalize(glm::cross(point2 - point1, point3 - point2))), mDistance(glm::dot(mNormal, point1))
{
}
Plane::PlaneIntersection Plane::Intersection(const AABB& aabb) const
{
const float radius = aabb.mAABBExtent.x * glm::abs(mNormal.x) +
aabb.mAABBExtent.y * glm::abs(mNormal.y) +
aabb.mAABBExtent.z * glm::abs(mNormal.z);
const float distance = glm::dot(mNormal, aabb.mAABBCenter) - mDistance;
if (glm::abs(distance) <= radius)
return PlaneIntersection::Intersect;
return distance > radius ? PlaneIntersection::Front : PlaneIntersection::Back;
}
Getting the six planes of a view frustrum, I first calculate the corners and then use three points to get each plane:
// std::array<Vec3, 8> - 8 corners of the view frustum
CameraFrustrum frusturm = CalculateCameraFrustrum(mRenderQueue.mCamera.mCameraViewProjectionMatrix);
// ntl, ntr, nbr
Plane nearP(Vec3(frusturm.at(1)), Vec3(frusturm.at(2)), Vec3(frusturm.at(3)));
// fbl, ftl, ftr
Plane farP(Vec3(frusturm.at(4)), Vec3(frusturm.at(5)), Vec3(frusturm.at(6)));
// nbl, fbl, ftl
Plane leftP(Vec3(frusturm.at(0)), Vec3(frusturm.at(4)), Vec3(frusturm.at(5)));
// ntr, ftr, fbr
Plane rightP(Vec3(frusturm.at(2)), Vec3(frusturm.at(6)), Vec3(frusturm.at(7)));
// ntl, ftl, ftr
Plane topP(Vec3(frusturm.at(1)), Vec3(frusturm.at(5)), Vec3(frusturm.at(6)));
// nbr, fbr, fbl
Plane bottomP(Vec3(frusturm.at(3)), Vec3(frusturm.at(7)), Vec3(frusturm.at(4)));
Then for each plane, use intersection() above. As I understand it, if even one of the plane intersection tests is returned 'Back', the AABB is not considered inside the frustrum.
Look at the alien, its AABB center about about Vec3(0, 1, -2) in the picture, camera standing at origo, looking down Z:
[attachment=27430:post-182913-0-44780300-1426029853.png]
Apparently the Far and Bottom planes says the alien is 'Back' , i.e not in the frustrum.
What am I doing wrong?