X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmath%2Fgeometry.cpp;h=3afa944f479b0fc41905d5de6364ee6ca8068e5b;hb=3989da924c4e33c52f500aead5ae62bb40294781;hp=aa0389dac3df5b54d7783440c19f261e2feb1fac;hpb=f5fc0c2bd1c0d1e2737d2b4ed49c3de16aa67c67;p=blobs.git diff --git a/src/math/geometry.cpp b/src/math/geometry.cpp index aa0389d..3afa944 100644 --- a/src/math/geometry.cpp +++ b/src/math/geometry.cpp @@ -41,15 +41,15 @@ bool Intersect( glm::dvec3(b_m[0]), glm::dvec3(b_m[1]), glm::dvec3(b_m[2]), - normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[0]))), - normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[1]))), - normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[2]))), - normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[0]))), - normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[1]))), - normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[2]))), - normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[0]))), - normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[1]))), - normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[2]))), + glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[0]))), + glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[1]))), + glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[2]))), + glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[0]))), + glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[1]))), + glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[2]))), + glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[0]))), + glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[1]))), + glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[2]))), }; depth = std::numeric_limits::infinity(); @@ -57,7 +57,7 @@ bool Intersect( int cur_axis = 0; for (const glm::dvec3 &axis : axes) { - if (any(isnan(axis))) { + if (glm::any(glm::isnan(axis))) { // can result from the cross products if A and B have parallel axes ++cur_axis; continue; @@ -65,7 +65,7 @@ bool Intersect( double a_min = std::numeric_limits::infinity(); double a_max = -std::numeric_limits::infinity(); for (const glm::dvec3 &corner : a_corners) { - double val = dot(corner, axis); + double val = glm::dot(corner, axis); a_min = std::min(a_min, val); a_max = std::max(a_max, val); } @@ -73,7 +73,7 @@ bool Intersect( double b_min = std::numeric_limits::infinity(); double b_max = -std::numeric_limits::infinity(); for (const glm::dvec3 &corner : b_corners) { - double val = dot(corner, axis); + double val = glm::dot(corner, axis); b_min = std::min(b_min, val); b_max = std::max(b_max, val); } @@ -94,5 +94,91 @@ bool Intersect( } +bool Intersect( + const Ray &ray, + const AABB &aabb, + const glm::dmat4 &M, + glm::dvec3 &normal, + double &dist +) noexcept { + double t_min = 0.0; + double t_max = std::numeric_limits::infinity(); + const glm::dvec3 aabb_pos(M[3].x, M[3].y, M[3].z); + const glm::dvec3 delta = aabb_pos - ray.Origin(); + + glm::dvec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max); + + for (int i = 0; i < 3; ++i) { + const glm::dvec3 axis(M[i].x, M[i].y, M[i].z); + const double e = glm::dot(axis, delta); + const double f = glm::dot(axis, ray.Direction()); + + if (std::abs(f) > std::numeric_limits::epsilon()) { + t1[i] = (e + aabb.min[i]) / f; + t2[i] = (e + aabb.max[i]) / f; + + t_min = std::max(t_min, std::min(t1[i], t2[i])); + t_max = std::min(t_max, std::max(t1[i], t2[i])); + + if (t_max < t_min) { + return false; + } + } else { + if (aabb.min[i] - e > 0.0 || aabb.max[i] - e < 0.0) { + return false; + } + } + } + + dist = t_min; + + glm::dvec3 min_all(glm::min(t1, t2)); + if (min_all.x > min_all.y) { + if (min_all.x > min_all.z) { + normal = glm::dvec3(t2.x < t1.x ? 1 : -1, 0, 0); + } else { + normal = glm::dvec3(0, 0, t2.z < t1.z ? 1 : -1); + } + } else if (min_all.y > min_all.z) { + normal = glm::dvec3(0, t2.y < t1.y ? 1 : -1, 0); + } else { + normal = glm::dvec3(0, 0, t2.z < t1.z ? 1 : -1); + } + + return true; +} + +bool Intersect( + const Ray &r, + const Sphere &s, + glm::dvec3 &normal, + double &dist +) noexcept { + const glm::dvec3 diff(s.origin - r.Origin()); + if (glm::dot(diff, r.Direction()) < 0.0) { + if (glm::length2(diff) > s.radius * s.radius) return false; + if (std::abs(glm::length2(diff) - s.radius * s.radius) < std::numeric_limits::epsilon() * s.radius) { + normal = glm::normalize(-diff); + dist = 0.0; + return true; + } + const glm::dvec3 pc(r.Direction() * glm::dot(r.Direction(), diff) + r.Origin()); + double idist = std::sqrt(s.radius * s.radius - glm::length2(pc - s.origin)); + dist = idist - glm::length(pc - r.Origin()); + normal = glm::normalize((r.Origin() + (r.Direction() * dist)) - s.origin); + return true; + } + const glm::dvec3 pc(r.Direction() * glm::dot(r.Direction(), diff) + r.Origin()); + if (glm::length2(s.origin - pc) > s.radius * s.radius) return false; + double idist = std::sqrt(s.radius * s.radius - glm::length2(pc - s.origin)); + if (glm::length2(diff) > s.radius * s.radius) { + dist = glm::length(pc - r.Origin()) - idist; + } else { + dist = glm::length(pc - r.Origin()) + idist; + } + normal = glm::normalize((r.Origin() + (r.Direction() * dist)) - s.origin); + return true; +} + } }