From af6b27fa0a3b16d7d0fe7f7be33109af1dcf5f88 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 7 Mar 2025 20:35:42 +0000 Subject: Construct terrain tile AxisAlignedBoundingBox during mesh generation No surface is simply the tile bounds, but with a surface, it's constrained to just the bounds of the surface itself. --- game/terrain.cpp | 14 +++++++++++--- game/terrain.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'game') diff --git a/game/terrain.cpp b/game/terrain.cpp index 9202d20..dae295a 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -77,6 +77,16 @@ Terrain::generateMeshes() .data(verticesBuffer, GL_ARRAY_BUFFER); } meshItr->second.count = static_cast(indices.size()); + if (!surfaceKey.surface) { + meshItr->second.aabb = {{surfaceKey.basePosition * TILE_SIZE || getExtents().min.z}, + {(surfaceKey.basePosition + 1) * TILE_SIZE || getExtents().max.z}}; + } + else { + meshItr->second.aabb = AxisAlignedBoundingBox::fromPoints( + indices | std::views::transform([this](const auto vertex) -> GlobalPosition3D { + return this->point(VertexHandle {static_cast(vertex)}); + })); + } } if (meshes.size() > surfaceIndices.size()) { std::erase_if(meshes, [&surfaceIndices](const auto & mesh) { @@ -102,9 +112,7 @@ Terrain::render(const SceneShader & shader, const Frustum & frustum) const grass->bind(); std::ranges::for_each(meshes, [ext = getExtents(), &frustum](const auto & surfaceDef) { - const AxisAlignedBoundingBox tileAabb {{surfaceDef.first.basePosition * TILE_SIZE || ext.min.z}, - {(surfaceDef.first.basePosition + 1) * TILE_SIZE || ext.max.z}}; - surfaceDef.second.visible = frustum.contains(tileAabb); + surfaceDef.second.visible = frustum.contains(surfaceDef.second.aabb); }); const auto chunkBySurface = std::views::chunk_by([](const auto & itr1, const auto & itr2) { diff --git a/game/terrain.h b/game/terrain.h index 40a5989..1d00f97 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -35,6 +35,7 @@ private: glVertexArray vertexArray; glBuffer indicesBuffer; GLsizei count; + AxisAlignedBoundingBox aabb; mutable bool visible; }; -- cgit v1.2.3