From 8231a803cbb561715ed0df5a1e393f002874b5db Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Apr 2024 18:24:32 +0100 Subject: Create water squares/polygons only where required --- game/water.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 15 deletions(-) (limited to 'game') diff --git a/game/water.cpp b/game/water.cpp index 1c0e9ca..3dc1916 100644 --- a/game/water.cpp +++ b/game/water.cpp @@ -12,34 +12,80 @@ #include #include #include +#include #include #include +namespace glm { + bool + operator<(const GlobalPosition2D a, const GlobalPosition2D b) + { + return std::tie(a.x, a.y) < std::tie(b.x, b.y); + } +} + Water::Water(std::shared_ptr tm) : geoData {std::move(tm)}, water {std::make_shared("water.png")} { generateMeshes(); } +static constexpr GlobalDistance MIN_HEIGHT = 1'000; +static constexpr GlobalDistance TILE_SIZE = 8'192; +static constexpr GlobalDistance BORDER = TILE_SIZE / 2; + void Water::generateMeshes() { + // Map out where a water square needs to exist to cover all terrain faces with a low vertex + std::set waterPositions; + std::for_each(geoData->vertices_sbegin(), geoData->vertices_end(), [this, &waterPositions](const auto vh) { + if (geoData->point(vh).z < MIN_HEIGHT) { + std::for_each(geoData->vf_begin(vh), geoData->vf_end(vh), + [done = std::set(), this, &waterPositions](const auto fh) mutable { + if (done.insert(fh).second) { + const auto getrange = [this, fh](glm::length_t axis) { + const auto mme = std::minmax_element(geoData->fv_begin(fh), geoData->fv_end(fh), + [this, axis](const auto vh1, const auto vh2) { + return geoData->point(vh1)[axis] < geoData->point(vh2)[axis]; + }); + + return std::make_pair((geoData->point(*mme.first)[axis] - BORDER) / TILE_SIZE, + (geoData->point(*mme.second)[axis] + BORDER) / TILE_SIZE); + }; + const auto xrange = getrange(0); + const auto yrange = getrange(1); + for (auto x = xrange.first; x < xrange.second; x++) { + for (auto y = yrange.first; y < yrange.second; y++) { + waterPositions.emplace(x, y); + } + } + } + }); + } + }); + std::vector indices; - indices.reserve(geoData->n_faces() * 3); std::vector vertices; - vertices.reserve(geoData->n_vertices()); - std::map vertexIndex; - std::transform(geoData->vertices_sbegin(), geoData->vertices_end(), std::back_inserter(vertices), - [this, &vertexIndex](const GeoData::VertexHandle v) { - vertexIndex.emplace(v, vertexIndex.size()); - const auto p = geoData->point(v); - return Vertex {p, RelativePosition2D(p) / 10000.F, geoData->normal(v)}; - }); - std::for_each( - geoData->faces_sbegin(), 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]; - }); + std::map vertexIndex; + std::for_each(waterPositions.begin(), waterPositions.end(), + [&indices, &vertices, &vertexIndex, extents = geoData->getExtents(), this](const GlobalPosition2D p) { + std::array currentIndices {}; + auto out = currentIndices.begin(); + for (auto x : {0, TILE_SIZE}) { + for (auto y : {0, TILE_SIZE}) { + const auto pos = (p * TILE_SIZE) + GlobalPosition2D {x, y}; + const auto v = vertexIndex.emplace(pos, vertices.size()); + if (v.second) { + const auto cpos = glm::clamp(pos, std::get<0>(extents).xy(), std::get<1>(extents).xy()); + vertices.emplace_back(RelativePosition3D {geoData->positionAt(cpos)}, + TextureRelCoord(pos / TILE_SIZE), up); + } + *out++ = static_cast(v.first->second); + } + } + for (const auto i : {0U, 3U, 1U, 0U, 2U, 3U}) { + indices.push_back(currentIndices[i]); + } }); meshes.create(vertices, indices); } -- cgit v1.2.3