From c508f08ce4841eabbcaf5df7c31ff4331aa9750c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 8 Jan 2022 13:39:36 +0000 Subject: Tweak ray tracer calls to account for scale --- game/geoData.cpp | 18 ++++++++---------- game/geoData.h | 4 ++-- test/test-geo.cpp | 33 +++++++++++++++------------------ 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/game/geoData.cpp b/game/geoData.cpp index 4537026..32ec55e 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -95,30 +95,28 @@ GeoData::positionAt(const glm::vec2 wcoord) const return wcoord || heightMid; } -GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1, float scale) : p {glm::floor(p0)}, d {glm::abs(p1 - p0)} +GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1) : p {glm::floor(p0)}, d {glm::abs(p1)} { using Limits = std::numeric_limits; static_assert(Limits::has_infinity); - static constexpr const glm::vec2 inf {Limits::infinity(), -Limits::infinity()}; - auto byAxis = [this, p0, p1, scale](auto axis) { + auto byAxis = [this, p0, p1](auto axis) { if (d[axis] == 0) { inc[axis] = 0; - return inf[axis]; + return Limits::infinity(); } - else if (p1[axis] > p0[axis]) { - inc[axis] = scale; - return (std::floor(p0[axis]) + 1 - p0[axis]) * d[1 - axis]; + else if (p1[axis] > 0) { + inc[axis] = 1; + return (std::floor(p0[axis]) + 1.F - p0[axis]) * d[1 - axis]; } else { - inc[axis] = -scale; + inc[axis] = -1; return (p0[axis] - std::floor(p0[axis])) * d[1 - axis]; } }; error = byAxis(0); error -= byAxis(1); - inc.z = scale; } glm::vec2 @@ -129,7 +127,7 @@ GeoData::RayTracer::next() static constexpr const glm::vec2 m {1, -1}; const int axis = (error > 0) ? 1 : 0; p[axis] += inc[axis]; - error += d[1 - axis] * m[axis] * inc.z; + error += d[1 - axis] * m[axis]; return cur; } diff --git a/game/geoData.h b/game/geoData.h index 7bd6ae5..a296205 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -34,7 +34,7 @@ public: class RayTracer { public: - RayTracer(glm::vec2 p0, glm::vec2 p1, float scale); + RayTracer(glm::vec2 p0, glm::vec2 p1); glm::vec2 next(); @@ -42,7 +42,7 @@ public: glm::vec2 p; const glm::vec2 d; float error; - glm::vec3 inc; + glm::vec2 inc; }; protected: diff --git a/test/test-geo.cpp b/test/test-geo.cpp index f0ed693..07e3bd1 100644 --- a/test/test-geo.cpp +++ b/test/test-geo.cpp @@ -97,14 +97,11 @@ BOOST_AUTO_TEST_CASE(get_height_at) BOOST_CHECK_EQUAL(positionAt({2.5F, 2.5F}), glm::vec3(2.5F, 2.5F, 2.5F)); } -using TestRayTracerData = std::tuple>; +using TestRayTracerData = std::tuple>; +BOOST_TEST_DECORATOR(*boost::unit_test::timeout(1)) BOOST_DATA_TEST_CASE(raytracer, boost::unit_test::data::make({ - {{0, 0}, {0, 0}, 3, - { - {0, 0}, - }}, - {{0, 0}, {4, 5}, 4, + {{1, 2}, {4, 5}, 4, { {0, 0}, {0, 4}, @@ -114,26 +111,26 @@ BOOST_DATA_TEST_CASE(raytracer, {8, 12}, {12, 12}, {12, 16}, - {16, 16}, + {12, 20}, {16, 20}, }}, {{-1, -1}, {-4, -5}, 5, { - {-1, -1}, - {-6, -1}, - {-6, -6}, - {-6, -11}, - {-11, -11}, - {-11, -16}, - {-16, -16}, - {-16, -21}, + {-5, -5}, + {-5, -10}, + {-10, -10}, + {-10, -15}, + {-15, -15}, + {-15, -20}, + {-20, -20}, + {-20, -25}, }}, }), - start, end, scale, points) + start, dir, scale, points) { - GeoData::RayTracer rt {start, end, scale}; + GeoData::RayTracer rt {start / scale, glm::normalize(dir)}; for (const auto & point : points) { - BOOST_CHECK_CLOSE_VEC(point, rt.next()); + BOOST_CHECK_CLOSE_VEC(point, rt.next() * scale); } } -- cgit v1.2.3