]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/geometry.cpp
make Intersection() repeatable for normal
[blank.git] / src / geometry / geometry.cpp
index 1e84fbb9068d1749fbe5a3edcc81519aa7718e87..6ff26cad40b928b761a62630f9538d467c3c6348 100644 (file)
@@ -14,7 +14,7 @@
 namespace blank {
 
 glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
-       glm::vec3 v(cross(a, b));
+       glm::vec3 v(glm::cross(a, b));
        if (iszero(v)) {
                // a and b are parallel
                if (iszero(a - b)) {
@@ -30,15 +30,15 @@ glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
                        } else {
                                arb.y += 1.0f;
                        }
-                       glm::vec3 axis(normalize(cross(a, arb)));
+                       glm::vec3 axis(glm::normalize(glm::cross(a, arb)));
                        return glm::mat3(glm::rotate(PI, axis));
                }
        }
-       float mv = length2(v);
-       float c = dot(a, b);
+       float mv = glm::length2(v);
+       float c = glm::dot(a, b);
        float f = (1 - c) / mv;
-       glm::mat3 vx(matrixCross3(v));
-       return glm::mat3(1.0f) + vx + (pow2(vx) * f);
+       glm::mat3 vx(glm::matrixCross3(v));
+       return glm::mat3(1.0f) + vx + (glm::pow2(vx) * f);
 }
 
 std::ostream &operator <<(std::ostream &out, const AABB &box) {
@@ -49,6 +49,23 @@ std::ostream &operator <<(std::ostream &out, const Ray &ray) {
        return out << "Ray(" << ray.orig << ", " << ray.dir << ')';
 }
 
+bool Intersection(
+       const Ray &ray,
+       const AABB &box,
+       float &dist
+) noexcept {
+       float t_min = 0.0f;
+       float t_max = std::numeric_limits<float>::infinity();
+       for (int i = 0; i < 3; ++i) {
+               float t1 = (box.min[i] - ray.orig[i]) * ray.inv_dir[i];
+               float t2 = (box.max[i] - ray.orig[i]) * ray.inv_dir[i];
+               t_min = std::max(t_min, std::min(t1, t2));
+               t_max = std::min(t_max, std::max(t1, t2));
+       }
+       dist = t_min;
+       return t_max >= t_min;
+}
+
 bool Intersection(
        const Ray &ray,
        const AABB &aabb,
@@ -85,28 +102,26 @@ bool Intersection(
                }
        }
 
-       glm::vec3 min_all(min(t1, t2));
-
        if (dist) {
                *dist = t_min;
        }
        if (normal) {
+               glm::vec3 min_all(glm::min(t1, t2));
                if (min_all.x > min_all.y) {
                        if (min_all.x > min_all.z) {
-                               normal->x = t2.x < t1.x ? 1 : -1;
+                               *normal = glm::vec3(t2.x < t1.x ? 1 : -1, 0, 0);
                        } else {
-                               normal->z = t2.z < t1.z ? 1 : -1;
+                               *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1);
                        }
                } else if (min_all.y > min_all.z) {
-                       normal->y = t2.y < t1.y ? 1 : -1;
+                       *normal = glm::vec3(0, t2.y < t1.y ? 1 : -1, 0);
                } else {
-                       normal->z = t2.z < t1.z ? 1 : -1;
+                       *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1);
                }
        }
        return true;
 }
 
-
 bool Intersection(
        const AABB &a_box,
        const glm::mat4 &a_m,
@@ -144,15 +159,15 @@ bool Intersection(
                glm::vec3(b_m[0]),
                glm::vec3(b_m[1]),
                glm::vec3(b_m[2]),
-               normalize(cross(glm::vec3(a_m[0]), glm::vec3(b_m[0]))),
-               normalize(cross(glm::vec3(a_m[0]), glm::vec3(b_m[1]))),
-               normalize(cross(glm::vec3(a_m[0]), glm::vec3(b_m[2]))),
-               normalize(cross(glm::vec3(a_m[1]), glm::vec3(b_m[0]))),
-               normalize(cross(glm::vec3(a_m[1]), glm::vec3(b_m[1]))),
-               normalize(cross(glm::vec3(a_m[1]), glm::vec3(b_m[2]))),
-               normalize(cross(glm::vec3(a_m[2]), glm::vec3(b_m[0]))),
-               normalize(cross(glm::vec3(a_m[2]), glm::vec3(b_m[1]))),
-               normalize(cross(glm::vec3(a_m[2]), glm::vec3(b_m[2]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[0]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[1]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[2]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[0]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[1]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[2]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[0]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[1]))),
+               glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[2]))),
        };
 
        depth = std::numeric_limits<float>::infinity();
@@ -160,7 +175,7 @@ bool Intersection(
 
        int cur_axis = 0;
        for (const glm::vec3 &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;
@@ -255,7 +270,7 @@ bool CullTest(const AABB &box, const Frustum &frustum) noexcept {
                        ((plane.normal.y > 0.0f) ? box.max.y : box.min.y),
                        ((plane.normal.z > 0.0f) ? box.max.z : box.min.z)
                );
-               const float dp = dot(plane.normal, np);
+               const float dp = glm::dot(plane.normal, np);
                // cull if nearest point is on the "outside" side of the plane
                if (dp < -plane.dist) return true;
        }