X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fgeometry.cpp;h=1e84fbb9068d1749fbe5a3edcc81519aa7718e87;hb=6513b55584093a86ce1e369e054263dd75c295c8;hp=7798d33e949362ede44727f75402832e87a286ae;hpb=69a4ba4fa3e4694887087ab6dddc784a593d2b03;p=blank.git diff --git a/src/geometry/geometry.cpp b/src/geometry/geometry.cpp index 7798d33..1e84fbb 100644 --- a/src/geometry/geometry.cpp +++ b/src/geometry/geometry.cpp @@ -4,6 +4,8 @@ #include "rotation.hpp" #include +#include +#include #include #include #include @@ -39,6 +41,14 @@ glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept { return glm::mat3(1.0f) + vx + (pow2(vx) * f); } +std::ostream &operator <<(std::ostream &out, const AABB &box) { + return out << "AABB(" << box.min << ", " << box.max << ')'; +} + +std::ostream &operator <<(std::ostream &out, const Ray &ray) { + return out << "Ray(" << ray.orig << ", " << ray.dir << ')'; +} + bool Intersection( const Ray &ray, const AABB &aabb, @@ -187,6 +197,21 @@ bool Intersection( } +std::ostream &operator <<(std::ostream &out, const Plane &plane) { + return out << "Plane(" << plane.normal << ", " << plane.dist << ')'; +} + +std::ostream &operator <<(std::ostream &out, const Frustum &frustum) { + return out << "Frustum(" << std::endl + << "\tleft: " << frustum.plane[0] << std::endl + << "\tright: " << frustum.plane[1] << std::endl + << "\tbottom: " << frustum.plane[2] << std::endl + << "\ttop: " << frustum.plane[3] << std::endl + << "\tnear: " << frustum.plane[4] << std::endl + << "\tfar: " << frustum.plane[5] << std::endl + << ')'; +} + bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept { // transform corners into clip space glm::vec4 corners[8] = { @@ -223,4 +248,18 @@ bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept { return false; } +bool CullTest(const AABB &box, const Frustum &frustum) noexcept { + for (const Plane &plane : frustum.plane) { + const glm::vec3 np( + ((plane.normal.x > 0.0f) ? box.max.x : box.min.x), + ((plane.normal.y > 0.0f) ? box.max.y : box.min.y), + ((plane.normal.z > 0.0f) ? box.max.z : box.min.z) + ); + const float dp = dot(plane.normal, np); + // cull if nearest point is on the "outside" side of the plane + if (dp < -plane.dist) return true; + } + return false; +} + }