]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/geometry.cpp
slight simplification of cull test
[blank.git] / src / geometry / geometry.cpp
index ff28d2e0602c041abcac485099dae79915ab9951..7798d33e949362ede44727f75402832e87a286ae 100644 (file)
@@ -32,7 +32,7 @@ glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
                        return glm::mat3(glm::rotate(PI, axis));
                }
        }
-       float mv = length_squared(v);
+       float mv = length2(v);
        float c = dot(a, b);
        float f = (1 - c) / mv;
        glm::mat3 vx(matrixCross3(v));
@@ -127,13 +127,22 @@ bool Intersection(
                glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.max.z, 1)),
        };
 
-       glm::vec3 axes[6] = {
-               glm::vec3(a_m * glm::vec4(1, 0, 0, 0)),
-               glm::vec3(a_m * glm::vec4(0, 1, 0, 0)),
-               glm::vec3(a_m * glm::vec4(0, 0, 1, 0)),
-               glm::vec3(b_m * glm::vec4(1, 0, 0, 0)),
-               glm::vec3(b_m * glm::vec4(0, 1, 0, 0)),
-               glm::vec3(b_m * glm::vec4(0, 0, 1, 0)),
+       glm::vec3 axes[15] = {
+               glm::vec3(a_m[0]),
+               glm::vec3(a_m[1]),
+               glm::vec3(a_m[2]),
+               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]))),
        };
 
        depth = std::numeric_limits<float>::infinity();
@@ -141,6 +150,11 @@ bool Intersection(
 
        int cur_axis = 0;
        for (const glm::vec3 &axis : axes) {
+               if (any(isnan(axis))) {
+                       // can result from the cross products if A and B have parallel axes
+                       ++cur_axis;
+                       continue;
+               }
                float a_min = std::numeric_limits<float>::infinity();
                float a_max = -std::numeric_limits<float>::infinity();
                for (const glm::vec3 &corner : a_corners) {
@@ -185,21 +199,19 @@ bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
                { box.max.x, box.max.y, box.min.z, 1.0f },
                { box.max.x, box.max.y, box.max.z, 1.0f },
        };
+
+       // check how many corners lie outside
+       int hits[6] = { 0, 0, 0, 0, 0, 0 };
        for (glm::vec4 &corner : corners) {
                corner = MVP * corner;
+               // replacing this with *= 1/w is effectively more expensive
                corner /= corner.w;
-       }
-
-       int hits[6] = { 0, 0, 0, 0, 0, 0 };
-
-       // check how many corners lie outside
-       for (const glm::vec4 &corner : corners) {
-               if (corner.x >  1.0f) ++hits[0];
-               if (corner.x < -1.0f) ++hits[1];
-               if (corner.y >  1.0f) ++hits[2];
-               if (corner.y < -1.0f) ++hits[3];
-               if (corner.z >  1.0f) ++hits[4];
-               if (corner.z < -1.0f) ++hits[5];
+               hits[0] += (corner.x >  1.0f);
+               hits[1] += (corner.x < -1.0f);
+               hits[2] += (corner.y >  1.0f);
+               hits[3] += (corner.y < -1.0f);
+               hits[4] += (corner.z >  1.0f);
+               hits[5] += (corner.z < -1.0f);
        }
 
        // if all corners are outside any given clip plane, the test is true