diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-03 20:12:03 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-11-03 20:12:03 +0000 |
commit | 3931bda936aba6ddae26c1d9f1d450781df800e8 (patch) | |
tree | c66bb3e540354baa2e45a782d8ae1f012be33a96 /game/terrain2.cpp | |
parent | Helper to get cartesian position from baricentric on Triangle (diff) | |
download | ilt-3931bda936aba6ddae26c1d9f1d450781df800e8.tar.bz2 ilt-3931bda936aba6ddae26c1d9f1d450781df800e8.tar.xz ilt-3931bda936aba6ddae26c1d9f1d450781df800e8.zip |
Implement terrain intersect ray
Diffstat (limited to 'game/terrain2.cpp')
-rw-r--r-- | game/terrain2.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/game/terrain2.cpp b/game/terrain2.cpp index b129284..d1af221 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -135,6 +135,29 @@ TerrainMesh::positionAt(const PointFace & p) const return p.point ^ out[0]; } +[[nodiscard]] std::optional<glm::vec3> +TerrainMesh::intersectRay(const Ray & ray) const +{ + return intersectRay(ray, findPoint(ray.start)); +} + +[[nodiscard]] std::optional<glm::vec3> +TerrainMesh::intersectRay(const Ray & ray, FaceHandle face) const +{ + std::optional<glm::vec3> out; + walkUntil(PointFace {ray.start, face}, ray.start + (ray.direction * 10000.F), [&out, &ray, this](FaceHandle face) { + glm::vec2 bari {}; + float dist {}; + Triangle<3> t {this, fv_range(face)}; + if (glm::intersectRayTriangle(ray.start, ray.direction, t[0], t[1], t[2], bari, dist)) { + out = t * bari; + return true; + } + return false; + }); + return out; +} + void TerrainMesh::walk(const PointFace & from, const glm::vec2 to, const std::function<void(FaceHandle)> & op) const { |