CFrustum: Use std::array where applicable

Makes the array stronger-typed. We can also use it to convert a few
loops into algorithms.
This commit is contained in:
Lioncash 2019-09-03 14:17:30 -04:00
parent ef23d36916
commit 0e516b9f0b
2 changed files with 30 additions and 34 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <array>
#include "zeus/CPlane.hpp" #include "zeus/CPlane.hpp"
namespace zeus { namespace zeus {
@ -9,7 +10,7 @@ class CProjection;
class CSphere; class CSphere;
class CFrustum { class CFrustum {
CPlane planes[6]; std::array<CPlane, 6> planes;
bool valid = false; bool valid = false;
public: public:

View File

@ -1,5 +1,6 @@
#include "zeus/CFrustum.hpp" #include "zeus/CFrustum.hpp"
#include <algorithm>
#include <cmath> #include <cmath>
#include "zeus/CAABox.hpp" #include "zeus/CAABox.hpp"
@ -9,8 +10,8 @@
namespace zeus { namespace zeus {
void CFrustum::updatePlanes(const CMatrix4f& viewMtx, const CMatrix4f& projection) { void CFrustum::updatePlanes(const CMatrix4f& viewMtx, const CMatrix4f& projection) {
CMatrix4f mvp = projection * viewMtx; const CMatrix4f mvp = projection * viewMtx;
CMatrix4f mvp_rm = mvp.transposed(); const CMatrix4f mvp_rm = mvp.transposed();
/* Left */ /* Left */
planes[0].mSimd = mvp_rm.m[3].mSimd + mvp_rm.m[0].mSimd; 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) { void CFrustum::updatePlanes(const CTransform& viewPointMtx, const CProjection& projection) {
zeus::CMatrix3f tmp(viewPointMtx.basis[0], viewPointMtx.basis[2], -viewPointMtx.basis[1]); const CMatrix3f tmp(viewPointMtx.basis[0], viewPointMtx.basis[2], -viewPointMtx.basis[1]);
zeus::CTransform viewBasis = zeus::CTransform(tmp.transposed()); const CTransform viewBasis = CTransform(tmp.transposed());
zeus::CTransform viewMtx = viewBasis * zeus::CTransform::Translate(-viewPointMtx.origin); const CTransform viewMtx = viewBasis * CTransform::Translate(-viewPointMtx.origin);
updatePlanes(viewMtx.toMatrix4f(), projection.getCachedMatrix()); updatePlanes(viewMtx.toMatrix4f(), projection.getCachedMatrix());
} }
bool CFrustum::aabbFrustumTest(const CAABox& aabb) const { bool CFrustum::aabbFrustumTest(const CAABox& aabb) const {
if (!valid) if (!valid) {
return true; 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(), [&center, &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 { bool CFrustum::sphereFrustumTest(const CSphere& sphere) const {
if (!valid) if (!valid) {
return true; 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 { bool CFrustum::pointFrustumTest(const CVector3f& point) const {
if (!valid) if (!valid) {
return true; 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 } // namespace zeus