summaryrefslogtreecommitdiff
path: root/game/geoData.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-01-07 18:57:08 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-01-07 19:02:01 +0000
commit4ef5c934aacbfce6b3c5502425975edcf61cf0e8 (patch)
tree4f7a64f173dc9bab8467f32b95eb111cf5f0f162 /game/geoData.cpp
parentDon't invoke CHECK parameters multiple times (diff)
downloadilt-4ef5c934aacbfce6b3c5502425975edcf61cf0e8.tar.bz2
ilt-4ef5c934aacbfce6b3c5502425975edcf61cf0e8.tar.xz
ilt-4ef5c934aacbfce6b3c5502425975edcf61cf0e8.zip
Initial commit GeoData ray tracer
Diffstat (limited to 'game/geoData.cpp')
-rw-r--r--game/geoData.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index c3fb5dc..4537026 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -95,6 +95,45 @@ 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)}
+{
+ using Limits = std::numeric_limits<typename glm::vec2::value_type>;
+ static_assert(Limits::has_infinity);
+ static constexpr const glm::vec2 inf {Limits::infinity(), -Limits::infinity()};
+
+ auto byAxis = [this, p0, p1, scale](auto axis) {
+ if (d[axis] == 0) {
+ inc[axis] = 0;
+ return inf[axis];
+ }
+ else if (p1[axis] > p0[axis]) {
+ inc[axis] = scale;
+ return (std::floor(p0[axis]) + 1 - p0[axis]) * d[1 - axis];
+ }
+ else {
+ inc[axis] = -scale;
+ return (p0[axis] - std::floor(p0[axis])) * d[1 - axis];
+ }
+ };
+
+ error = byAxis(0);
+ error -= byAxis(1);
+ inc.z = scale;
+}
+
+glm::vec2
+GeoData::RayTracer::next()
+{
+ glm::vec2 cur {p};
+
+ 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;
+
+ return cur;
+}
+
unsigned int
GeoData::at(glm::ivec2 coord) const
{