X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fprimitive.hpp;h=72da84749140ca6f2d6b2c71a069c3fd9c031199;hb=ab0ba4313c473378b4516e3702d524dc1d1fc5d4;hp=7da1096b38ddd528546748d232289a7e30fefa3a;hpb=ee920127d653b8a3cfbee1efefde909ffa177662;p=blank.git diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index 7da1096..72da847 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,31 @@ struct AABB { } }; +std::ostream &operator <<(std::ostream &, const AABB &); + +// TODO: this should really use setters/getters for dir and inv_dir so +// manipulating code doesn't "forget" to call Update() struct Ray { glm::vec3 orig; glm::vec3 dir; + + glm::vec3 inv_dir; + + void Update() noexcept { + inv_dir = 1.0f / dir; + } }; +std::ostream &operator <<(std::ostream &, const Ray &); + +/// axis aligned boolean ray/box intersection test +/// if true, dist constains distance from ray's origin to intersection point +bool Intersection( + const Ray &, + const AABB &, + float &dist) noexcept; + +/// detailed oriented ray/box intersection test bool Intersection( const Ray &, const AABB &, @@ -40,6 +61,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 +71,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; }