]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/primitive.hpp
faster chunk culling test
[blank.git] / src / geometry / primitive.hpp
index cdeae86e5c68b21303b7c27c775d9ff657df8924..da4c37f1e03296d254d55f051d5e199f94132a9c 100644 (file)
@@ -2,6 +2,7 @@
 #define BLANK_GEOMETRY_PRIMITIVE_HPP_
 
 #include <algorithm>
+#include <iosfwd>
 #include <glm/glm.hpp>
 
 
@@ -28,11 +29,15 @@ struct AABB {
        }
 };
 
+std::ostream &operator <<(std::ostream &, const AABB &);
+
 struct Ray {
        glm::vec3 orig;
        glm::vec3 dir;
 };
 
+std::ostream &operator <<(std::ostream &, const Ray &);
+
 bool Intersection(
        const Ray &,
        const AABB &,
@@ -50,7 +55,72 @@ bool Intersection(
        float &depth,
        glm::vec3 &normal) noexcept;
 
-bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
+
+struct Plane {
+       glm::vec3 normal;
+       float dist;
+
+       float &A() noexcept { return normal.x; }
+       float &B() noexcept { return normal.y; }
+       float &C() noexcept { return normal.z; }
+       float &D() noexcept { return dist; }
+       float A() const noexcept { return normal.x; }
+       float B() const noexcept { return normal.y; }
+       float C() const noexcept { return normal.z; }
+       float D() const noexcept { return dist; }
+
+       Plane(const glm::vec3 &n, float d)
+       : normal(n), dist(d) { }
+       Plane(const glm::vec4 &abcd)
+       : normal(abcd), dist(abcd.w) { }
+
+       void Normalize() noexcept {
+               const float l = length(normal);
+               normal /= l;
+               dist /= l;
+       }
+};
+
+std::ostream &operator <<(std::ostream &, const Plane &);
+
+struct Frustum {
+       Plane plane[6];
+       Plane &Left() noexcept { return plane[0]; }
+       Plane &Right() noexcept { return plane[1]; }
+       Plane &Bottom() noexcept { return plane[2]; }
+       Plane &Top() noexcept { return plane[3]; }
+       Plane &Near() noexcept { return plane[4]; }
+       Plane &Far() noexcept { return plane[5]; }
+       const Plane &Left() const noexcept { return plane[0]; }
+       const Plane &Right() const noexcept { return plane[1]; }
+       const Plane &Bottom() const noexcept { return plane[2]; }
+       const Plane &Top() const noexcept { return plane[3]; }
+       const Plane &Near() const noexcept { return plane[4]; }
+       const Plane &Far() const noexcept { return plane[5]; }
+
+       /// create frustum from transposed MVP
+       Frustum(const glm::mat4 &mat)
+       : plane{
+               { mat[3] + mat[0] },
+               { mat[3] - mat[0] },
+               { mat[3] + mat[1] },
+               { mat[3] - mat[1] },
+               { mat[3] + mat[2] },
+               { mat[3] - mat[2] },
+       } { }
+
+       void Normalize() noexcept {
+               for (Plane &p : plane) {
+                       p.Normalize();
+               }
+       }
+};
+
+std::ostream &operator <<(std::ostream &, const Plane &);
+std::ostream &operator <<(std::ostream &, const Frustum &);
+
+bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
+bool CullTest(const AABB &box, const Frustum &) noexcept;
 
 }