X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fprimitive.hpp;h=52a3617835245382b6e2014091116cc881fc3c87;hb=c2bf905f4b9863fe0f5c876ed00fe298cb95ab6b;hp=da4c37f1e03296d254d55f051d5e199f94132a9c;hpb=1e93bfb5089737f6b6d8fdd2f17260944fca44b2;p=blank.git diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index da4c37f..52a3617 100644 --- a/src/geometry/primitive.hpp +++ b/src/geometry/primitive.hpp @@ -1,9 +1,11 @@ #ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_ #define BLANK_GEOMETRY_PRIMITIVE_HPP_ +#include "../graphics/glm.hpp" + #include #include -#include +#include namespace blank { @@ -24,20 +26,49 @@ struct AABB { /// return distance between origin and farthest vertex float OriginRadius() const noexcept { - glm::vec3 high(glm::max(abs(min), abs(max))); - return length(high); + glm::vec3 high(glm::max(glm::abs(min), glm::abs(max))); + return glm::length(high); } }; 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; + } + + /// get shortest distance of this ray's line to given point + float Distance(const glm::vec3 &point) const noexcept { + // d = |(x2-x1)×(x1-x0)|/|x2-x1| + // where x0 is point, and x1 and x2 are points on the line + // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html + // x1 = orig + // x2-x1 = dir, which means |x2-x1| is 1.0 + return glm::length(glm::cross(dir, orig - point)); + } + float DistanceSquared(const glm::vec3 &point) const noexcept { + return glm::length2(glm::cross(dir, orig - point)); + } }; 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 &, @@ -71,11 +102,11 @@ struct Plane { Plane(const glm::vec3 &n, float d) : normal(n), dist(d) { } - Plane(const glm::vec4 &abcd) + explicit Plane(const glm::vec4 &abcd) : normal(abcd), dist(abcd.w) { } void Normalize() noexcept { - const float l = length(normal); + const float l = glm::length(normal); normal /= l; dist /= l; } @@ -99,14 +130,14 @@ struct Frustum { const Plane &Far() const noexcept { return plane[5]; } /// create frustum from transposed MVP - Frustum(const glm::mat4 &mat) + explicit 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] }, + Plane{ mat[3] + mat[0] }, + Plane{ mat[3] - mat[0] }, + Plane{ mat[3] + mat[1] }, + Plane{ mat[3] - mat[1] }, + Plane{ mat[3] + mat[2] }, + Plane{ mat[3] - mat[2] }, } { } void Normalize() noexcept {