From b3d75fe53ec3f7766e4ee6e35bfccd99c1ef9eed Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 13 Jan 2022 19:38:11 +0000 Subject: Progressive constructors for RayTracer --- game/geoData.cpp | 43 ++++++++++++++++++++++++------------------- game/geoData.h | 4 ++++ 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 2028937..25c7131 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -99,28 +99,33 @@ GeoData::positionAt(const glm::vec2 wcoord) const return wcoord || heightMid; } -GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1) : p {glm::floor(p0)}, d {glm::abs(p1)} +GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1) : RayTracer {p0, p1, glm::abs(p1)} { } +GeoData::RayTracer::RayTracer(glm::vec2 p0, glm::vec2 p1, glm::vec2 d) : + RayTracer {p0, d, byAxis(p0, p1, d, 0), byAxis(p0, p1, d, 1)} { - using Limits = std::numeric_limits; - static_assert(Limits::has_infinity); +} - auto byAxis = [this, p0, p1](auto axis) { - if (d[axis] == 0) { - inc[axis] = 0; - return Limits::infinity(); - } - else if (p1[axis] > 0) { - inc[axis] = 1; - return (std::floor(p0[axis]) + 1.F - p0[axis]) * d[1 - axis]; - } - else { - inc[axis] = -1; - return (p0[axis] - std::floor(p0[axis])) * d[1 - axis]; - } - }; +GeoData::RayTracer::RayTracer( + glm::vec2 p0, glm::vec2 d_, std::pair xdata, std::pair ydata) : + p {glm::floor(p0)}, + d {d_}, error {xdata.second - ydata.second}, inc {xdata.first, ydata.first} +{ +} - error = byAxis(0); - error -= byAxis(1); +std::pair +GeoData::RayTracer::byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_t axis) +{ + using Limits = std::numeric_limits; + static_assert(Limits::has_infinity); + if (d[axis] == 0) { + return {0, Limits::infinity()}; + } + else if (p1[axis] > 0) { + return {1, (std::floor(p0[axis]) + 1.F - p0[axis]) * d[1 - axis]}; + } + else { + return {-1, (p0[axis] - std::floor(p0[axis])) * d[1 - axis]}; + } } glm::vec2 diff --git a/game/geoData.h b/game/geoData.h index 2a1c8bd..b16aed7 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -44,6 +44,10 @@ public: glm::vec2 next(); private: + RayTracer(glm::vec2 p0, glm::vec2 p1, glm::vec2 d); + RayTracer(glm::vec2 p0, glm::vec2 d, std::pair, std::pair); + static std::pair byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_t); + glm::vec2 p; const glm::vec2 d; float error; -- cgit v1.2.3