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

View File

@ -1,5 +1,6 @@
#include "zeus/CFrustum.hpp"
#include <algorithm>
#include <cmath>
#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(), [&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 {
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