diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-10 02:07:53 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-10 02:07:53 +0000 |
commit | 94f0e426bc8f298aec90fd0d4fcf6f823f8c399c (patch) | |
tree | c1be0231cab6f9680aa8c3f9e0acf531d8debb89 /game/geoData.cpp | |
parent | Tweak ray tracer calls to account for scale (diff) | |
download | ilt-94f0e426bc8f298aec90fd0d4fcf6f823f8c399c.tar.bz2 ilt-94f0e426bc8f298aec90fd0d4fcf6f823f8c399c.tar.xz ilt-94f0e426bc8f298aec90fd0d4fcf6f823f8c399c.zip |
Ray Trace cursor clicks to terrain surface
Diffstat (limited to 'game/geoData.cpp')
-rw-r--r-- | game/geoData.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp index 32ec55e..c9e909b 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -4,8 +4,10 @@ #include <array> #include <cmath> #include <cstddef> +#include <glm/gtx/intersect.hpp> #include <maths.h> #include <random> +#include <ray.hpp> #include <stb/stb_image.h> #include <stdexcept> #include <util.h> @@ -132,6 +134,38 @@ GeoData::RayTracer::next() return cur; } +std::optional<glm::vec3> +GeoData::intersectRay(const Ray & ray) const +{ + if (glm::length(!ray.direction) <= 0) { + return {}; + } + RayTracer rt {ray.start / scale, ray.direction}; + while (true) { + const auto n {rt.next() * scale}; + try { + const auto point = quad(n); + for (auto offset : {0U, 1U}) { + glm::vec2 bary; + float distance; + if (glm::intersectRayTriangle(ray.start, ray.direction, point[offset], point[offset + 1], + point[offset + 2], bary, distance)) { + return point[offset] + ((point[offset + 1] - point[offset]) * bary[0]) + + ((point[offset + 2] - point[offset]) * bary[1]); + } + } + } + catch (std::range_error &) { + const auto rel = n / !ray.direction; + if (rel.x > 0 && rel.y > 0) { + return {}; + } + } + } + + return {}; +} + unsigned int GeoData::at(glm::ivec2 coord) const { |