diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-07 01:10:26 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-07 01:10:26 +0000 |
commit | 95eed94c9294f719724283e672cc7c83b2abd282 (patch) | |
tree | f0078bdacea65bfb03be3700c6e0f0c896336778 | |
parent | Helper to create a cubiod from dimensions (diff) | |
download | ilt-95eed94c9294f719724283e672cc7c83b2abd282.tar.bz2 ilt-95eed94c9294f719724283e672cc7c83b2abd282.tar.xz ilt-95eed94c9294f719724283e672cc7c83b2abd282.zip |
Unified crossProduct
-rw-r--r-- | game/geoData.cpp | 2 | ||||
-rw-r--r-- | gfx/gl/camera.cpp | 4 | ||||
-rw-r--r-- | lib/maths.h | 20 | ||||
-rw-r--r-- | lib/ray.cpp | 5 |
4 files changed, 23 insertions, 8 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp index 97f4a26..816733f 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -161,7 +161,7 @@ namespace { positionOnTriangle(const GlobalPosition2D point, const GeoData::Triangle<3> & t) { const CalcPosition3D a = t[1] - t[0], b = t[2] - t[0]; - const auto n = crossInt(a, b); + const auto n = crossProduct(a, b); return {point, ((n.x * t[0].x) + (n.y * t[0].y) + (n.z * t[0].z) - (n.x * point.x) - (n.y * point.y)) / n.z}; } diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index d362b94..0bc911a 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -31,8 +31,8 @@ Camera::updateView() Direction3D Camera::upFromForward(const Direction3D & forward) { - const auto right = glm::cross(forward, ::down); - return glm::cross(forward, right); + const auto right = crossProduct(forward, ::down); + return crossProduct(forward, right); } std::array<GlobalPosition4D, 4> diff --git a/lib/maths.h b/lib/maths.h index c1bf61a..b5af9ca 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -68,9 +68,9 @@ sq(T v) return v * v; } -template<std::integral T, glm::qualifier Q> -inline constexpr glm::vec<3, T, Q> -crossInt(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +template<glm::qualifier Q> +inline constexpr glm::vec<3, int64_t, Q> +crossProduct(const glm::vec<3, int64_t, Q> a, const glm::vec<3, int64_t, Q> b) { return { (a.y * b.z) - (a.z * b.y), @@ -79,6 +79,20 @@ crossInt(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) }; } +template<std::integral T, glm::qualifier Q> +inline constexpr glm::vec<3, T, Q> +crossProduct(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +{ + return crossProduct<int64_t, Q>(a, b); +} + +template<std::floating_point T, glm::qualifier Q> +inline constexpr glm::vec<3, T, Q> +crossProduct(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +{ + return glm::cross(a, b); +} + template<typename R = float, typename Ta, typename Tb> inline constexpr auto ratio(Ta a, Tb b) diff --git a/lib/ray.cpp b/lib/ray.cpp index 9fb3648..254ad14 100644 --- a/lib/ray.cpp +++ b/lib/ray.cpp @@ -1,4 +1,5 @@ #include "ray.h" +#include "maths.h" #include <algorithm> Ray @@ -14,8 +15,8 @@ Ray::distanceToLine(const Position3D & p1, const Position3D & e1) const const auto diff = p1 - e1; const auto d1 = glm::normalize(diff); const auto &p2 = start, &d2 = direction; - const auto n = glm::cross(d1, d2); - const auto n2 = glm::cross(d2, n); + const auto n = crossProduct(d1, d2); + const auto n2 = crossProduct(d2, n); const auto c1 = p1 + (glm::dot((p2 - p1), n2) / glm::dot(d1, n2)) * d1; const auto difflength = glm::length(diff); if (glm::length(c1 - p1) > difflength || glm::length(c1 - e1) > difflength) { |