From 7d1351c2a25d71d312bf341ccab6fe6ca213023f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 16:13:42 +0100 Subject: Fix creating a flat terrain of exactly the requested size Which must be a size of multiples of GRID_SIZE, 10m --- game/geoData.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'game/geoData.cpp') diff --git a/game/geoData.cpp b/game/geoData.cpp index ed4303b..2313342 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -65,36 +65,36 @@ GeoData::loadFromAsciiGrid(const std::filesystem::path & input) return mesh; }; -template constexpr static T GRID_SIZE = 10'000; +constexpr static GlobalDistance GRID_SIZE = 10'000; GeoData GeoData::createFlat(GlobalPosition2D lower, GlobalPosition2D upper, GlobalDistance h) { + assert((upper - lower) % GRID_SIZE == GlobalPosition2D {}); GeoData mesh; mesh.lowerExtent = {lower, h}; mesh.upperExtent = {upper, h}; std::vector vertices; - for (GlobalDistance row = lower.x; row < upper.x; row += GRID_SIZE) { - for (GlobalDistance col = lower.y; col < upper.y; col += GRID_SIZE) { + for (GlobalDistance row = lower.x; row <= upper.x; row += GRID_SIZE) { + for (GlobalDistance col = lower.y; col <= upper.y; col += GRID_SIZE) { vertices.push_back(mesh.add_vertex({col, row, h})); } } - const auto nrows = static_cast(std::ceil(float(upper.x - lower.x) / GRID_SIZE)); - const auto ncols = static_cast(std::ceil(float(upper.y - lower.y) / GRID_SIZE)); - for (size_t row = 1; row < nrows; ++row) { - for (size_t col = 1; col < ncols; ++col) { + const auto n = glm::vec<2, size_t> {((upper - lower) / GRID_SIZE) + 1}; + for (auto row = 1U; row < n.x; ++row) { + for (auto col = 1U; col < n.y; ++col) { mesh.add_face({ - vertices[ncols * (row - 1) + (col - 1)], - vertices[ncols * (row - 0) + (col - 0)], - vertices[ncols * (row - 0) + (col - 1)], + vertices[n.y * (row - 1) + (col - 1)], + vertices[n.y * (row - 0) + (col - 0)], + vertices[n.y * (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)], + vertices[n.y * (row - 1) + (col - 1)], + vertices[n.y * (row - 1) + (col - 0)], + vertices[n.y * (row - 0) + (col - 0)], }); } } -- cgit v1.2.3