X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel%2Fgeometry.cpp;h=a4ac5006d55a019544f122a035af5d98d21cf308;hb=0d580658b896dfec07466c31ae4847455724ee95;hp=e0b55826de35ed58bb05ca441a260a5877f45e86;hpb=4f93c2526b82132ee695949779775b5e7abc7d4d;p=blank.git diff --git a/src/model/geometry.cpp b/src/model/geometry.cpp index e0b5582..a4ac500 100644 --- a/src/model/geometry.cpp +++ b/src/model/geometry.cpp @@ -1,10 +1,41 @@ #include "geometry.hpp" #include +#include +#include +#include namespace blank { +glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept { + glm::vec3 v(cross(a, b)); + if (iszero(v)) { + // a and b are parallel + if (iszero(a - b)) { + // a and b are identical + return glm::mat3(1.0f); + } else { + // a and b are opposite + // create arbitrary unit vector perpendicular to a and + // rotate 180° around it + glm::vec3 arb(a); + if (std::abs(a.x - 1.0f) > std::numeric_limits::epsilon()) { + arb.x += 1.0f; + } else { + arb.y += 1.0f; + } + glm::vec3 axis(normalize(cross(a, arb))); + return glm::mat3(glm::rotate(PI, axis)); + } + } + float mv = length_squared(v); + float c = dot(a, b); + float f = (1 - c) / mv; + glm::mat3 vx(matrixCross3(v)); + return glm::mat3(1.0f) + vx + (pow2(vx) * f); +} + bool Intersection( const Ray &ray, const AABB &aabb,