X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fprimitive.hpp;h=da4c37f1e03296d254d55f051d5e199f94132a9c;hb=6513b55584093a86ce1e369e054263dd75c295c8;hp=7da1096b38ddd528546748d232289a7e30fefa3a;hpb=ee920127d653b8a3cfbee1efefde909ffa177662;p=blank.git diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index 7da1096..da4c37f 100644 --- a/src/geometry/primitive.hpp +++ b/src/geometry/primitive.hpp @@ -2,6 +2,7 @@ #define BLANK_GEOMETRY_PRIMITIVE_HPP_ #include +#include #include @@ -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 &, @@ -40,6 +45,8 @@ bool Intersection( float *dist = nullptr, glm::vec3 *normal = nullptr) noexcept; +/// matrices may translate and rotate, but must not scale/shear/etc +/// (basically the first three columns must have unit length) bool Intersection( const AABB &a_box, const glm::mat4 &a_m, @@ -48,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; }