X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=blobdiff_plain;f=src%2Fmath%2Fgeometry.hpp;h=b4a9f50bb43c86c283691a65736f90981b62368a;hp=8f062841fe8adf64b05f6c8a0668f3e8a07adf2f;hb=3989da924c4e33c52f500aead5ae62bb40294781;hpb=41ee8b5d0229752576a55e102d8510ea1dbc909b diff --git a/src/math/geometry.hpp b/src/math/geometry.hpp index 8f06284..b4a9f50 100644 --- a/src/math/geometry.hpp +++ b/src/math/geometry.hpp @@ -40,6 +40,7 @@ bool Intersect( glm::dvec3 &normal, double &depth) noexcept; + class Ray { public: @@ -77,6 +78,32 @@ bool Intersect( glm::dvec3 &normal, double &dist) noexcept; + +struct Sphere { + + glm::dvec3 origin; + double radius; + +}; + +/// matrix may scale, but only uniformly +inline Sphere operator *(const glm::dmat4 &m, const Sphere &s) noexcept { + glm::dvec4 o(m * glm::dvec4(s.origin, 1.0)); + glm::dvec4 p(m * glm::dvec4(s.origin + glm::dvec3(s.radius, 0.0, 0.0), 1.0)); + return Sphere{glm::dvec3(o) / o.w, glm::length((glm::dvec3(p) / p.w) - (glm::dvec3(o) / o.w))}; +} + +inline std::ostream &operator <<(std::ostream &out, const Sphere &s) { + return out << "Sphere(" << s.origin << ", " << s.radius << ")"; +} + +/// oriented ray/sphere intersection test +bool Intersect( + const Ray &, + const Sphere &, + glm::dvec3 &normal, + double &dist) noexcept; + } }