From 4d91563233bb68422a6ec3268a81d567504e5ddb Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Sat, 11 Jan 2025 20:23:09 +0100 Subject: [PATCH] Cleanup Frustum class --- openVulkanoCpp/Math/Frustum.hpp | 79 ++++++++++++++------------------- 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/openVulkanoCpp/Math/Frustum.hpp b/openVulkanoCpp/Math/Frustum.hpp index 16ad91e..a93781b 100644 --- a/openVulkanoCpp/Math/Frustum.hpp +++ b/openVulkanoCpp/Math/Frustum.hpp @@ -37,7 +37,7 @@ namespace OpenVulkano::Math {} // m = ProjectionMatrix * ViewMatrix - Frustum(Math::Matrix4f m) + Frustum(Matrix4f m) { m = Math::Utils::transpose(m); m_planes[LEFT] = m[3] + m[0]; @@ -48,21 +48,21 @@ namespace OpenVulkano::Math m_planes[FAR] = m[3] - m[2]; Math::Vector3f crosses[COMBINATIONS] = { - Math::Utils::cross(Math::Vector3f(m_planes[LEFT]), Math::Vector3f(m_planes[RIGHT])), - Math::Utils::cross(Math::Vector3f(m_planes[LEFT]), Math::Vector3f(m_planes[BOTTOM])), - Math::Utils::cross(Math::Vector3f(m_planes[LEFT]), Math::Vector3f(m_planes[TOP])), - Math::Utils::cross(Math::Vector3f(m_planes[LEFT]), Math::Vector3f(m_planes[NEAR])), - Math::Utils::cross(Math::Vector3f(m_planes[LEFT]), Math::Vector3f(m_planes[FAR])), - Math::Utils::cross(Math::Vector3f(m_planes[RIGHT]), Math::Vector3f(m_planes[BOTTOM])), - Math::Utils::cross(Math::Vector3f(m_planes[RIGHT]), Math::Vector3f(m_planes[TOP])), - Math::Utils::cross(Math::Vector3f(m_planes[RIGHT]), Math::Vector3f(m_planes[NEAR])), - Math::Utils::cross(Math::Vector3f(m_planes[RIGHT]), Math::Vector3f(m_planes[FAR])), - Math::Utils::cross(Math::Vector3f(m_planes[BOTTOM]), Math::Vector3f(m_planes[TOP])), - Math::Utils::cross(Math::Vector3f(m_planes[BOTTOM]), Math::Vector3f(m_planes[NEAR])), - Math::Utils::cross(Math::Vector3f(m_planes[BOTTOM]), Math::Vector3f(m_planes[FAR])), - Math::Utils::cross(Math::Vector3f(m_planes[TOP]), Math::Vector3f(m_planes[NEAR])), - Math::Utils::cross(Math::Vector3f(m_planes[TOP]), Math::Vector3f(m_planes[FAR])), - Math::Utils::cross(Math::Vector3f(m_planes[NEAR]), Math::Vector3f(m_planes[FAR])) + Math::Utils::cross(Vector3f(m_planes[LEFT]), Vector3f(m_planes[RIGHT])), + Math::Utils::cross(Vector3f(m_planes[LEFT]), Vector3f(m_planes[BOTTOM])), + Math::Utils::cross(Vector3f(m_planes[LEFT]), Vector3f(m_planes[TOP])), + Math::Utils::cross(Vector3f(m_planes[LEFT]), Vector3f(m_planes[NEAR])), + Math::Utils::cross(Vector3f(m_planes[LEFT]), Vector3f(m_planes[FAR])), + Math::Utils::cross(Vector3f(m_planes[RIGHT]), Vector3f(m_planes[BOTTOM])), + Math::Utils::cross(Vector3f(m_planes[RIGHT]), Vector3f(m_planes[TOP])), + Math::Utils::cross(Vector3f(m_planes[RIGHT]), Vector3f(m_planes[NEAR])), + Math::Utils::cross(Vector3f(m_planes[RIGHT]), Vector3f(m_planes[FAR])), + Math::Utils::cross(Vector3f(m_planes[BOTTOM]), Vector3f(m_planes[TOP])), + Math::Utils::cross(Vector3f(m_planes[BOTTOM]), Vector3f(m_planes[NEAR])), + Math::Utils::cross(Vector3f(m_planes[BOTTOM]), Vector3f(m_planes[FAR])), + Math::Utils::cross(Vector3f(m_planes[TOP]), Vector3f(m_planes[NEAR])), + Math::Utils::cross(Vector3f(m_planes[TOP]), Vector3f(m_planes[FAR])), + Math::Utils::cross(Vector3f(m_planes[NEAR]), Vector3f(m_planes[FAR])) }; m_points[0] = Intersection(crosses); @@ -73,7 +73,6 @@ namespace OpenVulkano::Math m_points[5] = Intersection(crosses); m_points[6] = Intersection(crosses); m_points[7] = Intersection(crosses); - } // http://iquilezles.org/www/articles/frustumcorrect/frustumcorrect.htm @@ -96,35 +95,25 @@ namespace OpenVulkano::Math } // check frustum outside/inside box - int out; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].x > maxp.x) ? 1 : 0); - if (out == 8) return false; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].x < minp.x) ? 1 : 0); - if (out == 8) return false; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].y > maxp.y) ? 1 : 0); - if (out == 8) return false; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].y < minp.y) ? 1 : 0); - if (out == 8) return false; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].z > maxp.z) ? 1 : 0); - if (out == 8) return false; - out = 0; - for (int i = 0; i < 8; i++) out += ((m_points[i].z < minp.z) ? 1 : 0); - if (out == 8) return false; + for (int j = 0, out = 0; j < 3; j++) + { + for (int i = 0; i < 8; i++) out += m_points[i][j] > maxp[j]; + if (out == 8) return false; + out = 0; + for (int i = 0; i < 8; i++) out += m_points[i][j] < minp[j]; + if (out == 8) return false; + out = 0; + } return true; } - [[nodiscard]] bool IsBoxVisible(const Math::Vector4f& minp, const Math::Vector4f& maxp) const + [[nodiscard]] bool IsBoxVisible(const Vector4f& minp, const Vector4f& maxp) const { - return IsBoxVisible(Math::Vector3f(minp), Math::Vector3f(maxp)); + return IsBoxVisible(Vector3f(minp), Vector3f(maxp)); } - [[nodiscard]] bool IsBoxVisible(const Range& range) const + [[nodiscard]] bool IsBoxVisible(const Range& range) const { return IsBoxVisible(range.min, range.max); } @@ -140,15 +129,15 @@ namespace OpenVulkano::Math }; template - Math::Vector3f Intersection(const Math::Vector3f* crosses) const + Vector3f Intersection(const Vector3f* crosses) const { - float D = Math::Utils::dot(Math::Vector3f(m_planes[a]), crosses[ij2k::k]); - Math::Vector3f res = Math::Matrix3f(crosses[ij2k::k], -crosses[ij2k::k], crosses[ij2k::k]) * - Math::Vector3f(m_planes[a].w, m_planes[b].w, m_planes[c].w); + float D = Math::Utils::dot(Vector3f(m_planes[a]), crosses[ij2k::k]); + Vector3f res = Matrix3f(crosses[ij2k::k], -crosses[ij2k::k], crosses[ij2k::k]) * + Vector3f(m_planes[a].w, m_planes[b].w, m_planes[c].w); return res * (-1.0f / D); } - Math::Vector4f m_planes[COUNT]; - Math::Vector3f m_points[8]; + Vector4f m_planes[COUNT]; + Vector3f m_points[8]; }; }