diff --git a/include/zeus/CFrustum.hpp b/include/zeus/CFrustum.hpp index 54d0d29..6da9695 100644 --- a/include/zeus/CFrustum.hpp +++ b/include/zeus/CFrustum.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include "zeus/CPlane.hpp" namespace zeus { @@ -9,7 +10,7 @@ class CProjection; class CSphere; class CFrustum { - CPlane planes[6]; + std::array planes; bool valid = false; public: diff --git a/src/CFrustum.cpp b/src/CFrustum.cpp index c65347e..4a138be 100644 --- a/src/CFrustum.cpp +++ b/src/CFrustum.cpp @@ -1,5 +1,6 @@ #include "zeus/CFrustum.hpp" +#include #include #include "zeus/CAABox.hpp" @@ -9,8 +10,8 @@ namespace zeus { void CFrustum::updatePlanes(const CMatrix4f& viewMtx, const CMatrix4f& projection) { - CMatrix4f mvp = projection * viewMtx; - CMatrix4f mvp_rm = mvp.transposed(); + const CMatrix4f mvp = projection * viewMtx; + const CMatrix4f mvp_rm = mvp.transposed(); /* Left */ planes[0].mSimd = mvp_rm.m[3].mSimd + mvp_rm.m[0].mSimd; @@ -41,54 +42,48 @@ void CFrustum::updatePlanes(const CMatrix4f& viewMtx, const CMatrix4f& projectio } void CFrustum::updatePlanes(const CTransform& viewPointMtx, const CProjection& projection) { - zeus::CMatrix3f tmp(viewPointMtx.basis[0], viewPointMtx.basis[2], -viewPointMtx.basis[1]); - zeus::CTransform viewBasis = zeus::CTransform(tmp.transposed()); - zeus::CTransform viewMtx = viewBasis * zeus::CTransform::Translate(-viewPointMtx.origin); + const CMatrix3f tmp(viewPointMtx.basis[0], viewPointMtx.basis[2], -viewPointMtx.basis[1]); + const CTransform viewBasis = CTransform(tmp.transposed()); + const CTransform viewMtx = viewBasis * CTransform::Translate(-viewPointMtx.origin); updatePlanes(viewMtx.toMatrix4f(), projection.getCachedMatrix()); } bool CFrustum::aabbFrustumTest(const CAABox& aabb) const { - if (!valid) + if (!valid) { return true; - - CVector3f center = aabb.center(); - CVector3f extents = aabb.extents(); - - for (uint32_t i = 0; i < 6; ++i) { - const CPlane& plane = planes[i]; - - float m = plane.normal().dot(center) + plane.d(); - float n = extents.dot({std::fabs(plane.x()), std::fabs(plane.y()), std::fabs(plane.z())}); - - if (m + n < 0.f) - return false; } - return true; + + const CVector3f center = aabb.center(); + const CVector3f extents = aabb.extents(); + + return std::none_of(planes.cbegin(), planes.cend(), [¢er, &extents](const CPlane& plane) { + const float m = plane.normal().dot(center) + plane.d(); + const float n = extents.dot({std::fabs(plane.x()), std::fabs(plane.y()), std::fabs(plane.z())}); + return m + n < 0.f; + }); } bool CFrustum::sphereFrustumTest(const CSphere& sphere) const { - if (!valid) + if (!valid) { return true; - - for (uint32_t i = 0; i < 6; ++i) { - float dadot = planes[i].normal().dot(sphere.position); - if ((dadot + planes[i].d() + sphere.radius) < 0.f) - return false; } - return true; + + return std::none_of(planes.cbegin(), planes.cend(), [&sphere](const CPlane& plane) { + const float dadot = plane.normal().dot(sphere.position); + return (dadot + plane.d() + sphere.radius) < 0.f; + }); } bool CFrustum::pointFrustumTest(const CVector3f& point) const { - if (!valid) + if (!valid) { return true; - - for (uint32_t i = 0; i < 6; ++i) { - float dadot = planes[i].normal().dot(point); - if ((dadot + planes[i].d()) < 0.f) - return false; } - return true; + + return std::none_of(planes.cbegin(), planes.cend(), [&point](const CPlane& plane) { + const float dadot = plane.normal().dot(point); + return (dadot + plane.d()) < 0.f; + }); } } // namespace zeus