diff options
Diffstat (limited to 'src/Frustum.cpp')
-rw-r--r-- | src/Frustum.cpp | 114 |
1 files changed, 57 insertions, 57 deletions
diff --git a/src/Frustum.cpp b/src/Frustum.cpp index e928e6b..ef60eb0 100644 --- a/src/Frustum.cpp +++ b/src/Frustum.cpp @@ -1,63 +1,63 @@ #include "Frustum.hpp" -#include <glm/gtc/type_ptr.hpp> - -void Frustum::NormalizePlane(FrustumSide side) { - float magnitude = (float) sqrt(frustum[side][A] * frustum[side][A] + frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C]); - - frustum[side][A] /= magnitude; - frustum[side][B] /= magnitude; - frustum[side][C] /= magnitude; - frustum[side][D] /= magnitude; +Frustum::Frustum(const glm::mat4 &vpMat) { + planes[RIGHT] = glm::vec4( + vpMat[0][3] - vpMat[0][0], + vpMat[1][3] - vpMat[1][0], + vpMat[2][3] - vpMat[2][0], + vpMat[3][3] - vpMat[3][0]); + + planes[LEFT] = glm::vec4( + vpMat[0][3] + vpMat[0][0], + vpMat[1][3] + vpMat[1][0], + vpMat[2][3] + vpMat[2][0], + vpMat[3][3] + vpMat[3][0]); + + planes[BOTTOM] = glm::vec4( + vpMat[0][3] + vpMat[0][1], + vpMat[1][3] + vpMat[1][1], + vpMat[2][3] + vpMat[2][1], + vpMat[3][3] + vpMat[3][1]); + + planes[TOP] = glm::vec4( + vpMat[0][3] - vpMat[0][1], + vpMat[1][3] - vpMat[1][1], + vpMat[2][3] - vpMat[2][1], + vpMat[3][3] - vpMat[3][1]); + + planes[FAR] = glm::vec4( + vpMat[0][3] - vpMat[0][2], + vpMat[1][3] - vpMat[1][2], + vpMat[2][3] - vpMat[2][2], + vpMat[3][3] - vpMat[3][2]); + + planes[NEAR] = glm::vec4( + vpMat[0][3] + vpMat[0][2], + vpMat[1][3] + vpMat[1][2], + vpMat[2][3] + vpMat[2][2], + vpMat[3][3] + vpMat[3][2]); + + for (auto &plane : planes) { + float magnitude = sqrt(plane.x * plane.x + plane.y * plane.y + plane.z * plane.z); + plane.x /= magnitude; + plane.y /= magnitude; + plane.z /= magnitude; + plane.w /= magnitude; + } } -void Frustum::UpdateFrustum(const glm::mat4& vpmat) { - vp = vpmat; - float *clip = glm::value_ptr(vp); - - frustum[RIGHT][A] = clip[3] - clip[0]; - frustum[RIGHT][B] = clip[7] - clip[4]; - frustum[RIGHT][C] = clip[11] - clip[8]; - frustum[RIGHT][D] = clip[15] - clip[12]; - NormalizePlane(RIGHT); - - - frustum[LEFT][A] = clip[3] + clip[0]; - frustum[LEFT][B] = clip[7] + clip[4]; - frustum[LEFT][C] = clip[11] + clip[8]; - frustum[LEFT][D] = clip[15] + clip[12]; - NormalizePlane(LEFT); - - frustum[BOTTOM][A] = clip[3] + clip[1]; - frustum[BOTTOM][B] = clip[7] + clip[5]; - frustum[BOTTOM][C] = clip[11] + clip[9]; - frustum[BOTTOM][D] = clip[15] + clip[13]; - NormalizePlane(BOTTOM); - - frustum[TOP][A] = clip[3] - clip[1]; - frustum[TOP][B] = clip[7] - clip[5]; - frustum[TOP][C] = clip[11] - clip[9]; - frustum[TOP][D] = clip[15] - clip[13]; - NormalizePlane(TOP); - - frustum[BACK][A] = clip[3] - clip[2]; - frustum[BACK][B] = clip[7] - clip[6]; - frustum[BACK][C] = clip[11] - clip[10]; - frustum[BACK][D] = clip[15] - clip[14]; - NormalizePlane(BACK); - - frustum[FRONT][A] = clip[3] + clip[2]; - frustum[FRONT][B] = clip[7] + clip[6]; - frustum[FRONT][C] = clip[11] + clip[10]; - frustum[FRONT][D] = clip[15] + clip[14]; - NormalizePlane(FRONT); +bool Frustum::TestPoint(const glm::vec3 &pos) { + for (const auto &plane : planes) { + if (GetDistanceToPoint(plane, pos) < 0) + return false; + } + return true; } -bool Frustum::TestPoint(VectorF point) { - for (int i = 0; i < 6; i++) { - if (frustum[i][A] * point.x + frustum[i][B] * point.y + frustum[i][C] * point.z + frustum[i][D] <= 0) { - return false; - } - } - return true; -}
\ No newline at end of file +bool Frustum::TestSphere(const glm::vec3 &pos, float radius) { + for (const auto &plane : planes) { + if (GetDistanceToPoint(plane, pos) < -radius) + return false; + } + return true; +} |