From 16bec12e3e75e95d251745f904ac4e15af9c9724 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 28 Oct 2023 02:34:45 +0100 Subject: Initial OpenMesh based terrain data and tests --- game/terrain2.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ game/terrain2.h | 24 ++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 game/terrain2.cpp create mode 100644 game/terrain2.h (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp new file mode 100644 index 0000000..83df3fa --- /dev/null +++ b/game/terrain2.cpp @@ -0,0 +1,66 @@ +#include "terrain2.h" +#include + +TerrainMesh::TerrainMesh(const std::filesystem::path & input) +{ + size_t ncols = 0, nrows = 0, xllcorner = 0, yllcorner = 0, cellsize = 0; + std::map properties { + {"ncols", &ncols}, + {"nrows", &nrows}, + {"xllcorner", &xllcorner}, + {"yllcorner", &yllcorner}, + {"cellsize", &cellsize}, + }; + std::ifstream f {input}; + while (!properties.empty()) { + std::string property; + f >> property; + f >> *properties.at(property); + properties.erase(property); + } + std::vector vertices; + vertices.reserve(ncols * nrows); + for (size_t row = 0; row < nrows; ++row) { + for (size_t col = 0; col < ncols; ++col) { + float height = 0; + f >> height; + vertices.push_back(add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); + } + } + if (!f.good()) { + throw std::runtime_error("Couldn't read terrain file"); + } + for (size_t row = 1; row < nrows; ++row) { + for (size_t col = 1; col < ncols; ++col) { + add_face({ + vertices[ncols * (row - 1) + (col - 1)], + vertices[ncols * (row - 0) + (col - 0)], + vertices[ncols * (row - 0) + (col - 1)], + }); + add_face({ + vertices[ncols * (row - 1) + (col - 1)], + vertices[ncols * (row - 1) + (col - 0)], + vertices[ncols * (row - 0) + (col - 0)], + }); + } + } + update_face_normals(); + update_vertex_normals(); +}; + +bool +TerrainMesh::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) +{ + const auto det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); + + return det * ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0 + && det * ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x)) >= 0 + && det * ((a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x)) >= 0; +} + +bool +TerrainMesh::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const +{ + auto vertices = cfv_iter(face); + return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++)); +} diff --git a/game/terrain2.h b/game/terrain2.h new file mode 100644 index 0000000..d2da8d0 --- /dev/null +++ b/game/terrain2.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +struct TerrainTraits : public OpenMesh::DefaultTraits { + FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + EdgeAttributes(OpenMesh::Attributes::Status); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + HalfedgeAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + using Point = glm::vec3; + using Normal = glm::vec3; +}; + +class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT { +public: + explicit TerrainMesh(const std::filesystem::path &); + +protected: + [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); + [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; +}; -- cgit v1.2.3 From 49a422ed91289a31142a8c0fb8b7030d2dea98a1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 29 Oct 2023 14:11:46 +0000 Subject: Initial commit of findPoint on terrain 2D navigate from a default/given starting point, possible scope for improvement, but it's not exactly slow; <9ms --- game/terrain2.cpp | 33 ++++++++++++++++++++++++++++++++- game/terrain2.h | 4 ++++ 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 83df3fa..cc0bf90 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -48,6 +48,32 @@ TerrainMesh::TerrainMesh(const std::filesystem::path & input) update_vertex_normals(); }; +OpenMesh::FaceHandle +TerrainMesh::findPoint(glm::vec2 p) const +{ + return findPoint(p, *faces_begin()); +} + +OpenMesh::FaceHandle +TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const +{ + ConstFaceVertexIter vertices; + while (f.is_valid() && !triangleContainsPoint(p, vertices = cfv_iter(f))) { + for (auto next = cfh_iter(f); next.is_valid(); ++next) { + f = opposite_face_handle(*next); + if (f.is_valid()) { + const auto e1 = point(to_vertex_handle(*next)); + const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); + if ((e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x)) { + break; + } + } + f.reset(); + } + } + return f; +} + bool TerrainMesh::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) { @@ -61,6 +87,11 @@ TerrainMesh::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const g bool TerrainMesh::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const { - auto vertices = cfv_iter(face); + return triangleContainsPoint(p, cfv_iter(face)); +} + +bool +TerrainMesh::triangleContainsPoint(const glm::vec2 p, ConstFaceVertexIter vertices) const +{ return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++)); } diff --git a/game/terrain2.h b/game/terrain2.h index d2da8d0..713fe23 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -18,7 +18,11 @@ class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT { public: explicit TerrainMesh(const std::filesystem::path &); + [[nodiscard]] FaceHandle findPoint(glm::vec2) const; + [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; + protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; + [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; }; -- cgit v1.2.3 From 583aa8eb078090fd48c6d6db8ee69da5ad8811fb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 31 Oct 2023 00:29:35 +0000 Subject: Initial commit of walk/walkUtil terrain --- game/terrain2.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- game/terrain2.h | 3 +++ 2 files changed, 57 insertions(+), 1 deletion(-) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index cc0bf90..3e24778 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -54,6 +54,28 @@ TerrainMesh::findPoint(glm::vec2 p) const return findPoint(p, *faces_begin()); } +namespace { + [[nodiscard]] constexpr inline bool + pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) + { + return (e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x); + } + + static_assert(pointLeftOfLine({1, 2}, {1, 1}, {2, 2})); + static_assert(pointLeftOfLine({2, 1}, {2, 2}, {1, 1})); + static_assert(pointLeftOfLine({2, 2}, {1, 2}, {2, 1})); + static_assert(pointLeftOfLine({1, 1}, {2, 1}, {1, 2})); + + [[nodiscard]] constexpr inline bool + linesCross(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2) + { + return pointLeftOfLine(a2, b1, b2) && pointLeftOfLine(a1, b2, b1) && pointLeftOfLine(b1, a1, a2) + && pointLeftOfLine(b2, a2, a1); + } + + static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1})); +} + OpenMesh::FaceHandle TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const { @@ -64,7 +86,7 @@ TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const if (f.is_valid()) { const auto e1 = point(to_vertex_handle(*next)); const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); - if ((e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x)) { + if (pointLeftOfLine(p, e1, e2)) { break; } } @@ -74,6 +96,37 @@ TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const return f; } +void +TerrainMesh::walk(const glm::vec2 from, const glm::vec2 to, const std::function & op) const +{ + walkUntil(from, to, [&op](const auto & fh) { + op(fh); + return false; + }); +} + +void +TerrainMesh::walkUntil(const glm::vec2 from, const glm::vec2 to, const std::function & op) const +{ + auto f = findPoint(from); + assert(f.is_valid()); // TODO replace with a boundary search + FaceHandle previousFace; + while (f.is_valid() && !op(f)) { + for (auto next = cfh_iter(f); next.is_valid(); ++next) { + f = opposite_face_handle(*next); + if (f.is_valid() && f != previousFace) { + const auto e1 = point(to_vertex_handle(*next)); + const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); + if (linesCross(from, to, e1, e2)) { + previousFace = f; + break; + } + } + f.reset(); + } + } +} + bool TerrainMesh::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) { diff --git a/game/terrain2.h b/game/terrain2.h index 713fe23..848c36e 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -21,6 +21,9 @@ public: [[nodiscard]] FaceHandle findPoint(glm::vec2) const; [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; + void walk(const glm::vec2 from, const glm::vec2 to, const std::function & op) const; + void walkUntil(const glm::vec2 from, const glm::vec2 to, const std::function & op) const; + protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; -- cgit v1.2.3 From a0f1dc90aabd4cb5cfe2aef7116af170e6d0b953 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 31 Oct 2023 01:26:58 +0000 Subject: Helper type for storing/passing/returning a point and its containing face point is const as face is mutable as a cache of the face containing point. --- game/terrain2.cpp | 18 ++++++++++++++++++ game/terrain2.h | 10 ++++++++++ 2 files changed, 28 insertions(+) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 3e24778..e98cebb 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -54,6 +54,24 @@ TerrainMesh::findPoint(glm::vec2 p) const return findPoint(p, *faces_begin()); } +bool +TerrainMesh::locate(const TerrainMesh::PointFace & pointFace, FaceHandle start) const +{ + if (pointFace.face.is_valid()) { + assert(triangleContainsPoint(pointFace.point, pointFace.face)); + return true; + } + else { + return (pointFace.face = findPoint(pointFace.point, start)).is_valid(); + } +} + +bool +TerrainMesh::locate(const TerrainMesh::PointFace & pointFace) const +{ + return locate(pointFace, *faces_begin()); +} + namespace { [[nodiscard]] constexpr inline bool pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) diff --git a/game/terrain2.h b/game/terrain2.h index 848c36e..e516719 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -18,6 +18,13 @@ class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT { public: explicit TerrainMesh(const std::filesystem::path &); + struct PointFace { + PointFace(const glm::vec2 p) : point {p} { } + + const glm::vec2 point; + mutable FaceHandle face {}; + }; + [[nodiscard]] FaceHandle findPoint(glm::vec2) const; [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; @@ -28,4 +35,7 @@ protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; + + bool locate(const PointFace &) const; + bool locate(const PointFace &, FaceHandle start) const; }; -- cgit v1.2.3 From a4f1c55ad34f6be9adc9573fd7f26507ae2c59b0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 31 Oct 2023 02:40:03 +0000 Subject: Update walk/walkUntil to work on PointFace from parameter --- game/terrain2.cpp | 11 ++++++----- game/terrain2.h | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index e98cebb..5d1c595 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -115,7 +115,7 @@ TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const } void -TerrainMesh::walk(const glm::vec2 from, const glm::vec2 to, const std::function & op) const +TerrainMesh::walk(const PointFace & from, const glm::vec2 to, const std::function & op) const { walkUntil(from, to, [&op](const auto & fh) { op(fh); @@ -124,10 +124,11 @@ TerrainMesh::walk(const glm::vec2 from, const glm::vec2 to, const std::function< } void -TerrainMesh::walkUntil(const glm::vec2 from, const glm::vec2 to, const std::function & op) const +TerrainMesh::walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const { - auto f = findPoint(from); - assert(f.is_valid()); // TODO replace with a boundary search + locate(from); + assert(from.face.is_valid()); // TODO replace with a boundary search + auto f = from.face; FaceHandle previousFace; while (f.is_valid() && !op(f)) { for (auto next = cfh_iter(f); next.is_valid(); ++next) { @@ -135,7 +136,7 @@ TerrainMesh::walkUntil(const glm::vec2 from, const glm::vec2 to, const std::func if (f.is_valid() && f != previousFace) { const auto e1 = point(to_vertex_handle(*next)); const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); - if (linesCross(from, to, e1, e2)) { + if (linesCross(from.point, to, e1, e2)) { previousFace = f; break; } diff --git a/game/terrain2.h b/game/terrain2.h index e516719..1a4e263 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -28,8 +28,8 @@ public: [[nodiscard]] FaceHandle findPoint(glm::vec2) const; [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; - void walk(const glm::vec2 from, const glm::vec2 to, const std::function & op) const; - void walkUntil(const glm::vec2 from, const glm::vec2 to, const std::function & op) const; + void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; + void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); -- cgit v1.2.3 From b46cf55d66f262778bf0353650f00620c7740f2a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 31 Oct 2023 19:36:16 +0000 Subject: Make PointFace harder to misuse --- game/terrain2.cpp | 33 +++++++++++++++++++++------------ game/terrain2.h | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 16 deletions(-) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 5d1c595..8d29143 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -54,22 +54,32 @@ TerrainMesh::findPoint(glm::vec2 p) const return findPoint(p, *faces_begin()); } -bool -TerrainMesh::locate(const TerrainMesh::PointFace & pointFace, FaceHandle start) const +TerrainMesh::PointFace::PointFace(const glm::vec2 p, const TerrainMesh * mesh) : + PointFace {p, mesh, *mesh->faces_begin()} +{ +} + +TerrainMesh::PointFace::PointFace(const glm::vec2 p, const TerrainMesh * mesh, FaceHandle start) : + PointFace {p, mesh->findPoint(p, start)} { - if (pointFace.face.is_valid()) { - assert(triangleContainsPoint(pointFace.point, pointFace.face)); - return true; +} + +TerrainMesh::FaceHandle +TerrainMesh::PointFace::face(const TerrainMesh * mesh, FaceHandle start) const +{ + if (_face.is_valid()) { + assert(mesh->triangleContainsPoint(point, _face)); + return _face; } else { - return (pointFace.face = findPoint(pointFace.point, start)).is_valid(); + return (_face = mesh->findPoint(point, start)); } } -bool -TerrainMesh::locate(const TerrainMesh::PointFace & pointFace) const +TerrainMesh::FaceHandle +TerrainMesh::PointFace::face(const TerrainMesh * mesh) const { - return locate(pointFace, *faces_begin()); + return face(mesh, *mesh->faces_begin()); } namespace { @@ -126,9 +136,8 @@ TerrainMesh::walk(const PointFace & from, const glm::vec2 to, const std::functio void TerrainMesh::walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const { - locate(from); - assert(from.face.is_valid()); // TODO replace with a boundary search - auto f = from.face; + assert(from.face(this).is_valid()); // TODO replace with a boundary search + auto f = from.face(this); FaceHandle previousFace; while (f.is_valid() && !op(f)) { for (auto next = cfh_iter(f); next.is_valid(); ++next) { diff --git a/game/terrain2.h b/game/terrain2.h index 1a4e263..641d608 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -19,10 +19,26 @@ public: explicit TerrainMesh(const std::filesystem::path &); struct PointFace { + // NOLINTNEXTLINE(hicpp-explicit-conversions) PointFace(const glm::vec2 p) : point {p} { } + PointFace(const glm::vec2 p, FaceHandle face) : point {p}, _face {face} { } + + PointFace(const glm::vec2 p, const TerrainMesh *); + PointFace(const glm::vec2 p, const TerrainMesh *, FaceHandle start); + const glm::vec2 point; - mutable FaceHandle face {}; + [[nodiscard]] FaceHandle face(const TerrainMesh *) const; + [[nodiscard]] FaceHandle face(const TerrainMesh *, FaceHandle start) const; + + [[nodiscard]] bool + isLocated() const + { + return _face.is_valid(); + } + + private: + mutable FaceHandle _face {}; }; [[nodiscard]] FaceHandle findPoint(glm::vec2) const; @@ -35,7 +51,4 @@ protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; - - bool locate(const PointFace &) const; - bool locate(const PointFace &, FaceHandle start) const; }; -- cgit v1.2.3 From 1117f91396da00fc48158866ff0ffd1883c4cbd1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Nov 2023 20:37:08 +0000 Subject: Generic N-dimensional terrain triangle --- game/terrain2.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'game') diff --git a/game/terrain2.h b/game/terrain2.h index 641d608..c2c3d19 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -1,5 +1,6 @@ #pragma once +#include "collections.h" // IWYU pragma: keep IterableCollection #include #include #include @@ -41,6 +42,19 @@ public: mutable FaceHandle _face {}; }; + template struct Triangle : public glm::vec<3, glm::vec> { + using base = glm::vec<3, glm::vec>; + using base::base; + + template Triangle(const TerrainMesh * m, Range range) + { + assert(std::distance(range.begin(), range.end()) == 3); + std::transform(range.begin(), range.end(), &base::operator[](0), [m](auto vh) { + return m->point(vh); + }); + } + }; + [[nodiscard]] FaceHandle findPoint(glm::vec2) const; [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; -- cgit v1.2.3 From 8a5d36623e4fa261aea16731954c9987dcbb1ef1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Nov 2023 20:38:10 +0000 Subject: Terrain mesh method for getting the height/position at a point on the surface --- game/terrain2.cpp | 11 +++++++++++ game/terrain2.h | 2 ++ 2 files changed, 13 insertions(+) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 8d29143..b129284 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -1,5 +1,7 @@ #include "terrain2.h" #include +#include +#include TerrainMesh::TerrainMesh(const std::filesystem::path & input) { @@ -124,6 +126,15 @@ TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const return f; } +glm::vec3 +TerrainMesh::positionAt(const PointFace & p) const +{ + glm::vec3 out {}; + Triangle<3> t {this, fv_range(p.face(this))}; + glm::intersectLineTriangle(p.point ^ 0.F, up, t[0], t[1], t[2], out); + return p.point ^ out[0]; +} + void TerrainMesh::walk(const PointFace & from, const glm::vec2 to, const std::function & op) const { diff --git a/game/terrain2.h b/game/terrain2.h index c2c3d19..f1d288e 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -58,6 +58,8 @@ public: [[nodiscard]] FaceHandle findPoint(glm::vec2) const; [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; + [[nodiscard]] glm::vec3 positionAt(const PointFace &) const; + void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; -- cgit v1.2.3 From 889baa0abe996976d8717e7e411961804b853465 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 3 Nov 2023 20:10:56 +0000 Subject: Helper to get cartesian position from baricentric on Triangle --- game/terrain2.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'game') diff --git a/game/terrain2.h b/game/terrain2.h index f1d288e..da9f823 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -53,6 +53,13 @@ public: return m->point(vh); }); } + + glm::vec + operator*(glm::vec2 bari) const + { + const auto & t {*this}; + return t[0] + ((t[1] - t[0]) * bari.x) + ((t[2] - t[1]) * bari.y); + } }; [[nodiscard]] FaceHandle findPoint(glm::vec2) const; -- cgit v1.2.3 From 3931bda936aba6ddae26c1d9f1d450781df800e8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 3 Nov 2023 20:12:03 +0000 Subject: Implement terrain intersect ray --- game/terrain2.cpp | 23 +++++++++++++++++++++++ game/terrain2.h | 4 ++++ 2 files changed, 27 insertions(+) (limited to 'game') 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 +TerrainMesh::intersectRay(const Ray & ray) const +{ + return intersectRay(ray, findPoint(ray.start)); +} + +[[nodiscard]] std::optional +TerrainMesh::intersectRay(const Ray & ray, FaceHandle face) const +{ + std::optional 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 & op) const { diff --git a/game/terrain2.h b/game/terrain2.h index da9f823..5539a50 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -1,9 +1,11 @@ #pragma once #include "collections.h" // IWYU pragma: keep IterableCollection +#include "ray.h" #include #include #include +#include #include struct TerrainTraits : public OpenMesh::DefaultTraits { @@ -66,6 +68,8 @@ public: [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; [[nodiscard]] glm::vec3 positionAt(const PointFace &) const; + [[nodiscard]] std::optional intersectRay(const Ray &) const; + [[nodiscard]] std::optional intersectRay(const Ray &, FaceHandle start) const; void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; -- cgit v1.2.3 From a46fded5d93487974ac5f40ff36c8c0f4f7a9db2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Nov 2023 11:21:23 +0000 Subject: Static helper for loading ASCII grid data --- game/terrain2.cpp | 16 ++++++++++------ game/terrain2.h | 5 ++++- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index d1af221..105eb33 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -3,7 +3,8 @@ #include #include -TerrainMesh::TerrainMesh(const std::filesystem::path & input) +TerrainMesh +TerrainMesh::loadFromAsciiGrid(const std::filesystem::path & input) { size_t ncols = 0, nrows = 0, xllcorner = 0, yllcorner = 0, cellsize = 0; std::map properties { @@ -22,11 +23,12 @@ TerrainMesh::TerrainMesh(const std::filesystem::path & input) } std::vector vertices; vertices.reserve(ncols * nrows); + TerrainMesh mesh; for (size_t row = 0; row < nrows; ++row) { for (size_t col = 0; col < ncols; ++col) { float height = 0; f >> height; - vertices.push_back(add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); + vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); } } if (!f.good()) { @@ -34,20 +36,22 @@ TerrainMesh::TerrainMesh(const std::filesystem::path & input) } for (size_t row = 1; row < nrows; ++row) { for (size_t col = 1; col < ncols; ++col) { - add_face({ + mesh.add_face({ vertices[ncols * (row - 1) + (col - 1)], vertices[ncols * (row - 0) + (col - 0)], vertices[ncols * (row - 0) + (col - 1)], }); - add_face({ + mesh.add_face({ vertices[ncols * (row - 1) + (col - 1)], vertices[ncols * (row - 1) + (col - 0)], vertices[ncols * (row - 0) + (col - 0)], }); } } - update_face_normals(); - update_vertex_normals(); + mesh.update_face_normals(); + mesh.update_vertex_normals(); + + return mesh; }; OpenMesh::FaceHandle diff --git a/game/terrain2.h b/game/terrain2.h index 5539a50..69cd380 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -18,8 +18,11 @@ struct TerrainTraits : public OpenMesh::DefaultTraits { }; class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT { +private: + TerrainMesh() = default; + public: - explicit TerrainMesh(const std::filesystem::path &); + static TerrainMesh loadFromAsciiGrid(const std::filesystem::path &); struct PointFace { // NOLINTNEXTLINE(hicpp-explicit-conversions) -- cgit v1.2.3 From c463bce75418a145d319f214571e30df311ff8df Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Nov 2023 12:14:59 +0000 Subject: Calculate and expose the extents of the terrain mesh --- game/terrain2.cpp | 5 +++++ game/terrain2.h | 9 +++++++++ 2 files changed, 14 insertions(+) (limited to 'game') diff --git a/game/terrain2.cpp b/game/terrain2.cpp index 105eb33..0e8b78e 100644 --- a/game/terrain2.cpp +++ b/game/terrain2.cpp @@ -24,10 +24,15 @@ TerrainMesh::loadFromAsciiGrid(const std::filesystem::path & input) std::vector vertices; vertices.reserve(ncols * nrows); TerrainMesh mesh; + mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits::max()}; + mesh.upperExtent + = {xllcorner + (cellsize * ncols), yllcorner + (cellsize * nrows), std::numeric_limits::min()}; for (size_t row = 0; row < nrows; ++row) { for (size_t col = 0; col < ncols; ++col) { float height = 0; f >> height; + mesh.upperExtent.z = std::max(mesh.upperExtent.z, height); + mesh.lowerExtent.z = std::min(mesh.lowerExtent.z, height); vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); } } diff --git a/game/terrain2.h b/game/terrain2.h index 69cd380..eda56d0 100644 --- a/game/terrain2.h +++ b/game/terrain2.h @@ -77,8 +77,17 @@ public: void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; + [[nodiscard]] auto + getExtents() const + { + return std::tie(lowerExtent, upperExtent); + } + protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; + +private: + glm::vec3 lowerExtent {}, upperExtent {}; }; -- cgit v1.2.3 From 582ac127f763f512c45f35e17b768487e3b51796 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Nov 2023 17:07:58 +0000 Subject: Rename TerrainMesh to GeoData to drop inplace --- game/geoData.cpp | 340 +++++++++++++++++++++++++++++------------------------- game/geoData.h | 115 +++++++++++------- game/terrain.cpp | 65 +++-------- game/terrain2.cpp | 221 ----------------------------------- game/terrain2.h | 93 --------------- 5 files changed, 274 insertions(+), 560 deletions(-) delete mode 100644 game/terrain2.cpp delete mode 100644 game/terrain2.h (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 34a1030..d32bf8c 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -1,214 +1,238 @@ #include "geoData.h" -#include "gfx/image.h" -#include -#include -#include -#include +#include #include -#include -#include #include -#include -#include -#include -#include -#include - -GeoData::GeoData(Limits l, float s) : - limit {std::move(l)}, size {(limit.second - limit.first) + 1}, scale {s}, nodes {[this]() { - return (static_cast(size.x * size.y)); - }()} -{ -} -void -GeoData::generateRandom() +GeoData +GeoData::loadFromAsciiGrid(const std::filesystem::path & input) { - // We acknowledge this is terrible :) - - // Add hills - std::mt19937 gen(std::random_device {}()); - std::uniform_int_distribution<> rxpos(limit.first.x + 2, limit.second.x - 2), - rypos(limit.first.y + 2, limit.second.y - 2); - std::uniform_int_distribution<> rsize(10, 30); - std::uniform_real_distribution rheight(1, 3); - for (int h = 0; h < 500;) { - const glm::ivec2 hpos {rxpos(gen), rypos(gen)}; - const glm::ivec2 hsize {rsize(gen), rsize(gen)}; - if (const auto lim1 = hpos - hsize; lim1.x > limit.first.x && lim1.y > limit.first.y) { - if (const auto lim2 = hpos + hsize; lim2.x < limit.second.x && lim2.y < limit.second.y) { - const auto height = rheight(gen); - const glm::ivec2 hsizesqrd {hsize.x * hsize.x, hsize.y * hsize.y}; - for (auto y = lim1.y; y < lim2.y; y += 1) { - for (auto x = lim1.x; x < lim2.x; x += 1) { - const auto dist {hpos - glm::ivec2 {x, y}}; - const glm::ivec2 distsqrd {dist.x * dist.x, dist.y * dist.y}; - const auto out {ratio(sq(x - hpos.x), sq(hsize.x)) + ratio(sq(y - hpos.y), sq(hsize.y))}; - if (out <= 1.0F) { - auto & node {nodes[at({x, y})]}; - const auto m {1.F / (7.F * out - 8.F) + 1.F}; - node.height += height * m; - } - } - } - h += 1; - } + size_t ncols = 0, nrows = 0, xllcorner = 0, yllcorner = 0, cellsize = 0; + std::map properties { + {"ncols", &ncols}, + {"nrows", &nrows}, + {"xllcorner", &xllcorner}, + {"yllcorner", &yllcorner}, + {"cellsize", &cellsize}, + }; + std::ifstream f {input}; + while (!properties.empty()) { + std::string property; + f >> property; + f >> *properties.at(property); + properties.erase(property); + } + std::vector vertices; + vertices.reserve(ncols * nrows); + GeoData mesh; + mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits::max()}; + mesh.upperExtent + = {xllcorner + (cellsize * ncols), yllcorner + (cellsize * nrows), std::numeric_limits::min()}; + for (size_t row = 0; row < nrows; ++row) { + for (size_t col = 0; col < ncols; ++col) { + float height = 0; + f >> height; + mesh.upperExtent.z = std::max(mesh.upperExtent.z, height); + mesh.lowerExtent.z = std::min(mesh.lowerExtent.z, height); + vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); } } -} + if (!f.good()) { + throw std::runtime_error("Couldn't read terrain file"); + } + for (size_t row = 1; row < nrows; ++row) { + for (size_t col = 1; col < ncols; ++col) { + mesh.add_face({ + vertices[ncols * (row - 1) + (col - 1)], + vertices[ncols * (row - 0) + (col - 0)], + vertices[ncols * (row - 0) + (col - 1)], + }); + mesh.add_face({ + vertices[ncols * (row - 1) + (col - 1)], + vertices[ncols * (row - 1) + (col - 0)], + vertices[ncols * (row - 0) + (col - 0)], + }); + } + } + mesh.update_face_normals(); + mesh.update_vertex_normals(); -void -GeoData::loadFromImages(const std::filesystem::path & fileName, float scale_) -{ - const Image map {fileName.c_str(), STBI_grey}; - size = {map.width, map.height}; - limit = {{0, 0}, size - glm::uvec2 {1, 1}}; - const auto points {size.x * size.y}; - scale = scale_; - nodes.resize(points); - - std::transform(map.data.data(), map.data.data() + points, nodes.begin(), [](auto d) { - return Node {(d * 0.1F) - 1.5F}; - }); -} + return mesh; +}; -GeoData::Quad -GeoData::quad(glm::vec2 wcoord) const +GeoData +GeoData::createFlat(glm::vec2 lower, glm::vec2 upper, float h) { - constexpr static const std::array corners {{{0, 0}, {0, 1}, {1, 0}, {1, 1}}}; - return transform_array(transform_array(corners, - [coord = (wcoord / scale)](const auto c) { - return glm::vec2 {std::floor(coord.x), std::floor(coord.y)} + c; - }), - [this](const auto c) { - return (c * scale) || nodes[at(c)].height; - }); -} + GeoData mesh; -glm::vec3 -GeoData::positionAt(const glm::vec2 wcoord) const -{ - const auto point {quad(wcoord)}; - const glm::vec2 frac = (wcoord - !point.front()) / scale; - auto edge = [&point, &frac](auto offset) { - return point[offset].z + ((point[offset + 2].z - point[offset].z) * frac.x); - }; - const auto heightFloor = edge(0U), heightCeil = edge(1U), - heightMid = heightFloor + ((heightCeil - heightFloor) * frac.y); + mesh.lowerExtent = lower ^ h; + mesh.upperExtent = upper ^ h; + + const auto ll = mesh.add_vertex({lower.x, lower.y, h}), lu = mesh.add_vertex({lower.x, upper.y, h}), + ul = mesh.add_vertex({upper.x, lower.y, h}), uu = mesh.add_vertex({upper.x, upper.y, h}); + + mesh.add_face(ll, uu, lu); + mesh.add_face(ll, ul, uu); - return wcoord || heightMid; + mesh.update_face_normals(); + mesh.update_vertex_normals(); + + return mesh; } -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)} +OpenMesh::FaceHandle +GeoData::findPoint(glm::vec2 p) const { + return findPoint(p, *faces_begin()); } -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} +GeoData::PointFace::PointFace(const glm::vec2 p, const GeoData * mesh) : PointFace {p, mesh, *mesh->faces_begin()} { } + +GeoData::PointFace::PointFace(const glm::vec2 p, const GeoData * mesh, FaceHandle start) : + PointFace {p, mesh->findPoint(p, start)} { } -std::pair -GeoData::RayTracer::byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_t axis) +GeoData::FaceHandle +GeoData::PointFace::face(const GeoData * mesh, FaceHandle start) const { - 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]}; + if (_face.is_valid()) { + assert(mesh->triangleContainsPoint(point, _face)); + return _face; } else { - return {-1, (p0[axis] - std::floor(p0[axis])) * d[1 - axis]}; + return (_face = mesh->findPoint(point, start)); } } -glm::vec2 -GeoData::RayTracer::next() +GeoData::FaceHandle +GeoData::PointFace::face(const GeoData * mesh) const { - const glm::vec2 cur {p}; + return face(mesh, *mesh->faces_begin()); +} + +namespace { + [[nodiscard]] constexpr inline bool + pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) + { + return (e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x); + } - 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]; + static_assert(pointLeftOfLine({1, 2}, {1, 1}, {2, 2})); + static_assert(pointLeftOfLine({2, 1}, {2, 2}, {1, 1})); + static_assert(pointLeftOfLine({2, 2}, {1, 2}, {2, 1})); + static_assert(pointLeftOfLine({1, 1}, {2, 1}, {1, 2})); - return cur; + [[nodiscard]] constexpr inline bool + linesCross(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2) + { + return pointLeftOfLine(a2, b1, b2) && pointLeftOfLine(a1, b2, b1) && pointLeftOfLine(b1, a1, a2) + && pointLeftOfLine(b2, a2, a1); + } + + static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1})); } -std::optional -GeoData::intersectRay(const Ray & ray) const +OpenMesh::FaceHandle +GeoData::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) 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]); + ConstFaceVertexIter vertices; + while (f.is_valid() && !triangleContainsPoint(p, vertices = cfv_iter(f))) { + for (auto next = cfh_iter(f); next.is_valid(); ++next) { + f = opposite_face_handle(*next); + if (f.is_valid()) { + const auto e1 = point(to_vertex_handle(*next)); + const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); + if (pointLeftOfLine(p, e1, e2)) { + break; } } - } - catch (std::range_error &) { - const auto rel = n / !ray.direction; - if (rel.x > 0 && rel.y > 0) { - return {}; - } + f.reset(); } } + return f; +} - return {}; +glm::vec3 +GeoData::positionAt(const PointFace & p) const +{ + glm::vec3 out {}; + Triangle<3> t {this, fv_range(p.face(this))}; + glm::intersectLineTriangle(p.point ^ 0.F, up, t[0], t[1], t[2], out); + return p.point ^ out[0]; } -unsigned int -GeoData::at(glm::ivec2 coord) const +[[nodiscard]] std::optional +GeoData::intersectRay(const Ray & ray) const { - if (coord.x < limit.first.x || coord.x > limit.second.x || coord.y < limit.first.y || coord.y > limit.second.y) { - throw std::range_error {"Coordinates outside GeoData limits"}; - } - const glm::uvec2 offset = coord - limit.first; - return offset.x + (offset.y * size.x); + return intersectRay(ray, findPoint(ray.start)); } -unsigned int -GeoData::at(int x, int y) const +[[nodiscard]] std::optional +GeoData::intersectRay(const Ray & ray, FaceHandle face) const { - return at({x, y}); + std::optional 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; } -GeoData::Limits -GeoData::getLimit() const +void +GeoData::walk(const PointFace & from, const glm::vec2 to, const std::function & op) const { - return limit; + walkUntil(from, to, [&op](const auto & fh) { + op(fh); + return false; + }); } -float -GeoData::getScale() const +void +GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const +{ + assert(from.face(this).is_valid()); // TODO replace with a boundary search + auto f = from.face(this); + FaceHandle previousFace; + while (f.is_valid() && !op(f)) { + for (auto next = cfh_iter(f); next.is_valid(); ++next) { + f = opposite_face_handle(*next); + if (f.is_valid() && f != previousFace) { + const auto e1 = point(to_vertex_handle(*next)); + const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); + if (linesCross(from.point, to, e1, e2)) { + previousFace = f; + break; + } + } + f.reset(); + } + } +} + +bool +GeoData::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) { - return scale; + const auto det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); + + return det * ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0 + && det * ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x)) >= 0 + && det * ((a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x)) >= 0; } -glm::uvec2 -GeoData::getSize() const +bool +GeoData::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const { - return size; + return triangleContainsPoint(p, cfv_iter(face)); } -std::span -GeoData::getNodes() const +bool +GeoData::triangleContainsPoint(const glm::vec2 p, ConstFaceVertexIter vertices) const { - return nodes; + return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++)); } diff --git a/game/geoData.h b/game/geoData.h index f433a5c..3a4f98e 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -1,61 +1,94 @@ #pragma once -#include +#include "collections.h" // IWYU pragma: keep IterableCollection +#include "ray.h" +#include #include -#include +#include #include -#include -#include -#include +#include -class Ray; +struct GeoDataTraits : public OpenMesh::DefaultTraits { + FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + EdgeAttributes(OpenMesh::Attributes::Status); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + HalfedgeAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + using Point = glm::vec3; + using Normal = glm::vec3; +}; + +class GeoData : public OpenMesh::TriMesh_ArrayKernelT { +private: + GeoData() = default; -class GeoData { public: - struct Node { - float height {-1.5F}; - }; - using Quad = std::array; + static GeoData loadFromAsciiGrid(const std::filesystem::path &); + static GeoData createFlat(glm::vec2 lower, glm::vec2, float h); - using Limits = std::pair; + struct PointFace { + // NOLINTNEXTLINE(hicpp-explicit-conversions) + PointFace(const glm::vec2 p) : point {p} { } - explicit GeoData(Limits limit, float scale = 10.F); + PointFace(const glm::vec2 p, FaceHandle face) : point {p}, _face {face} { } - void generateRandom(); - void loadFromImages(const std::filesystem::path &, float scale); + PointFace(const glm::vec2 p, const GeoData *); + PointFace(const glm::vec2 p, const GeoData *, FaceHandle start); - [[nodiscard]] glm::vec3 positionAt(glm::vec2) const; - [[nodiscard]] std::optional intersectRay(const Ray &) const; + const glm::vec2 point; + [[nodiscard]] FaceHandle face(const GeoData *) const; + [[nodiscard]] FaceHandle face(const GeoData *, FaceHandle start) const; - [[nodiscard]] unsigned int at(glm::ivec2) const; - [[nodiscard]] unsigned int at(int x, int y) const; - [[nodiscard]] Quad quad(glm::vec2) const; + [[nodiscard]] bool + isLocated() const + { + return _face.is_valid(); + } - [[nodiscard]] Limits getLimit() const; - [[nodiscard]] glm::uvec2 getSize() const; - [[nodiscard]] float getScale() const; - [[nodiscard]] std::span getNodes() const; + private: + mutable FaceHandle _face {}; + }; - class RayTracer { - public: - RayTracer(glm::vec2 p0, glm::vec2 p1); + template struct Triangle : public glm::vec<3, glm::vec> { + using base = glm::vec<3, glm::vec>; + using base::base; - glm::vec2 next(); + template Triangle(const GeoData * m, Range range) + { + assert(std::distance(range.begin(), range.end()) == 3); + std::transform(range.begin(), range.end(), &base::operator[](0), [m](auto vh) { + return m->point(vh); + }); + } - 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; - glm::vec2 inc; + glm::vec + operator*(glm::vec2 bari) const + { + const auto & t {*this}; + return t[0] + ((t[1] - t[0]) * bari.x) + ((t[2] - t[1]) * bari.y); + } }; + [[nodiscard]] FaceHandle findPoint(glm::vec2) const; + [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; + + [[nodiscard]] glm::vec3 positionAt(const PointFace &) const; + [[nodiscard]] std::optional intersectRay(const Ray &) const; + [[nodiscard]] std::optional intersectRay(const Ray &, FaceHandle start) const; + + void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; + void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; + + [[nodiscard]] auto + getExtents() const + { + return std::tie(lowerExtent, upperExtent); + } + protected: - Limits limit {}; // Base grid limits first(x,y) -> second(x,y) - glm::uvec2 size {}; - float scale {1}; - std::vector nodes; + [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); + [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; + [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; + +private: + glm::vec3 lowerExtent {}, upperExtent {}; }; diff --git a/game/terrain.cpp b/game/terrain.cpp index f3bec1e..5bb8abd 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -2,7 +2,6 @@ #include "game/geoData.h" #include "gfx/models/texture.h" #include -#include #include #include #include @@ -18,8 +17,8 @@ #include #include -Terrain::Terrain(std::shared_ptr gd) : - geoData {std::move(gd)}, grass {Texture::cachedTexture.get("grass.png")}, +Terrain::Terrain(std::shared_ptr tm) : + geoData {std::move(tm)}, grass {Texture::cachedTexture.get("grass.png")}, water {Texture::cachedTexture.get("water.png")} { generateMeshes(); @@ -29,51 +28,23 @@ void Terrain::generateMeshes() { std::vector indices; - const auto isize = geoData->getSize() - glm::uvec2 {1, 1}; - indices.reserve(static_cast(isize.x * isize.y) * 6); - - const auto limit = geoData->getLimit(); - // Indices - constexpr std::array indices_offsets {{ - {0, 0}, - {1, 0}, - {1, 1}, - {0, 0}, - {1, 1}, - {0, 1}, - }}; - for (auto y = limit.first.y; y < limit.second.y; y += 1) { - for (auto x = limit.first.x; x < limit.second.x; x += 1) { - std::transform(indices_offsets.begin(), indices_offsets.end(), std::back_inserter(indices), - [this, x, y](const auto off) { - return geoData->at(x + off.x, y + off.y); - }); - } - } - - const auto nodes = geoData->getNodes(); - const auto scale = geoData->getScale(); + indices.reserve(geoData->n_faces() * 3); std::vector vertices; - vertices.reserve(nodes.size()); - // Positions - for (auto y = limit.first.y; y <= limit.second.y; y += 1) { - for (auto x = limit.first.x; x <= limit.second.x; x += 1) { - const glm::vec2 xy {x, y}; - vertices.emplace_back((xy * scale) ^ nodes[geoData->at(x, y)].height, xy, ::up); - } - } - // Normals - const glm::uvec2 size = geoData->getSize(); - for (auto y = limit.first.y + 1; y < limit.second.y; y += 1) { - for (auto x = limit.first.x + 1; x < limit.second.x; x += 1) { - const auto n {geoData->at(x, y)}; - const auto a = vertices[n - 1].pos; - const auto b = vertices[n - size.x].pos; - const auto c = vertices[n + 1].pos; - const auto d = vertices[n + size.x].pos; - vertices[n].normal = -glm::normalize(glm::cross(b - d, a - c)); - } - } + vertices.reserve(geoData->n_vertices()); + std::map vertexIndex; + std::transform(geoData->vertices_begin(), geoData->vertices_end(), std::back_inserter(vertices), + [this, &vertexIndex](const GeoData::VertexHandle v) { + vertexIndex.emplace(v, vertexIndex.size()); + const glm::vec3 p = geoData->point(v); + return Vertex {p, p / 10.F, geoData->normal(v)}; + }); + std::for_each( + geoData->faces_begin(), geoData->faces_end(), [this, &vertexIndex, &indices](const GeoData::FaceHandle f) { + std::transform(geoData->fv_begin(f), geoData->fv_end(f), std::back_inserter(indices), + [&vertexIndex](const GeoData::VertexHandle v) { + return vertexIndex[v]; + }); + }); meshes.create(vertices, indices); } diff --git a/game/terrain2.cpp b/game/terrain2.cpp deleted file mode 100644 index 0e8b78e..0000000 --- a/game/terrain2.cpp +++ /dev/null @@ -1,221 +0,0 @@ -#include "terrain2.h" -#include -#include -#include - -TerrainMesh -TerrainMesh::loadFromAsciiGrid(const std::filesystem::path & input) -{ - size_t ncols = 0, nrows = 0, xllcorner = 0, yllcorner = 0, cellsize = 0; - std::map properties { - {"ncols", &ncols}, - {"nrows", &nrows}, - {"xllcorner", &xllcorner}, - {"yllcorner", &yllcorner}, - {"cellsize", &cellsize}, - }; - std::ifstream f {input}; - while (!properties.empty()) { - std::string property; - f >> property; - f >> *properties.at(property); - properties.erase(property); - } - std::vector vertices; - vertices.reserve(ncols * nrows); - TerrainMesh mesh; - mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits::max()}; - mesh.upperExtent - = {xllcorner + (cellsize * ncols), yllcorner + (cellsize * nrows), std::numeric_limits::min()}; - for (size_t row = 0; row < nrows; ++row) { - for (size_t col = 0; col < ncols; ++col) { - float height = 0; - f >> height; - mesh.upperExtent.z = std::max(mesh.upperExtent.z, height); - mesh.lowerExtent.z = std::min(mesh.lowerExtent.z, height); - vertices.push_back(mesh.add_vertex({xllcorner + (col * cellsize), yllcorner + (row * cellsize), height})); - } - } - if (!f.good()) { - throw std::runtime_error("Couldn't read terrain file"); - } - for (size_t row = 1; row < nrows; ++row) { - for (size_t col = 1; col < ncols; ++col) { - mesh.add_face({ - vertices[ncols * (row - 1) + (col - 1)], - vertices[ncols * (row - 0) + (col - 0)], - vertices[ncols * (row - 0) + (col - 1)], - }); - mesh.add_face({ - vertices[ncols * (row - 1) + (col - 1)], - vertices[ncols * (row - 1) + (col - 0)], - vertices[ncols * (row - 0) + (col - 0)], - }); - } - } - mesh.update_face_normals(); - mesh.update_vertex_normals(); - - return mesh; -}; - -OpenMesh::FaceHandle -TerrainMesh::findPoint(glm::vec2 p) const -{ - return findPoint(p, *faces_begin()); -} - -TerrainMesh::PointFace::PointFace(const glm::vec2 p, const TerrainMesh * mesh) : - PointFace {p, mesh, *mesh->faces_begin()} -{ -} - -TerrainMesh::PointFace::PointFace(const glm::vec2 p, const TerrainMesh * mesh, FaceHandle start) : - PointFace {p, mesh->findPoint(p, start)} -{ -} - -TerrainMesh::FaceHandle -TerrainMesh::PointFace::face(const TerrainMesh * mesh, FaceHandle start) const -{ - if (_face.is_valid()) { - assert(mesh->triangleContainsPoint(point, _face)); - return _face; - } - else { - return (_face = mesh->findPoint(point, start)); - } -} - -TerrainMesh::FaceHandle -TerrainMesh::PointFace::face(const TerrainMesh * mesh) const -{ - return face(mesh, *mesh->faces_begin()); -} - -namespace { - [[nodiscard]] constexpr inline bool - pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) - { - return (e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x); - } - - static_assert(pointLeftOfLine({1, 2}, {1, 1}, {2, 2})); - static_assert(pointLeftOfLine({2, 1}, {2, 2}, {1, 1})); - static_assert(pointLeftOfLine({2, 2}, {1, 2}, {2, 1})); - static_assert(pointLeftOfLine({1, 1}, {2, 1}, {1, 2})); - - [[nodiscard]] constexpr inline bool - linesCross(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2) - { - return pointLeftOfLine(a2, b1, b2) && pointLeftOfLine(a1, b2, b1) && pointLeftOfLine(b1, a1, a2) - && pointLeftOfLine(b2, a2, a1); - } - - static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1})); -} - -OpenMesh::FaceHandle -TerrainMesh::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const -{ - ConstFaceVertexIter vertices; - while (f.is_valid() && !triangleContainsPoint(p, vertices = cfv_iter(f))) { - for (auto next = cfh_iter(f); next.is_valid(); ++next) { - f = opposite_face_handle(*next); - if (f.is_valid()) { - const auto e1 = point(to_vertex_handle(*next)); - const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); - if (pointLeftOfLine(p, e1, e2)) { - break; - } - } - f.reset(); - } - } - return f; -} - -glm::vec3 -TerrainMesh::positionAt(const PointFace & p) const -{ - glm::vec3 out {}; - Triangle<3> t {this, fv_range(p.face(this))}; - glm::intersectLineTriangle(p.point ^ 0.F, up, t[0], t[1], t[2], out); - return p.point ^ out[0]; -} - -[[nodiscard]] std::optional -TerrainMesh::intersectRay(const Ray & ray) const -{ - return intersectRay(ray, findPoint(ray.start)); -} - -[[nodiscard]] std::optional -TerrainMesh::intersectRay(const Ray & ray, FaceHandle face) const -{ - std::optional 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 & op) const -{ - walkUntil(from, to, [&op](const auto & fh) { - op(fh); - return false; - }); -} - -void -TerrainMesh::walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const -{ - assert(from.face(this).is_valid()); // TODO replace with a boundary search - auto f = from.face(this); - FaceHandle previousFace; - while (f.is_valid() && !op(f)) { - for (auto next = cfh_iter(f); next.is_valid(); ++next) { - f = opposite_face_handle(*next); - if (f.is_valid() && f != previousFace) { - const auto e1 = point(to_vertex_handle(*next)); - const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); - if (linesCross(from.point, to, e1, e2)) { - previousFace = f; - break; - } - } - f.reset(); - } - } -} - -bool -TerrainMesh::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) -{ - const auto det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); - - return det * ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0 - && det * ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x)) >= 0 - && det * ((a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x)) >= 0; -} - -bool -TerrainMesh::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const -{ - return triangleContainsPoint(p, cfv_iter(face)); -} - -bool -TerrainMesh::triangleContainsPoint(const glm::vec2 p, ConstFaceVertexIter vertices) const -{ - return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++)); -} diff --git a/game/terrain2.h b/game/terrain2.h deleted file mode 100644 index eda56d0..0000000 --- a/game/terrain2.h +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#include "collections.h" // IWYU pragma: keep IterableCollection -#include "ray.h" -#include -#include -#include -#include -#include - -struct TerrainTraits : public OpenMesh::DefaultTraits { - FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - EdgeAttributes(OpenMesh::Attributes::Status); - VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - HalfedgeAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - using Point = glm::vec3; - using Normal = glm::vec3; -}; - -class TerrainMesh : public OpenMesh::TriMesh_ArrayKernelT { -private: - TerrainMesh() = default; - -public: - static TerrainMesh loadFromAsciiGrid(const std::filesystem::path &); - - struct PointFace { - // NOLINTNEXTLINE(hicpp-explicit-conversions) - PointFace(const glm::vec2 p) : point {p} { } - - PointFace(const glm::vec2 p, FaceHandle face) : point {p}, _face {face} { } - - PointFace(const glm::vec2 p, const TerrainMesh *); - PointFace(const glm::vec2 p, const TerrainMesh *, FaceHandle start); - - const glm::vec2 point; - [[nodiscard]] FaceHandle face(const TerrainMesh *) const; - [[nodiscard]] FaceHandle face(const TerrainMesh *, FaceHandle start) const; - - [[nodiscard]] bool - isLocated() const - { - return _face.is_valid(); - } - - private: - mutable FaceHandle _face {}; - }; - - template struct Triangle : public glm::vec<3, glm::vec> { - using base = glm::vec<3, glm::vec>; - using base::base; - - template Triangle(const TerrainMesh * m, Range range) - { - assert(std::distance(range.begin(), range.end()) == 3); - std::transform(range.begin(), range.end(), &base::operator[](0), [m](auto vh) { - return m->point(vh); - }); - } - - glm::vec - operator*(glm::vec2 bari) const - { - const auto & t {*this}; - return t[0] + ((t[1] - t[0]) * bari.x) + ((t[2] - t[1]) * bari.y); - } - }; - - [[nodiscard]] FaceHandle findPoint(glm::vec2) const; - [[nodiscard]] FaceHandle findPoint(glm::vec2, FaceHandle start) const; - - [[nodiscard]] glm::vec3 positionAt(const PointFace &) const; - [[nodiscard]] std::optional intersectRay(const Ray &) const; - [[nodiscard]] std::optional intersectRay(const Ray &, FaceHandle start) const; - - void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; - void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; - - [[nodiscard]] auto - getExtents() const - { - return std::tie(lowerExtent, upperExtent); - } - -protected: - [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); - [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; - [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; - -private: - glm::vec3 lowerExtent {}, upperExtent {}; -}; -- cgit v1.2.3 From 5ed36466b48aa347277c70422eb00bee264a5c8b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Nov 2023 17:07:58 +0000 Subject: Simplify some logic with the triangle construct --- game/geoData.cpp | 26 ++++++++++---------------- game/geoData.h | 10 ++++++++-- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index d32bf8c..5e49c94 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -135,8 +135,7 @@ namespace { OpenMesh::FaceHandle GeoData::findPoint(glm::vec2 p, OpenMesh::FaceHandle f) const { - ConstFaceVertexIter vertices; - while (f.is_valid() && !triangleContainsPoint(p, vertices = cfv_iter(f))) { + while (f.is_valid() && !triangleContainsPoint(p, triangle<2>(f))) { for (auto next = cfh_iter(f); next.is_valid(); ++next) { f = opposite_face_handle(*next); if (f.is_valid()) { @@ -156,7 +155,7 @@ glm::vec3 GeoData::positionAt(const PointFace & p) const { glm::vec3 out {}; - Triangle<3> t {this, fv_range(p.face(this))}; + const auto t = triangle<3>(p.face(this)); glm::intersectLineTriangle(p.point ^ 0.F, up, t[0], t[1], t[2], out); return p.point ^ out[0]; } @@ -174,7 +173,7 @@ GeoData::intersectRay(const Ray & ray, FaceHandle face) const 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)}; + const auto t = triangle<3>(face); if (glm::intersectRayTriangle(ray.start, ray.direction, t[0], t[1], t[2], bari, dist)) { out = t * bari; return true; @@ -216,23 +215,18 @@ GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::functi } bool -GeoData::triangleContainsPoint(const glm::vec2 p, const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) +GeoData::triangleContainsPoint(const glm::vec2 p, const Triangle<2> & t) { - const auto det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); + const auto mul = [](const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) { + return (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x); + }; + const auto det = mul(t[1], t[0], t[2]); - return det * ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0 - && det * ((c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x)) >= 0 - && det * ((a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x)) >= 0; + return det * mul(t[1], t[0], p) >= 0 && det * mul(t[2], t[1], p) >= 0 && det * mul(t[0], t[2], p) >= 0; } bool GeoData::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const { - return triangleContainsPoint(p, cfv_iter(face)); -} - -bool -GeoData::triangleContainsPoint(const glm::vec2 p, ConstFaceVertexIter vertices) const -{ - return triangleContainsPoint(p, point(*vertices++), point(*vertices++), point(*vertices++)); + return triangleContainsPoint(p, triangle<2>(face)); } diff --git a/game/geoData.h b/game/geoData.h index 3a4f98e..b8561d0 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -85,9 +85,15 @@ public: } protected: - [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const glm::vec2, const glm::vec2, const glm::vec2); + template + [[nodiscard]] Triangle + triangle(FaceHandle f) const + { + return {this, fv_range(f)}; + } + + [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const Triangle<2> &); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; - [[nodiscard]] bool triangleContainsPoint(const glm::vec2, ConstFaceVertexIter) const; private: glm::vec3 lowerExtent {}, upperExtent {}; -- cgit v1.2.3 From f53816a8fd9ec15ef03b5bf45472eda46e53c1ad Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Nov 2023 21:05:35 +0000 Subject: Refactor to further simplify line/point operations Adds another perf test --- game/geoData.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 5e49c94..c58658a 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -111,12 +111,16 @@ GeoData::PointFace::face(const GeoData * mesh) const } namespace { - [[nodiscard]] constexpr inline bool - pointLeftOfLine(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) + template typename Op> + [[nodiscard]] constexpr inline auto + pointLineOp(const glm::vec2 p, const glm::vec2 e1, const glm::vec2 e2) { - return (e2.x - e1.x) * (p.y - e1.y) > (e2.y - e1.y) * (p.x - e1.x); + return Op {}((e2.x - e1.x) * (p.y - e1.y), (e2.y - e1.y) * (p.x - e1.x)); } + constexpr auto pointLeftOfLine = pointLineOp; + constexpr auto pointLeftOfOrOnLine = pointLineOp; + static_assert(pointLeftOfLine({1, 2}, {1, 1}, {2, 2})); static_assert(pointLeftOfLine({2, 1}, {2, 2}, {1, 1})); static_assert(pointLeftOfLine({2, 2}, {1, 2}, {2, 1})); @@ -217,12 +221,8 @@ GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::functi bool GeoData::triangleContainsPoint(const glm::vec2 p, const Triangle<2> & t) { - const auto mul = [](const glm::vec2 a, const glm::vec2 b, const glm::vec2 c) { - return (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x); - }; - const auto det = mul(t[1], t[0], t[2]); - - return det * mul(t[1], t[0], p) >= 0 && det * mul(t[2], t[1], p) >= 0 && det * mul(t[0], t[2], p) >= 0; + return pointLeftOfOrOnLine(p, t[0], t[1]) && pointLeftOfOrOnLine(p, t[1], t[2]) + && pointLeftOfOrOnLine(p, t[2], t[0]); } bool -- cgit v1.2.3 From 04078dbb3fe4acd09d150e016c2b2e0d5d036950 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 Nov 2023 13:07:32 +0000 Subject: Add methods to walk the boundary of the terrain mesh --- game/geoData.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ game/geoData.h | 6 ++++++ 2 files changed, 51 insertions(+) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index c58658a..f9c11bf 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -218,6 +218,43 @@ GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::functi } } +void +GeoData::boundaryWalk(const std::function & op) const +{ + boundaryWalk(op, findBoundaryStart()); +} + +void +GeoData::boundaryWalk(const std::function & op, HalfedgeHandle start) const +{ + assert(is_boundary(start)); + boundaryWalkUntil( + [&op](auto heh) { + op(heh); + return false; + }, + start); +} + +void +GeoData::boundaryWalkUntil(const std::function & op) const +{ + boundaryWalkUntil(op, findBoundaryStart()); +} + +void +GeoData::boundaryWalkUntil(const std::function & op, HalfedgeHandle start) const +{ + assert(is_boundary(start)); + if (!op(start)) { + for (auto heh = next_halfedge_handle(start); heh != start; heh = next_halfedge_handle(heh)) { + if (op(heh)) { + break; + } + } + } +} + bool GeoData::triangleContainsPoint(const glm::vec2 p, const Triangle<2> & t) { @@ -230,3 +267,11 @@ GeoData::triangleContainsPoint(const glm::vec2 p, FaceHandle face) const { return triangleContainsPoint(p, triangle<2>(face)); } + +GeoData::HalfedgeHandle +GeoData::findBoundaryStart() const +{ + return *std::find_if(halfedges_begin(), halfedges_end(), [this](const auto heh) { + return is_boundary(heh); + }); +} diff --git a/game/geoData.h b/game/geoData.h index b8561d0..15d7e24 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -78,6 +78,11 @@ public: void walk(const PointFace & from, const glm::vec2 to, const std::function & op) const; void walkUntil(const PointFace & from, const glm::vec2 to, const std::function & op) const; + void boundaryWalk(const std::function &) const; + void boundaryWalk(const std::function &, HalfedgeHandle start) const; + void boundaryWalkUntil(const std::function &) const; + void boundaryWalkUntil(const std::function &, HalfedgeHandle start) const; + [[nodiscard]] auto getExtents() const { @@ -94,6 +99,7 @@ protected: [[nodiscard]] static bool triangleContainsPoint(const glm::vec2, const Triangle<2> &); [[nodiscard]] bool triangleContainsPoint(const glm::vec2, FaceHandle) const; + [[nodiscard]] HalfedgeHandle findBoundaryStart() const; private: glm::vec3 lowerExtent {}, upperExtent {}; -- cgit v1.2.3 From eb5dea3d035c61327543d7ab527d897ef3768570 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 Nov 2023 14:21:14 +0000 Subject: Fix and split linesCross Previous tested they crossed in a specific direction, which is wrong but useful. Adds linesCrossLtR for this use case. --- game/geoData.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index f9c11bf..78f753f 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -128,12 +128,23 @@ namespace { [[nodiscard]] constexpr inline bool linesCross(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2) + { + return (pointLeftOfLine(a2, b1, b2) == pointLeftOfLine(a1, b2, b1)) + && (pointLeftOfLine(b1, a1, a2) == pointLeftOfLine(b2, a2, a1)); + } + + static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1})); + static_assert(linesCross({2, 2}, {1, 1}, {1, 2}, {2, 1})); + + [[nodiscard]] constexpr inline bool + linesCrossLtR(const glm::vec2 a1, const glm::vec2 a2, const glm::vec2 b1, const glm::vec2 b2) { return pointLeftOfLine(a2, b1, b2) && pointLeftOfLine(a1, b2, b1) && pointLeftOfLine(b1, a1, a2) && pointLeftOfLine(b2, a2, a1); } - static_assert(linesCross({1, 1}, {2, 2}, {1, 2}, {2, 1})); + static_assert(linesCrossLtR({1, 1}, {2, 2}, {1, 2}, {2, 1})); + static_assert(!linesCrossLtR({2, 2}, {1, 1}, {1, 2}, {2, 1})); } OpenMesh::FaceHandle @@ -208,7 +219,7 @@ GeoData::walkUntil(const PointFace & from, const glm::vec2 to, const std::functi if (f.is_valid() && f != previousFace) { const auto e1 = point(to_vertex_handle(*next)); const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(*next))); - if (linesCross(from.point, to, e1, e2)) { + if (linesCrossLtR(from.point, to, e1, e2)) { previousFace = f; break; } -- cgit v1.2.3 From acca54f3a12225454633dca2d56506c0dcebb653 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 Nov 2023 15:37:13 +0000 Subject: Fix calculating extents --- game/geoData.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 78f753f..781b41e 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -25,8 +25,8 @@ GeoData::loadFromAsciiGrid(const std::filesystem::path & input) vertices.reserve(ncols * nrows); GeoData mesh; mesh.lowerExtent = {xllcorner, yllcorner, std::numeric_limits::max()}; - mesh.upperExtent - = {xllcorner + (cellsize * ncols), yllcorner + (cellsize * nrows), std::numeric_limits::min()}; + mesh.upperExtent = {xllcorner + (cellsize * (ncols - 1)), yllcorner + (cellsize * (nrows - 1)), + std::numeric_limits::min()}; for (size_t row = 0; row < nrows; ++row) { for (size_t col = 0; col < ncols; ++col) { float height = 0; -- cgit v1.2.3 From 58402765133fab24d08b64f3752553bd2b8393b9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 5 Nov 2023 15:52:02 +0000 Subject: Fix todo for handling a terrain walk from outside the mesh --- game/geoData.cpp | 22 ++++++++++++++++++++-- game/geoData.h | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 781b41e..61dedda 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -210,8 +210,10 @@ GeoData::walk(const PointFace & from, const glm::vec2 to, const std::function & op) const { - assert(from.face(this).is_valid()); // TODO replace with a boundary search auto f = from.face(this); + if (!f.is_valid()) { + f = opposite_face_handle(findEntry(from.point, to)); + } FaceHandle previousFace; while (f.is_valid() && !op(f)) { for (auto next = cfh_iter(f); next.is_valid(); ++next) { @@ -241,7 +243,7 @@ GeoData::boundaryWalk(const std::function & op, HalfedgeHa assert(is_boundary(start)); boundaryWalkUntil( [&op](auto heh) { - op(heh); + op(heh); return false; }, start); @@ -266,6 +268,22 @@ GeoData::boundaryWalkUntil(const std::function & op, Halfe } } +GeoData::HalfedgeHandle +GeoData::findEntry(const glm::vec2 from, const glm::vec2 to) const +{ + HalfedgeHandle entry; + boundaryWalkUntil([this, from, to, &entry](auto he) { + const auto e1 = point(to_vertex_handle(he)); + const auto e2 = point(to_vertex_handle(opposite_halfedge_handle(he))); + if (linesCrossLtR(from, to, e1, e2)) { + entry = he; + return true; + } + return false; + }); + return entry; +} + bool GeoData::triangleContainsPoint(const glm::vec2 p, const Triangle<2> & t) { diff --git a/game/geoData.h b/game/geoData.h index 15d7e24..474731b 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -83,6 +83,8 @@ public: void boundaryWalkUntil(const std::function &) const; void boundaryWalkUntil(const std::function &, HalfedgeHandle start) const; + [[nodiscard]] HalfedgeHandle findEntry(const glm::vec2 from, const glm::vec2 to) const; + [[nodiscard]] auto getExtents() const { -- cgit v1.2.3 From d6e99a696aa582b81018078b68f6600c8030d643 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 7 Nov 2023 02:51:42 +0000 Subject: WIP typedefing all the things - headers --- game/geoData.h | 33 +++++++++++++++++---------------- game/network/link.h | 12 ++++++------ game/network/network.h | 42 +++++++++++++++++++++--------------------- game/network/network.impl.h | 12 ++++++------ game/network/rail.h | 10 +++++----- game/selectable.h | 3 ++- game/vehicles/railVehicle.h | 2 +- game/vehicles/train.h | 2 +- 8 files changed, 59 insertions(+), 57 deletions(-) (limited to 'game') diff --git a/game/geoData.h b/game/geoData.h index f9a7d7b..b3ec51d 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include #include #include @@ -16,47 +17,47 @@ public: float height {-1.5F}; }; - using Quad = std::array; + using Quad = std::array; - using Limits = std::pair; + using Limits = std::pair, glm::vec<2, int>>; explicit GeoData(Limits limit, float scale = 10.F); void generateRandom(); void loadFromImages(const std::filesystem::path &, float scale); - [[nodiscard]] glm::vec3 positionAt(glm::vec2) const; - [[nodiscard]] std::optional intersectRay(const Ray &) const; + [[nodiscard]] Position3D positionAt(Position2D) const; + [[nodiscard]] std::optional intersectRay(const Ray &) const; - [[nodiscard]] unsigned int at(glm::ivec2) const; + [[nodiscard]] unsigned int at(glm::vec<2, int>) const; [[nodiscard]] unsigned int at(int x, int y) const; - [[nodiscard]] Quad quad(glm::vec2) const; + [[nodiscard]] Quad quad(Position2D) const; [[nodiscard]] Limits getLimit() const; - [[nodiscard]] glm::uvec2 getSize() const; + [[nodiscard]] glm::vec<2, unsigned int> getSize() const; [[nodiscard]] float getScale() const; [[nodiscard]] std::span getNodes() const; class RayTracer { public: - RayTracer(glm::vec2 p0, glm::vec2 p1); + RayTracer(Position2D p0, Position2D p1); - glm::vec2 next(); + Position2D 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); + RayTracer(Position2D p0, Position2D p1, Position2D d); + RayTracer(Position2D p0, Position2D d, std::pair, std::pair); + static std::pair byAxis(Position2D p0, Position2D p1, Position2D d, glm::length_t); - glm::vec2 p; - const glm::vec2 d; + Position2D p; + const Position2D d; float error; - glm::vec2 inc; + Position2D inc; }; protected: Limits limit {}; // Base grid limits first(x,y) -> second(x,y) - glm::uvec2 size {}; + glm::vec<2, unsigned> size {}; float scale {1}; std::vector nodes; }; diff --git a/game/network/link.h b/game/network/link.h index 0d66c42..78d3e91 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -17,12 +17,12 @@ class Ray; // it has location class Node : public StdTypeDefs { public: - explicit Node(glm::vec3 p) noexcept : pos(p) {}; + explicit Node(Position3D p) noexcept : pos(p) {}; virtual ~Node() noexcept = default; NO_COPY(Node); NO_MOVE(Node); - glm::vec3 pos; + Position3D pos; }; // Generic network link @@ -51,14 +51,14 @@ public: float length; protected: - [[nodiscard]] virtual glm::vec3 + [[nodiscard]] virtual Position3D vehiclePositionOffset() const { return {}; } }; -bool operator<(const glm::vec3 & a, const glm::vec3 & b); +bool operator<(const Position3D & a, const Position3D & b); bool operator<(const Node & a, const Node & b); class LinkStraight : public virtual Link { @@ -77,14 +77,14 @@ LinkStraight::~LinkStraight() = default; class LinkCurve : public virtual Link { public: inline ~LinkCurve() override = 0; - LinkCurve(glm::vec3, float, Arc); + LinkCurve(Position3D, float, Arc); NO_COPY(LinkCurve); NO_MOVE(LinkCurve); [[nodiscard]] Location positionAt(float dist, unsigned char start) const override; [[nodiscard]] bool intersectRay(const Ray &) const override; - glm::vec3 centreBase; + Position3D centreBase; float radius; Arc arc; }; diff --git a/game/network/network.h b/game/network/network.h index b9316eb..02f8c30 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -26,32 +26,32 @@ public: virtual ~Network() = default; DEFAULT_MOVE_NO_COPY(Network); - [[nodiscard]] Node::Ptr findNodeAt(glm::vec3) const; - [[nodiscard]] Node::Ptr nodeAt(glm::vec3); + [[nodiscard]] Node::Ptr findNodeAt(Position3D) const; + [[nodiscard]] Node::Ptr nodeAt(Position3D); enum class NodeIs { InNetwork, NotInNetwork }; using NodeInsertion = std::pair; - [[nodiscard]] NodeInsertion newNodeAt(glm::vec3); - [[nodiscard]] NodeInsertion candidateNodeAt(glm::vec3) const; + [[nodiscard]] NodeInsertion newNodeAt(Position3D); + [[nodiscard]] NodeInsertion candidateNodeAt(Position3D) const; [[nodiscard]] virtual Link::Ptr intersectRayLinks(const Ray &) const = 0; [[nodiscard]] virtual Node::Ptr intersectRayNodes(const Ray &) const; - [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, glm::vec3) const; + [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, Position3D) const; [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &) const; - virtual Link::CCollection candidateStraight(glm::vec3, glm::vec3) = 0; - virtual Link::CCollection candidateJoins(glm::vec3, glm::vec3) = 0; - virtual Link::CCollection candidateExtend(glm::vec3, glm::vec3) = 0; - virtual Link::CCollection addStraight(glm::vec3, glm::vec3) = 0; - virtual Link::CCollection addJoins(glm::vec3, glm::vec3) = 0; - virtual Link::CCollection addExtend(glm::vec3, glm::vec3) = 0; + virtual Link::CCollection candidateStraight(Position3D, Position3D) = 0; + virtual Link::CCollection candidateJoins(Position3D, Position3D) = 0; + virtual Link::CCollection candidateExtend(Position3D, Position3D) = 0; + virtual Link::CCollection addStraight(Position3D, Position3D) = 0; + virtual Link::CCollection addJoins(Position3D, Position3D) = 0; + virtual Link::CCollection addExtend(Position3D, Position3D) = 0; [[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0; protected: static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol); - static GenCurveDef genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir); + static GenCurveDef genCurveDef(const Position3D & start, const Position3D & end, float startDir); static std::pair genCurveDef( - const glm::vec3 & start, const glm::vec3 & end, float startDir, float endDir); + const Position3D & start, const Position3D & end, float startDir, float endDir); using Nodes = std::set>; Nodes nodes; @@ -71,7 +71,7 @@ protected: public: template std::shared_ptr - candidateLink(glm::vec3 a, glm::vec3 b, Params &&... params) + candidateLink(Position3D a, Position3D b, Params &&... params) requires std::is_base_of_v { const auto node1 = candidateNodeAt(a).first, node2 = candidateNodeAt(b).first; @@ -80,7 +80,7 @@ public: template std::shared_ptr - addLink(glm::vec3 a, glm::vec3 b, Params &&... params) + addLink(Position3D a, Position3D b, Params &&... params) requires std::is_base_of_v { const auto node1 = nodeAt(a), node2 = nodeAt(b); @@ -89,12 +89,12 @@ public: return l; } - Link::CCollection candidateStraight(glm::vec3 n1, glm::vec3 n2) override; - Link::CCollection candidateJoins(glm::vec3, glm::vec3) override; - Link::CCollection candidateExtend(glm::vec3, glm::vec3) override; - Link::CCollection addStraight(glm::vec3 n1, glm::vec3 n2) override; - Link::CCollection addJoins(glm::vec3, glm::vec3) override; - Link::CCollection addExtend(glm::vec3, glm::vec3) override; + Link::CCollection candidateStraight(Position3D n1, Position3D n2) override; + Link::CCollection candidateJoins(Position3D, Position3D) override; + Link::CCollection candidateExtend(Position3D, Position3D) override; + Link::CCollection addStraight(Position3D n1, Position3D n2) override; + Link::CCollection addJoins(Position3D, Position3D) override; + Link::CCollection addExtend(Position3D, Position3D) override; [[nodiscard]] float findNodeDirection(Node::AnyCPtr) const override; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index 045335f..8e9e85c 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -54,14 +54,14 @@ NetworkOf::findNodeDirection(Node::AnyCPtr n) const template Link::CCollection -NetworkOf::candidateStraight(glm::vec3 n1, glm::vec3 n2) +NetworkOf::candidateStraight(Position3D n1, Position3D n2) { return {candidateLink(n1, n2)}; } template Link::CCollection -NetworkOf::candidateJoins(glm::vec3 start, glm::vec3 end) +NetworkOf::candidateJoins(Position3D start, Position3D end) { if (glm::distance(start, end) < 2.F) { return {}; @@ -75,7 +75,7 @@ NetworkOf::candidateJoins(glm::vec3 start, glm::vec3 end) template Link::CCollection -NetworkOf::candidateExtend(glm::vec3 start, glm::vec3 end) +NetworkOf::candidateExtend(Position3D start, Position3D end) { const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(candidateNodeAt(start).first)); return {candidateLink(cstart, cend, centre)}; @@ -83,14 +83,14 @@ NetworkOf::candidateExtend(glm::vec3 start, glm::vec3 end) template Link::CCollection -NetworkOf::addStraight(glm::vec3 n1, glm::vec3 n2) +NetworkOf::addStraight(Position3D n1, Position3D n2) { return {addLink(n1, n2)}; } template Link::CCollection -NetworkOf::addJoins(glm::vec3 start, glm::vec3 end) +NetworkOf::addJoins(Position3D start, Position3D end) { if (glm::distance(start, end) < 2.F) { return {}; @@ -103,7 +103,7 @@ NetworkOf::addJoins(glm::vec3 start, glm::vec3 end) template Link::CCollection -NetworkOf::addExtend(glm::vec3 start, glm::vec3 end) +NetworkOf::addExtend(Position3D start, Position3D end) { const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(nodeAt(start))); return {addLink(cstart, cend, centre)}; diff --git a/game/network/rail.h b/game/network/rail.h index 6684801..4a1932f 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -32,7 +32,7 @@ public: NO_MOVE(RailLink); protected: - [[nodiscard]] glm::vec3 vehiclePositionOffset() const override; + [[nodiscard]] Position3D vehiclePositionOffset() const override; [[nodiscard]] static Mesh::Ptr defaultMesh(const std::span vertices); Mesh::Ptr mesh; @@ -45,22 +45,22 @@ public: RailLinkStraight(const Node::Ptr &, const Node::Ptr &); private: - RailLinkStraight(Node::Ptr, Node::Ptr, const glm::vec3 & diff); + RailLinkStraight(Node::Ptr, Node::Ptr, const Position3D & diff); }; class RailLinkCurve : public RailLink, public LinkCurve { public: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, glm::vec2); + RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position2D); private: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, glm::vec3, const Arc); + RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position3D, const Arc); }; class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); - std::shared_ptr addLinksBetween(glm::vec3 start, glm::vec3 end); + std::shared_ptr addLinksBetween(Position3D start, Position3D end); private: void tick(TickDuration elapsed) override; diff --git a/game/selectable.h b/game/selectable.h index 31287d8..9732dca 100644 --- a/game/selectable.h +++ b/game/selectable.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include #include @@ -11,5 +12,5 @@ public: virtual ~Selectable() = default; DEFAULT_MOVE_COPY(Selectable); - [[nodiscard]] virtual bool intersectRay(const Ray &, glm::vec2 *, float *) const = 0; + [[nodiscard]] virtual bool intersectRay(const Ray &, Position2D *, float *) const = 0; }; diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index be88142..e034852 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -17,7 +17,7 @@ public: void move(const Train *, float & trailBy); - [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; + [[nodiscard]] bool intersectRay(const Ray &, Position2D *, float *) const override; RailVehicleClassPtr rvClass; using LV = RailVehicleClass::LocationVertex; diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 20c3bc4..7f0bb99 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -27,7 +27,7 @@ public: return objects.front()->location; } - [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; + [[nodiscard]] bool intersectRay(const Ray &, Position2D *, float *) const override; void tick(TickDuration elapsed) override; void doActivity(Go *, TickDuration) override; -- cgit v1.2.3 From 9c2c3f71065c94a18c02440111b6ff8ca977b90e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 9 Nov 2023 00:40:40 +0000 Subject: WIP typedefing all the things - sources --- game/network/link.cpp | 14 +++++++------- game/network/network.cpp | 18 +++++++++--------- game/network/rail.cpp | 30 +++++++++++++++--------------- game/vehicles/railVehicle.cpp | 12 ++++++------ game/vehicles/train.cpp | 2 +- 5 files changed, 38 insertions(+), 38 deletions(-) (limited to 'game') diff --git a/game/network/link.cpp b/game/network/link.cpp index bb27a52..498afe4 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -8,10 +8,10 @@ Link::Link(End a, End b, float l) : ends {{std::move(a), std::move(b)}}, length {l} { } -LinkCurve::LinkCurve(glm::vec3 c, float r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { } +LinkCurve::LinkCurve(Position3D c, float r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { } bool -operator<(const glm::vec3 & a, const glm::vec3 & b) +operator<(const Position3D & a, const Position3D & b) { // NOLINTNEXTLINE(hicpp-use-nullptr,modernize-use-nullptr) return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z); @@ -48,7 +48,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto ang {as.first + ((as.second - as.first) * frac)}; const auto relPos {!sincosf(ang) * radius}; const auto relClimb {vehiclePositionOffset() - + glm::vec3 {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; + + Position3D {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})}; return Location {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; } @@ -60,14 +60,14 @@ LinkCurve::intersectRay(const Ray & ray) const const auto & e1p {ends[1].node->pos}; const auto slength = round_frac(length / 2.F, 5.F); const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F)); - const auto step {glm::vec3 {arc_length(arc), e1p.z - e0p.z, slength} / segs}; + const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength} / segs}; const auto trans {glm::translate(centreBase)}; auto segCount = static_cast(std::lround(segs)) + 1; - std::vector points; + std::vector points; points.reserve(segCount); - for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { - const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, 0.F, swing.y})}; + for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { + const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; points.emplace_back(t * glm::vec4 {0, 0, 0, 1}); } return ray.passesCloseToEdges(points, 1.F); diff --git a/game/network/network.cpp b/game/network/network.cpp index 083b08e..d18345c 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -14,13 +14,13 @@ Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } Node::Ptr -Network::nodeAt(glm::vec3 pos) +Network::nodeAt(Position3D pos) { return newNodeAt(pos).first; } Network::NodeInsertion -Network::newNodeAt(glm::vec3 pos) +Network::newNodeAt(Position3D pos) { if (auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { return {*nodes.insert(std::move(n)).first, i}; @@ -31,7 +31,7 @@ Network::newNodeAt(glm::vec3 pos) } Node::Ptr -Network::findNodeAt(glm::vec3 pos) const +Network::findNodeAt(Position3D pos) const { if (const auto n = nodes.find(pos); n != nodes.end()) { return *n; @@ -40,7 +40,7 @@ Network::findNodeAt(glm::vec3 pos) const } Network::NodeInsertion -Network::candidateNodeAt(glm::vec3 pos) const +Network::candidateNodeAt(Position3D pos) const { if (const auto n = nodes.find(pos); n != nodes.end()) { return {*n, NodeIs::InNetwork}; @@ -54,7 +54,7 @@ Network::intersectRayNodes(const Ray & ray) const // Click within 2m of a node if (const auto node = std::find_if(nodes.begin(), nodes.end(), [&ray](const Node::Ptr & node) { - glm::vec3 ipos, inorm; + Position3D ipos, inorm; return glm::intersectRaySphere(ray.start, ray.direction, node->pos, 2.F, ipos, inorm); }); node != nodes.end()) { @@ -79,7 +79,7 @@ Network::joinLinks(const Link::Ptr & l, const Link::Ptr & ol) } Link::Nexts -Network::routeFromTo(const Link::End & start, glm::vec3 dest) const +Network::routeFromTo(const Link::End & start, Position3D dest) const { auto destNode {findNodeAt(dest)}; if (!destNode) { @@ -95,7 +95,7 @@ Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) const } GenCurveDef -Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir) +Network::genCurveDef(const Position3D & start, const Position3D & end, float startDir) { const auto diff {end - start}; const auto vy {vector_yaw(diff)}; @@ -111,11 +111,11 @@ Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float start } std::pair -Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir, float endDir) +Network::genCurveDef(const Position3D & start, const Position3D & end, float startDir, float endDir) { startDir += pi; endDir += pi; - const glm::vec2 flatStart {!start}, flatEnd {!end}; + const Position2D flatStart {!start}, flatEnd {!end}; auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.z + ((end.z - start.z) * (sm / (sm + em))); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 5a4f1e1..f46504b 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -18,7 +18,7 @@ template class NetworkOf; constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; -constexpr glm::vec3 RAIL_HEIGHT {0, 0, .25F}; +constexpr Size3D RAIL_HEIGHT {0, 0, .25F}; RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } @@ -28,7 +28,7 @@ RailLinks::tick(TickDuration) } std::shared_ptr -RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) +RailLinks::addLinksBetween(Position3D start, Position3D end) { auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) { @@ -45,7 +45,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) if (dir == vector_yaw(end - start)) { return addLink(start, end); } - const glm::vec2 flatStart {!start}, flatEnd {!end}; + const Position2D flatStart {!start}, flatEnd {!end}; if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); @@ -100,7 +100,7 @@ RailLink::render(const SceneShader &) const mesh->Draw(); } -constexpr const std::array, RAIL_CROSSSECTION_VERTICES> railCrossSection {{ +constexpr const std::array, RAIL_CROSSSECTION_VERTICES> railCrossSection {{ // ___________ // _/ \_ // left to right @@ -122,7 +122,7 @@ RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : R { } -RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const glm::vec3 & diff) : +RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & diff) : Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) { if (glGenVertexArrays) { @@ -133,20 +133,20 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const glm::vec3 & d for (auto ei : {1U, 0U}) { const auto trans {glm::translate(ends[ei].node->pos) * e}; for (const auto & rcs : railCrossSection) { - const glm::vec3 m {(trans * glm::vec4 {rcs.first, 1})}; - vertices.emplace_back(m, glm::vec2 {rcs.second, len * static_cast(ei)}, up); + const Position3D m {(trans * glm::vec4 {rcs.first, 1})}; + vertices.emplace_back(m, Position2D {rcs.second, len * static_cast(ei)}, up); } } mesh = defaultMesh(vertices); } } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, glm::vec2 c) : +RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position2D c) : RailLinkCurve(a, b, c ^ a->pos.z, {!c, a->pos, b->pos}) { } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, glm::vec3 c, const Arc arc) : +RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3D c, const Arc arc) : Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)}, (glm::length(a->pos - c)) * arc_length(arc)), LinkCurve {c, glm::length(ends[0].node->pos - c), arc} @@ -156,25 +156,25 @@ RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, glm::vec3 const auto & e1p {ends[1].node->pos}; const auto slength = round_sleepers(length / 2.F); const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F)); - const auto step {glm::vec3 {arc_length(arc), e1p.z - e0p.z, slength} / segs}; + const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength} / segs}; const auto trans {glm::translate(centreBase)}; auto segCount = static_cast(std::lround(segs)) + 1; std::vector vertices; vertices.reserve(segCount * railCrossSection.size()); - for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { + for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { const auto t { - trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, 0.F, swing.y})}; + trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; for (const auto & rcs : railCrossSection) { - const glm::vec3 m {(t * glm::vec4 {rcs.first, 1})}; - vertices.emplace_back(m, glm::vec2 {rcs.second, swing.z}, up); + const Position3D m {(t * glm::vec4 {rcs.first, 1})}; + vertices.emplace_back(m, Position2D {rcs.second, swing.z}, up); } } mesh = defaultMesh(vertices); } } -glm::vec3 +Position3D RailLink::vehiclePositionOffset() const { return RAIL_HEIGHT; diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 2d820b6..fc43995 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -14,8 +14,8 @@ RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, location {&LV::body, *this}, bogies {{ - {&LV::front, *this, glm::vec3 {0, rvClass->wheelBase / 2.F, 0}}, - {&LV::back, *this, glm::vec3 {0, -rvClass->wheelBase / 2.F, 0}}, + {&LV::front, *this, Position3D {0, rvClass->wheelBase / 2.F, 0}}, + {&LV::back, *this, Position3D {0, -rvClass->wheelBase / 2.F, 0}}, }} { } @@ -32,13 +32,13 @@ RailVehicle::move(const Train * t, float & trailBy) } bool -RailVehicle::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * distance) const +RailVehicle::intersectRay(const Ray & ray, Position2D * baryPos, float * distance) const { constexpr const auto X = 1.35F; const auto Y = this->rvClass->length / 2.F; constexpr const auto Z = 3.9F; const auto moveBy = location.getTransform(); - const std::array cornerVertices {{ + const std::array cornerVertices {{ moveBy * glm::vec4 {-X, Y, 0, 1}, // LFB moveBy * glm::vec4 {X, Y, 0, 1}, // RFB moveBy * glm::vec4 {-X, Y, Z, 1}, // LFT @@ -48,7 +48,7 @@ RailVehicle::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * distance moveBy * glm::vec4 {-X, -Y, Z, 1}, // LBT moveBy * glm::vec4 {X, -Y, Z, 1}, // RBT }}; - static constexpr const std::array triangles {{ + static constexpr const std::array, 10> triangles {{ // Front {0, 1, 2}, {1, 2, 3}, @@ -66,7 +66,7 @@ RailVehicle::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * distance {3, 6, 7}, }}; return std::any_of( - triangles.begin(), triangles.end(), [&cornerVertices, &ray, &baryPos, &distance](const glm::uvec3 idx) { + triangles.begin(), triangles.end(), [&cornerVertices, &ray, &baryPos, &distance](const auto & idx) { return glm::intersectRayTriangle(ray.start, ray.direction, cornerVertices[idx[0]], cornerVertices[idx[1]], cornerVertices[idx[2]], *baryPos, *distance); }); diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 6f3b036..4aa24dc 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -20,7 +20,7 @@ Train::getBogiePosition(float linkDist, float dist) const } bool -Train::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * distance) const +Train::intersectRay(const Ray & ray, Position2D * baryPos, float * distance) const { return applyOne(&RailVehicle::intersectRay, ray, baryPos, distance) != end(); } -- cgit v1.2.3 From d771fbda2c171cfbc36cc0eb3122c1a02ffbb081 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 9 Nov 2023 01:57:27 +0000 Subject: WIP typedefing just about everything else --- game/network/network.h | 2 +- game/network/rail.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'game') diff --git a/game/network/network.h b/game/network/network.h index 02f8c30..8af06a9 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -16,7 +16,7 @@ class Texture; class SceneShader; class Ray; -template using GenDef = std::tuple...>; +template using GenDef = std::tuple...>; using GenCurveDef = GenDef<3, 3, 2>; class Network { diff --git a/game/network/rail.cpp b/game/network/rail.cpp index f46504b..cc61db9 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -133,7 +133,7 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & for (auto ei : {1U, 0U}) { const auto trans {glm::translate(ends[ei].node->pos) * e}; for (const auto & rcs : railCrossSection) { - const Position3D m {(trans * glm::vec4 {rcs.first, 1})}; + const Position3D m {(trans * (rcs.first ^ 1))}; vertices.emplace_back(m, Position2D {rcs.second, len * static_cast(ei)}, up); } } @@ -166,7 +166,7 @@ RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3 const auto t { trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; for (const auto & rcs : railCrossSection) { - const Position3D m {(t * glm::vec4 {rcs.first, 1})}; + const Position3D m {(t * (rcs.first ^ 1))}; vertices.emplace_back(m, Position2D {rcs.second, swing.z}, up); } } -- cgit v1.2.3 From 5e25d79beef19c39537b0f15982a175fec45bb3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 11 Nov 2023 17:37:57 +0000 Subject: Refactor BufferedLocationT to use a callback Simplifies customisation in the face of multiple fields --- game/vehicles/railVehicle.cpp | 15 ++++++++++++--- game/vehicles/railVehicle.h | 5 ++--- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'game') diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index fc43995..1ed904d 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -12,10 +12,19 @@ #include RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : - RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, location {&LV::body, *this}, + RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, + location {[this](const BufferedLocation * l) { + this->get()->body = l->getTransform(); + }}, bogies {{ - {&LV::front, *this, Position3D {0, rvClass->wheelBase / 2.F, 0}}, - {&LV::back, *this, Position3D {0, -rvClass->wheelBase / 2.F, 0}}, + {[this](const BufferedLocation * l) { + this->get()->front = l->getTransform(); + }, + Position3D {0, rvClass->wheelBase / 2.F, 0}}, + {[this](const BufferedLocation * l) { + this->get()->back = l->getTransform(); + }, + Position3D {0, -rvClass->wheelBase / 2.F, 0}}, }} { } diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index e034852..20d1ea1 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -21,9 +21,8 @@ public: RailVehicleClassPtr rvClass; using LV = RailVehicleClass::LocationVertex; - using BLocation = BufferedLocationT; - BLocation location; - std::array bogies; + BufferedLocationUpdater location; + std::array bogies; }; using RailVehiclePtr = std::unique_ptr; -- cgit v1.2.3 From 356e874050e5ad5af87b04a2bb01ce34a86640bb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 13 Nov 2023 00:17:11 +0000 Subject: Send position and rotation matrix to GPU separately --- game/scenary/foliage.cpp | 3 ++- game/scenary/foliage.h | 3 ++- game/scenary/plant.cpp | 2 +- game/scenary/plant.h | 2 +- game/vehicles/railVehicle.cpp | 9 ++++++--- game/vehicles/railVehicleClass.cpp | 8 +++++--- game/vehicles/railVehicleClass.h | 1 + 7 files changed, 18 insertions(+), 10 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 702a52c..c258b77 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -15,7 +15,8 @@ void Foliage::postLoad() { texture = getTexture(); - bodyMesh->configureVAO(instanceVAO).addAttribs(instances.bufferName(), 1); + bodyMesh->configureVAO(instanceVAO) + .addAttribs(instances.bufferName(), 1); } void diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index b72a9c2..5a9d2de 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -15,7 +15,8 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs { glVertexArray instanceVAO; public: - mutable InstanceVertices instances; + using LocationVertex = std::pair; + mutable InstanceVertices instances; void render(const SceneShader &) const override; void shadows(const ShadowMapper &) const override; diff --git a/game/scenary/plant.cpp b/game/scenary/plant.cpp index 4fb3cb5..b39c28b 100644 --- a/game/scenary/plant.cpp +++ b/game/scenary/plant.cpp @@ -2,6 +2,6 @@ #include "location.h" Plant::Plant(std::shared_ptr type, const Location & position) : - type {std::move(type)}, location {this->type->instances.acquire(position.getTransform())} + type {std::move(type)}, location {this->type->instances.acquire(position.getRotationTransform(), position.pos)} { } diff --git a/game/scenary/plant.h b/game/scenary/plant.h index 82ab0e5..77c9ff7 100644 --- a/game/scenary/plant.h +++ b/game/scenary/plant.h @@ -7,7 +7,7 @@ class Location; class Plant : public WorldObject { std::shared_ptr type; - InstanceVertices::InstanceProxy location; + InstanceVertices::InstanceProxy location; void tick(TickDuration) override diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 1ed904d..6e6e18f 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -14,15 +14,18 @@ RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, location {[this](const BufferedLocation * l) { - this->get()->body = l->getTransform(); + this->get()->body = l->getRotationTransform(); + this->get()->bodyPos = l->position(); }}, bogies {{ {[this](const BufferedLocation * l) { - this->get()->front = l->getTransform(); + this->get()->front = l->getRotationTransform(); + this->get()->frontPos = l->position(); }, Position3D {0, rvClass->wheelBase / 2.F, 0}}, {[this](const BufferedLocation * l) { - this->get()->back = l->getTransform(); + this->get()->back = l->getRotationTransform(); + this->get()->backPos = l->position(); }, Position3D {0, -rvClass->wheelBase / 2.F, 0}}, }} diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 324148c..7a6a9fe 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -35,13 +35,15 @@ void RailVehicleClass::postLoad() { texture = getTexture(); - bodyMesh->configureVAO(instanceVAO).addAttribs(instances.bufferName(), 1); + bodyMesh->configureVAO(instanceVAO) + .addAttribs(instances.bufferName(), 1); bogies.front() ->configureVAO(instancesBogiesVAO.front()) - .addAttribs(instances.bufferName(), 1); + .addAttribs(instances.bufferName(), 1); bogies.back() ->configureVAO(instancesBogiesVAO.back()) - .addAttribs(instances.bufferName(), 1); + .addAttribs(instances.bufferName(), 1); + static_assert(sizeof(LocationVertex) == 228UL); } void diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 4668d7d..16dce01 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -20,6 +20,7 @@ public: struct LocationVertex { glm::mat4 body, front, back; + Position3D bodyPos, frontPos, backPos; }; std::array bogies; -- cgit v1.2.3 From 685b33980cc7a346574b24732464f0cbe3115a1f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 15 Nov 2023 01:29:24 +0000 Subject: Switch to millimeters for spatial units Mostly a case of changing far too many magic numbers, something else to fix I guess. I probably missed something. Also there's some hackery when loading 3D models, which are still assumed to be in metres. --- game/geoData.cpp | 2 +- game/geoData.h | 2 +- game/network/rail.cpp | 18 +++++++++--------- game/vehicles/linkHistory.cpp | 2 +- game/vehicles/railVehicle.cpp | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 76550cc..da067f7 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -31,7 +31,7 @@ GeoData::generateRandom() std::uniform_int_distribution<> rxpos(limit.first.x + 2, limit.second.x - 2), rypos(limit.first.y + 2, limit.second.y - 2); std::uniform_int_distribution<> rsize(10, 30); - std::uniform_real_distribution rheight(1, 3); + std::uniform_real_distribution rheight(1000, 3000); for (int h = 0; h < 500;) { const glm::ivec2 hpos {rxpos(gen), rypos(gen)}; const glm::ivec2 hsize {rsize(gen), rsize(gen)}; diff --git a/game/geoData.h b/game/geoData.h index b3ec51d..3bceb9c 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -14,7 +14,7 @@ class Ray; class GeoData { public: struct Node { - float height {-1.5F}; + float height {-1500.F}; }; using Quad = std::array; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index cc61db9..ff101d4 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -18,7 +18,7 @@ template class NetworkOf; constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; -constexpr Size3D RAIL_HEIGHT {0, 0, .25F}; +constexpr Size3D RAIL_HEIGHT {0, 0, 250.F}; RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } @@ -104,11 +104,11 @@ constexpr const std::array, RAIL_CROSSSECTION_VERTI // ___________ // _/ \_ // left to right - {{-1.9F, 0.F, 0.F}, 0.F}, - {{-.608F, 0.F, RAIL_HEIGHT.z}, 0.34F}, - {{0, 0.F, RAIL_HEIGHT.z * .7F}, 0.5F}, - {{.608F, 0.F, RAIL_HEIGHT.z}, 0.66F}, - {{1.9F, 0.F, 0.F}, 1.F}, + {{-1900.F, 0.F, 0.F}, 0.F}, + {{-608.F, 0.F, RAIL_HEIGHT.z}, .34F}, + {{0, 0.F, RAIL_HEIGHT.z * .7F}, .5F}, + {{608.F, 0.F, RAIL_HEIGHT.z}, .66F}, + {{1900.F, 0.F, 0.F}, 1.F}, }}; constexpr auto sleepers {5.F}; // There are 5 repetitions of sleepers in the texture @@ -128,7 +128,7 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & if (glGenVertexArrays) { std::vector vertices; vertices.reserve(2 * railCrossSection.size()); - const auto len = round_sleepers(length / 2.F); + const auto len = round_sleepers(length / 2000.F); const auto e {flat_orientation(diff)}; for (auto ei : {1U, 0U}) { const auto trans {glm::translate(ends[ei].node->pos) * e}; @@ -155,8 +155,8 @@ RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3 const auto & e0p {ends[0].node->pos}; const auto & e1p {ends[1].node->pos}; const auto slength = round_sleepers(length / 2.F); - const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F)); - const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength} / segs}; + const auto segs = std::round(slength / std::pow(radius, 0.7F)); + const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs}; const auto trans {glm::translate(centreBase)}; auto segCount = static_cast(std::lround(segs)) + 1; diff --git a/game/vehicles/linkHistory.cpp b/game/vehicles/linkHistory.cpp index 2802109..e6bab36 100644 --- a/game/vehicles/linkHistory.cpp +++ b/game/vehicles/linkHistory.cpp @@ -8,7 +8,7 @@ LinkHistory::add(const Link::WPtr & l, unsigned char d) links.insert(links.begin(), {l, d}); const auto lp = l.lock(); totalLen += lp->length; - while (totalLen >= 1000.F && !links.empty()) { + while (totalLen >= 1000000.F && !links.empty()) { totalLen -= links.back().first.lock()->length; links.pop_back(); } diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 6e6e18f..26536f5 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -46,9 +46,9 @@ RailVehicle::move(const Train * t, float & trailBy) bool RailVehicle::intersectRay(const Ray & ray, Position2D * baryPos, float * distance) const { - constexpr const auto X = 1.35F; + constexpr const auto X = 1350.F; const auto Y = this->rvClass->length / 2.F; - constexpr const auto Z = 3.9F; + constexpr const auto Z = 3900.F; const auto moveBy = location.getTransform(); const std::array cornerVertices {{ moveBy * glm::vec4 {-X, Y, 0, 1}, // LFB -- cgit v1.2.3 From 3200eb41d60595813dca751fe15193ba0b44dddf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Nov 2023 14:32:56 +0000 Subject: Remove getTransform --- game/vehicles/railVehicle.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'game') diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 26536f5..bee0dd0 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -49,16 +49,16 @@ RailVehicle::intersectRay(const Ray & ray, Position2D * baryPos, float * distanc constexpr const auto X = 1350.F; const auto Y = this->rvClass->length / 2.F; constexpr const auto Z = 3900.F; - const auto moveBy = location.getTransform(); + const auto moveBy = location.getRotationTransform(); const std::array cornerVertices {{ - moveBy * glm::vec4 {-X, Y, 0, 1}, // LFB - moveBy * glm::vec4 {X, Y, 0, 1}, // RFB - moveBy * glm::vec4 {-X, Y, Z, 1}, // LFT - moveBy * glm::vec4 {X, Y, Z, 1}, // RFT - moveBy * glm::vec4 {-X, -Y, 0, 1}, // LBB - moveBy * glm::vec4 {X, -Y, 0, 1}, // RBB - moveBy * glm::vec4 {-X, -Y, Z, 1}, // LBT - moveBy * glm::vec4 {X, -Y, Z, 1}, // RBT + location.position() + (moveBy * glm::vec4 {-X, Y, 0, 1}).xyz(), // LFB + location.position() + (moveBy * glm::vec4 {X, Y, 0, 1}).xyz(), // RFB + location.position() + (moveBy * glm::vec4 {-X, Y, Z, 1}).xyz(), // LFT + location.position() + (moveBy * glm::vec4 {X, Y, Z, 1}).xyz(), // RFT + location.position() + (moveBy * glm::vec4 {-X, -Y, 0, 1}).xyz(), // LBB + location.position() + (moveBy * glm::vec4 {X, -Y, 0, 1}).xyz(), // RBB + location.position() + (moveBy * glm::vec4 {-X, -Y, Z, 1}).xyz(), // LBT + location.position() + (moveBy * glm::vec4 {X, -Y, Z, 1}).xyz(), // RBT }}; static constexpr const std::array, 10> triangles {{ // Front -- cgit v1.2.3 From 0aa665c3648d788755b00c9e431c872d57fddbb8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Nov 2023 16:28:39 +0000 Subject: Model positions as integers Introduces test failure in arcs due to rounding, but I don't want to create a complicated fix as link positions are still floats and hopefully that'll go away... somehow --- game/geoData.cpp | 4 ++-- game/scenary/foliage.h | 2 +- game/vehicles/railVehicle.cpp | 22 +++++++++++----------- game/vehicles/railVehicleClass.h | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index da067f7..ec990ea 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -73,7 +73,7 @@ GeoData::loadFromImages(const std::filesystem::path & fileName, float scale_) } GeoData::Quad -GeoData::quad(glm::vec2 wcoord) const +GeoData::quad(Position2D wcoord) const { constexpr static const std::array corners {{{0, 0}, {0, 1}, {1, 0}, {1, 1}}}; return transform_array(transform_array(corners, @@ -154,7 +154,7 @@ GeoData::intersectRay(const Ray & ray) const try { const auto point = quad(n); for (auto offset : {0U, 1U}) { - glm::vec2 bary; + BaryPosition bary; float distance; if (glm::intersectRayTriangle(ray.start, ray.direction, point[offset], point[offset + 1], point[offset + 2], bary, distance)) { diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index 5a9d2de..bbb6200 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -15,7 +15,7 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs { glVertexArray instanceVAO; public: - using LocationVertex = std::pair; + using LocationVertex = std::pair; mutable InstanceVertices instances; void render(const SceneShader &) const override; void shadows(const ShadowMapper &) const override; diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index bee0dd0..30b615c 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -38,9 +38,9 @@ RailVehicle::move(const Train * t, float & trailBy) const auto overhang {(rvClass->length - rvClass->wheelBase) / 2}; const auto & b1Pos = bogies[0] = t->getBogiePosition(t->linkDist, trailBy += overhang); const auto & b2Pos = bogies[1] = t->getBogiePosition(t->linkDist, trailBy += rvClass->wheelBase); - const auto diff = glm::normalize(b2Pos.position() - b1Pos.position()); - location.setLocation((b1Pos.position() + b2Pos.position()) / 2.F, {vector_pitch(diff), vector_yaw(diff), 0}); - trailBy += 0.6F + overhang; + const auto diff = glm::normalize(RelativePosition3D(b2Pos.position() - b1Pos.position())); + location.setLocation((b1Pos.position() + b2Pos.position()) / 2, {vector_pitch(diff), vector_yaw(diff), 0}); + trailBy += 600.F + overhang; } bool @@ -51,14 +51,14 @@ RailVehicle::intersectRay(const Ray & ray, Position2D * baryPos, float * distanc constexpr const auto Z = 3900.F; const auto moveBy = location.getRotationTransform(); const std::array cornerVertices {{ - location.position() + (moveBy * glm::vec4 {-X, Y, 0, 1}).xyz(), // LFB - location.position() + (moveBy * glm::vec4 {X, Y, 0, 1}).xyz(), // RFB - location.position() + (moveBy * glm::vec4 {-X, Y, Z, 1}).xyz(), // LFT - location.position() + (moveBy * glm::vec4 {X, Y, Z, 1}).xyz(), // RFT - location.position() + (moveBy * glm::vec4 {-X, -Y, 0, 1}).xyz(), // LBB - location.position() + (moveBy * glm::vec4 {X, -Y, 0, 1}).xyz(), // RBB - location.position() + (moveBy * glm::vec4 {-X, -Y, Z, 1}).xyz(), // LBT - location.position() + (moveBy * glm::vec4 {X, -Y, Z, 1}).xyz(), // RBT + location.position() + GlobalPosition3D(moveBy * glm::vec4 {-X, Y, 0, 1}).xyz(), // LFB + location.position() + GlobalPosition3D(moveBy * glm::vec4 {X, Y, 0, 1}).xyz(), // RFB + location.position() + GlobalPosition3D(moveBy * glm::vec4 {-X, Y, Z, 1}).xyz(), // LFT + location.position() + GlobalPosition3D(moveBy * glm::vec4 {X, Y, Z, 1}).xyz(), // RFT + location.position() + GlobalPosition3D(moveBy * glm::vec4 {-X, -Y, 0, 1}).xyz(), // LBB + location.position() + GlobalPosition3D(moveBy * glm::vec4 {X, -Y, 0, 1}).xyz(), // RBB + location.position() + GlobalPosition3D(moveBy * glm::vec4 {-X, -Y, Z, 1}).xyz(), // LBT + location.position() + GlobalPosition3D(moveBy * glm::vec4 {X, -Y, Z, 1}).xyz(), // RBT }}; static constexpr const std::array, 10> triangles {{ // Front diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 16dce01..913feea 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -20,7 +20,7 @@ public: struct LocationVertex { glm::mat4 body, front, back; - Position3D bodyPos, frontPos, backPos; + GlobalPosition3D bodyPos, frontPos, backPos; }; std::array bogies; -- cgit v1.2.3 From 3abe940a22d99939b666bd806214c8b986cb3ed9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 26 Nov 2023 14:20:28 +0000 Subject: Calculate vertex normals only Doesn't require half edge normals or face normals, which uses Newell's method, which results in integer overflow... Not to mention we don't need all the other normals. --- game/geoData.cpp | 16 ++++++++++++---- game/geoData.h | 6 ++++-- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 7710efe..70133a9 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -57,8 +57,7 @@ GeoData::loadFromAsciiGrid(const std::filesystem::path & input) }); } } - mesh.update_face_normals(); - mesh.update_vertex_normals(); + mesh.update_vertex_normals_only(); return mesh; }; @@ -77,8 +76,7 @@ GeoData::createFlat(GlobalPosition2D lower, GlobalPosition2D upper, GlobalDistan mesh.add_face(ll, uu, lu); mesh.add_face(ll, ul, uu); - mesh.update_face_normals(); - mesh.update_vertex_normals(); + mesh.update_vertex_normals_only(); return mesh; } @@ -317,3 +315,13 @@ GeoData::findBoundaryStart() const return is_boundary(heh); }); } + +void +GeoData::update_vertex_normals_only() +{ + for (auto vh : all_vertices()) { + Normal3D n; + calc_vertex_normal_correct(vh, n); + this->set_normal(vh, glm::normalize(n)); + } +} diff --git a/game/geoData.h b/game/geoData.h index 3141dbe..d4d0fb3 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -10,10 +10,10 @@ #include struct GeoDataTraits : public OpenMesh::DefaultTraits { - FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + FaceAttributes(OpenMesh::Attributes::Status); EdgeAttributes(OpenMesh::Attributes::Status); VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - HalfedgeAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + HalfedgeAttributes(OpenMesh::Attributes::Status); using Point = GlobalPosition3D; using Normal = Normal3D; }; @@ -105,6 +105,8 @@ protected: [[nodiscard]] bool triangleContainsPoint(const GlobalPosition2D, FaceHandle) const; [[nodiscard]] HalfedgeHandle findBoundaryStart() const; + void update_vertex_normals_only(); + private: GlobalPosition3D lowerExtent {}, upperExtent {}; }; -- cgit v1.2.3 From 0c97f72d6f3feab22a37f8d33e2d98080d52dab7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 26 Nov 2023 18:50:56 +0000 Subject: Fix terrain texture tiling --- game/terrain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/terrain.cpp b/game/terrain.cpp index 5bb8abd..3778f3d 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -35,8 +35,8 @@ Terrain::generateMeshes() std::transform(geoData->vertices_begin(), geoData->vertices_end(), std::back_inserter(vertices), [this, &vertexIndex](const GeoData::VertexHandle v) { vertexIndex.emplace(v, vertexIndex.size()); - const glm::vec3 p = geoData->point(v); - return Vertex {p, p / 10.F, geoData->normal(v)}; + const auto p = geoData->point(v); + return Vertex {p, p / 10000, geoData->normal(v)}; }); std::for_each( geoData->faces_begin(), geoData->faces_end(), [this, &vertexIndex, &indices](const GeoData::FaceHandle f) { -- cgit v1.2.3 From 0f56b13766fc6d8b45dabc4febf378756620ab32 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 Dec 2023 12:26:36 +0000 Subject: Replace positionAt with pure integer version --- game/geoData.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 70133a9..d092d00 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -155,6 +155,16 @@ namespace { static_assert(linesCrossLtR({1, 1}, {2, 2}, {1, 2}, {2, 1})); static_assert(!linesCrossLtR({2, 2}, {1, 1}, {1, 2}, {2, 1})); + + constexpr GlobalPosition3D + positionOnTriangle(const GlobalPosition2D point, const GeoData::Triangle<3> & t) + { + const glm::vec<3, int64_t> a = t[1] - t[0], b = t[2] - t[0]; + const auto n = crossInt(a, b); + return {point, ((n.x * t[0].x) + (n.y * t[0].y) + (n.z * t[0].z) - (n.x * point.x) - (n.y * point.y)) / n.z}; + } + + static_assert(positionOnTriangle({7, -2}, {{1, 2, 3}, {1, 0, 1}, {-2, 1, 0}}) == GlobalPosition3D {7, -2, 3}); } OpenMesh::FaceHandle @@ -179,10 +189,7 @@ GeoData::findPoint(GlobalPosition2D p, OpenMesh::FaceHandle f) const GlobalPosition3D GeoData::positionAt(const PointFace & p) const { - RelativePosition3D out {}; - const auto t = triangle<3>(p.face(this)); - glm::intersectLineTriangle({p.point, 0}, up, t[0], t[1], t[2], out); - return {p.point, out[0]}; + return positionOnTriangle(p.point, triangle<3>(p.face(this))); } [[nodiscard]] std::optional -- cgit v1.2.3 From 0ec5b3f7ee049a45fa6a87101813ea9717eecbbb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 Dec 2023 16:25:27 +0000 Subject: Extend ray for intersect walk to cover the extents of the map --- game/geoData.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index d092d00..6c6f1df 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -202,17 +202,19 @@ GeoData::intersectRay(const Ray & ray) const GeoData::intersectRay(const Ray & ray, FaceHandle face) const { std::optional out; - walkUntil(PointFace {ray.start, face}, ray.start + (ray.direction * 10000.F), [&out, &ray, this](FaceHandle face) { - BaryPosition bari {}; - float dist {}; - const auto t = triangle<3>(face); - if (glm::intersectRayTriangle( - ray.start, ray.direction, t[0], t[1], t[2], bari, dist)) { - out = t * bari; - return true; - } - return false; - }); + walkUntil(PointFace {ray.start, face}, + ray.start.xy() + (ray.direction.xy() * RelativePosition2D(upperExtent.xy() - lowerExtent.xy())), + [&out, &ray, this](FaceHandle face) { + BaryPosition bari {}; + float dist {}; + const auto t = triangle<3>(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; } -- cgit v1.2.3 From 826a380710261961eb339d12d44f1c45fc384131 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 Dec 2023 16:36:11 +0000 Subject: Handle the case where the ray never enters the map --- game/geoData.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 6c6f1df..e2b7f66 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -232,7 +232,11 @@ GeoData::walkUntil(const PointFace & from, const GlobalPosition2D to, const std: { auto f = from.face(this); if (!f.is_valid()) { - f = opposite_face_handle(findEntry(from.point, to)); + const auto entryEdge = findEntry(from.point, to); + if (!entryEdge.is_valid()) { + return; + } + f = opposite_face_handle(entryEdge); } FaceHandle previousFace; while (f.is_valid() && !op(f)) { -- cgit v1.2.3 From 9fc82f2dd5aaa6f858ba22c92946e8cadfd7d79b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 3 Dec 2023 13:50:24 +0000 Subject: Render correct VAO for rail vehicle shadows --- game/vehicles/railVehicleClass.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 7a6a9fe..3ca42db 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -66,7 +66,7 @@ RailVehicleClass::shadows(const ShadowMapper & mapper) const if (const auto count = static_cast(instances.size())) { mapper.dynamicPointInst.use(); bodyMesh->DrawInstanced(instanceVAO, count); - bogies.front()->DrawInstanced(instanceVAO, count); - bogies.back()->DrawInstanced(instanceVAO, count); + bogies.front()->DrawInstanced(instancesBogiesVAO.front(), count); + bogies.back()->DrawInstanced(instancesBogiesVAO.back(), count); } } -- cgit v1.2.3 From f3f926c97efe365afecd54d06d830961376bae30 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 17 Dec 2023 18:56:23 +0000 Subject: Use new calc types in geoData --- game/geoData.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index e2b7f66..97f4a26 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -120,7 +120,8 @@ namespace { [[nodiscard]] constexpr inline auto pointLineOp(const GlobalPosition2D p, const GlobalPosition2D e1, const GlobalPosition2D e2) { - return Op {}(int64_t(e2.x - e1.x) * int64_t(p.y - e1.y), int64_t(e2.y - e1.y) * int64_t(p.x - e1.x)); + return Op {}(CalcDistance(e2.x - e1.x) * CalcDistance(p.y - e1.y), + CalcDistance(e2.y - e1.y) * CalcDistance(p.x - e1.x)); } constexpr auto pointLeftOfLine = pointLineOp; @@ -159,7 +160,7 @@ namespace { constexpr GlobalPosition3D positionOnTriangle(const GlobalPosition2D point, const GeoData::Triangle<3> & t) { - const glm::vec<3, int64_t> a = t[1] - t[0], b = t[2] - t[0]; + const CalcPosition3D a = t[1] - t[0], b = t[2] - t[0]; const auto n = crossInt(a, b); return {point, ((n.x * t[0].x) + (n.y * t[0].y) + (n.z * t[0].z) - (n.x * point.x) - (n.y * point.y)) / n.z}; } -- cgit v1.2.3 From 0841ead91c49a212134f19f7c0b411984b0fda29 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Dec 2023 13:20:55 +0000 Subject: Remove weird operator! on vec2/3 --- game/network/link.cpp | 2 +- game/network/network.cpp | 4 ++-- game/network/rail.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'game') diff --git a/game/network/link.cpp b/game/network/link.cpp index 498afe4..d8479dd 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -46,7 +46,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; const auto as {std::make_pair(arc[start], arc[1 - start])}; const auto ang {as.first + ((as.second - as.first) * frac)}; - const auto relPos {!sincosf(ang) * radius}; + const auto relPos {sincosf(ang) ^ 0.F * radius}; const auto relClimb {vehiclePositionOffset() + Position3D {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})}; diff --git a/game/network/network.cpp b/game/network/network.cpp index d18345c..5de2f5d 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -100,7 +100,7 @@ Network::genCurveDef(const Position3D & start, const Position3D & end, float sta const auto diff {end - start}; const auto vy {vector_yaw(diff)}; const auto dir = pi + startDir; - const auto flatStart {!start}, flatEnd {!end}; + const auto flatStart {start.xy()}, flatEnd {end.xy()}; const auto n2ed {(vy * 2) - dir - pi}; const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)}; @@ -115,7 +115,7 @@ Network::genCurveDef(const Position3D & start, const Position3D & end, float sta { startDir += pi; endDir += pi; - const Position2D flatStart {!start}, flatEnd {!end}; + const Position2D flatStart {start.xy()}, flatEnd {end.xy()}; auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.z + ((end.z - start.z) * (sm / (sm + em))); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index ff101d4..545a728 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -45,7 +45,7 @@ RailLinks::addLinksBetween(Position3D start, Position3D end) if (dir == vector_yaw(end - start)) { return addLink(start, end); } - const Position2D flatStart {!start}, flatEnd {!end}; + const Position2D flatStart {start.xy()}, flatEnd {end.xy()}; if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); @@ -142,7 +142,7 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & } RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position2D c) : - RailLinkCurve(a, b, c ^ a->pos.z, {!c, a->pos, b->pos}) + RailLinkCurve(a, b, c ^ a->pos.z, {c ^ 0.F, a->pos, b->pos}) { } -- cgit v1.2.3 From 048f18e2a0b32044525cef41fa053984433c74b9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 29 Dec 2023 14:12:40 +0000 Subject: Remove misleading power operator^ on vec2/3 --- game/network/link.cpp | 2 +- game/network/network.cpp | 4 ++-- game/network/rail.cpp | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'game') diff --git a/game/network/link.cpp b/game/network/link.cpp index d8479dd..703a1ca 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -46,7 +46,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; const auto as {std::make_pair(arc[start], arc[1 - start])}; const auto ang {as.first + ((as.second - as.first) * frac)}; - const auto relPos {sincosf(ang) ^ 0.F * radius}; + const auto relPos {(sincosf(ang) || 0.F) * radius}; const auto relClimb {vehiclePositionOffset() + Position3D {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})}; diff --git a/game/network/network.cpp b/game/network/network.cpp index 5de2f5d..1ff5b26 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -125,7 +125,7 @@ Network::genCurveDef(const Position3D & start, const Position3D & end, float sta const auto c1 = flatStart + sincosf(startDir + half_pi) * radius; const auto c2 = flatEnd + sincosf(endDir + half_pi) * radius; const auto mid = (c1 + c2) / 2.F; - const auto midh = mid ^ midheight(mid); + const auto midh = mid || midheight(mid); return {{start, midh, c1}, {end, midh, c2}}; } else { @@ -133,7 +133,7 @@ Network::genCurveDef(const Position3D & start, const Position3D & end, float sta const auto c1 = flatStart + sincosf(startDir - half_pi) * radius; const auto c2 = flatEnd + sincosf(endDir - half_pi) * radius; const auto mid = (c1 + c2) / 2.F; - const auto midh = mid ^ midheight(mid); + const auto midh = mid || midheight(mid); return {{midh, start, c1}, {midh, end, c2}}; } } diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 545a728..303f1c8 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -57,7 +57,7 @@ RailLinks::addLinksBetween(Position3D start, Position3D end) const auto c1 = flatStart + sincosf(dir + half_pi) * radius; const auto c2 = flatEnd + sincosf(dir2 + half_pi) * radius; const auto mid = (c1 + c2) / 2.F; - const auto midh = mid ^ midheight(mid); + const auto midh = mid || midheight(mid); addLink(start, midh, c1); return addLink(end, midh, c2); } @@ -66,7 +66,7 @@ RailLinks::addLinksBetween(Position3D start, Position3D end) const auto c1 = flatStart + sincosf(dir - half_pi) * radius; const auto c2 = flatEnd + sincosf(dir2 - half_pi) * radius; const auto mid = (c1 + c2) / 2.F; - const auto midh = mid ^ midheight(mid); + const auto midh = mid || midheight(mid); addLink(midh, start, c1); return addLink(midh, end, c2); } @@ -133,7 +133,7 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & for (auto ei : {1U, 0U}) { const auto trans {glm::translate(ends[ei].node->pos) * e}; for (const auto & rcs : railCrossSection) { - const Position3D m {(trans * (rcs.first ^ 1))}; + const Position3D m {(trans * (rcs.first || 1.F))}; vertices.emplace_back(m, Position2D {rcs.second, len * static_cast(ei)}, up); } } @@ -142,7 +142,7 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & } RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position2D c) : - RailLinkCurve(a, b, c ^ a->pos.z, {c ^ 0.F, a->pos, b->pos}) + RailLinkCurve(a, b, c || a->pos.z, {c || 0.F, a->pos, b->pos}) { } @@ -166,7 +166,7 @@ RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3 const auto t { trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; for (const auto & rcs : railCrossSection) { - const Position3D m {(t * (rcs.first ^ 1))}; + const Position3D m {(t * (rcs.first || 1.F))}; vertices.emplace_back(m, Position2D {rcs.second, swing.z}, up); } } -- cgit v1.2.3 From 69e6b7d2d349dcc42d2d415a72181ba729d5a19d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 1 Jan 2024 15:53:54 +0000 Subject: Remove more use of legacy types --- game/vehicles/railVehicle.cpp | 2 +- game/vehicles/railVehicle.h | 2 +- game/vehicles/train.cpp | 2 +- game/vehicles/train.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 30b615c..7e4b1ee 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -44,7 +44,7 @@ RailVehicle::move(const Train * t, float & trailBy) } bool -RailVehicle::intersectRay(const Ray & ray, Position2D * baryPos, float * distance) const +RailVehicle::intersectRay(const Ray & ray, BaryPosition * baryPos, float * distance) const { constexpr const auto X = 1350.F; const auto Y = this->rvClass->length / 2.F; diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index 20d1ea1..8cbc49d 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -17,7 +17,7 @@ public: void move(const Train *, float & trailBy); - [[nodiscard]] bool intersectRay(const Ray &, Position2D *, float *) const override; + [[nodiscard]] bool intersectRay(const Ray &, BaryPosition *, float *) const override; RailVehicleClassPtr rvClass; using LV = RailVehicleClass::LocationVertex; diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 4aa24dc..13905a3 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -20,7 +20,7 @@ Train::getBogiePosition(float linkDist, float dist) const } bool -Train::intersectRay(const Ray & ray, Position2D * baryPos, float * distance) const +Train::intersectRay(const Ray & ray, BaryPosition * baryPos, float * distance) const { return applyOne(&RailVehicle::intersectRay, ray, baryPos, distance) != end(); } diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 7f0bb99..c77cd23 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -27,7 +27,7 @@ public: return objects.front()->location; } - [[nodiscard]] bool intersectRay(const Ray &, Position2D *, float *) const override; + [[nodiscard]] bool intersectRay(const Ray &, BaryPosition *, float *) const override; void tick(TickDuration elapsed) override; void doActivity(Go *, TickDuration) override; -- cgit v1.2.3