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 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'game/terrain2.cpp') 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 -- cgit v1.2.3