summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-01-03 18:37:09 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-01-03 18:37:09 +0000
commitd33f528f17ed1be17aca82e5c0bc9f39c1d496ae (patch)
tree17f37991e09d8d7345557e6a6d24a16ab8032d87 /game
parentMore complete and tested stream output support (diff)
downloadilt-d33f528f17ed1be17aca82e5c0bc9f39c1d496ae.tar.bz2
ilt-d33f528f17ed1be17aca82e5c0bc9f39c1d496ae.tar.xz
ilt-d33f528f17ed1be17aca82e5c0bc9f39c1d496ae.zip
Support for querying terrain height in GeoData
Diffstat (limited to 'game')
-rw-r--r--game/geoData.cpp23
-rw-r--r--game/geoData.h2
2 files changed, 25 insertions, 0 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp
index 55c69a1..d66f1ed 100644
--- a/game/geoData.cpp
+++ b/game/geoData.cpp
@@ -1,11 +1,14 @@
#include "geoData.h"
#include "gfx/image.h"
#include <algorithm>
+#include <array>
+#include <cmath>
#include <cstddef>
#include <maths.h>
#include <random>
#include <stb/stb_image.h>
#include <stdexcept>
+#include <util.h>
GeoData::GeoData(Limits l, float s) :
limit {std::move(l)}, size {(limit.second - limit.first) + 1}, scale {s}, nodes {[this]() {
@@ -65,6 +68,26 @@ GeoData::loadFromImages(const std::filesystem::path & fileName, float scale_)
});
}
+glm::vec3
+GeoData::positionAt(const glm::vec2 wcoord) const
+{
+ const auto coord {wcoord / scale};
+ const std::array<glm::vec2, 4> gridCorner {
+ {{std::floor(coord.x), std::floor(coord.y)}, {std::floor(coord.x), std::ceil(coord.y)},
+ {std::ceil(coord.x), std::floor(coord.y)}, {std::ceil(coord.x), std::ceil(coord.y)}}};
+
+ const auto point {transform_array(gridCorner, [this](const auto c) {
+ return nodes[at(c)].height;
+ })};
+
+ const glm::vec2 frac = coord - gridCorner.front();
+ const auto heightFloor = point[0] + ((point[2] - point[0]) * frac.x),
+ heightCeil = point[1] + ((point[3] - point[1]) * frac.x),
+ heightMid = heightFloor + ((heightCeil - heightFloor) * frac.y);
+
+ return wcoord || heightMid;
+}
+
unsigned int
GeoData::at(glm::ivec2 coord) const
{
diff --git a/game/geoData.h b/game/geoData.h
index 39c149c..e5057c3 100644
--- a/game/geoData.h
+++ b/game/geoData.h
@@ -20,6 +20,8 @@ public:
void generateRandom();
void loadFromImages(const std::filesystem::path &, float scale);
+ [[nodiscard]] glm::vec3 positionAt(glm::vec2) const;
+
[[nodiscard]] unsigned int at(glm::ivec2) const;
[[nodiscard]] unsigned int at(int x, int y) const;