summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-11-04 11:21:23 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-11-04 11:21:23 +0000
commita46fded5d93487974ac5f40ff36c8c0f4f7a9db2 (patch)
tree62b66deaefae4b77da3f56bd4e774ddc93f142da
parentPsycho-rebased branch terrain on top of main (diff)
downloadilt-a46fded5d93487974ac5f40ff36c8c0f4f7a9db2.tar.bz2
ilt-a46fded5d93487974ac5f40ff36c8c0f4f7a9db2.tar.xz
ilt-a46fded5d93487974ac5f40ff36c8c0f4f7a9db2.zip
Static helper for loading ASCII grid data
-rw-r--r--game/terrain2.cpp16
-rw-r--r--game/terrain2.h5
-rw-r--r--test/test-terrain.cpp2
3 files changed, 15 insertions, 8 deletions
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 <glm/gtx/intersect.hpp>
#include <maths.h>
-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<std::string_view, size_t *> properties {
@@ -22,11 +23,12 @@ TerrainMesh::TerrainMesh(const std::filesystem::path & input)
}
std::vector<VertexHandle> 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<TerrainTraits> {
+private:
+ TerrainMesh() = default;
+
public:
- explicit TerrainMesh(const std::filesystem::path &);
+ static TerrainMesh loadFromAsciiGrid(const std::filesystem::path &);
struct PointFace {
// NOLINTNEXTLINE(hicpp-explicit-conversions)
diff --git a/test/test-terrain.cpp b/test/test-terrain.cpp
index 53eebfb..512a90e 100644
--- a/test/test-terrain.cpp
+++ b/test/test-terrain.cpp
@@ -8,7 +8,7 @@
class TestTerrainMesh : public TerrainMesh {
public:
- TestTerrainMesh() : TerrainMesh {FIXTURESDIR "height/SD19.asc"} { }
+ TestTerrainMesh() : TerrainMesh {TerrainMesh::loadFromAsciiGrid(FIXTURESDIR "height/SD19.asc")} { }
};
constexpr size_t ncols = 200, nrows = 200, xllcorner = 310000, yllcorner = 490000, cellsize = 50;