X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fprimitive.hpp;h=b9f2187580f5b64e692451553a352c72006e7e42;hb=242b87a5fb412f9006e4b7debc1408cf7ac83000;hp=da4c37f1e03296d254d55f051d5e199f94132a9c;hpb=1e93bfb5089737f6b6d8fdd2f17260944fca44b2;p=blank.git diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp index da4c37f..b9f2187 100644 --- a/src/geometry/primitive.hpp +++ b/src/geometry/primitive.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace blank { @@ -31,13 +32,42 @@ 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; + } + + /// 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 length(cross(dir, orig - point)); + } + float DistanceSquared(const glm::vec3 &point) const noexcept { + return length2(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 &,