diff options
Diffstat (limited to 'game/terrain.cpp')
| -rw-r--r-- | game/terrain.cpp | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/game/terrain.cpp b/game/terrain.cpp index f10aac6..f3d7a7d 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -6,7 +6,7 @@ #include <gfx/image.h> #include <gfx/models/mesh.h> #include <gfx/models/vertex.h> -#include <glMappedBufferWriter.h> +#include <glMappedBufferSpan.h> #include <glm/glm.hpp> #include <location.h> #include <maths.h> @@ -16,11 +16,13 @@ static constexpr RGB OPEN_SURFACE {-1}; static constexpr GlobalDistance TILE_SIZE = 1024 * 1024; // ~1km, power of 2, fast divide -template<> -VertexArrayObject & -VertexArrayObject::addAttribsFor<Terrain::Vertex>(const GLuint arrayBuffer, const GLuint divisor) +void +Terrain::initialise() { - return addAttribs<Terrain::Vertex, &Terrain::Vertex::pos, &Terrain::Vertex::normal>(arrayBuffer, divisor); + glDebugScope _ {0}; + vertexArray.configure().addAttribs<Terrain::Vertex, &Terrain::Vertex::pos, &Terrain::Vertex::normal>( + 0, verticesBuffer); + generateMeshes(); } bool @@ -33,7 +35,8 @@ Terrain::SurfaceKey::operator<(const SurfaceKey & other) const inline void Terrain::copyVerticesToBuffer() const { - std::ranges::transform(all_vertices(), glMappedBufferWriter<Vertex> {GL_ARRAY_BUFFER, verticesBuffer, n_vertices()}, + std::ranges::transform(all_vertices(), + glMappedBufferSpan<Vertex> {verticesBuffer, n_vertices(), GL_WRITE_ONLY, true}.begin(), [this](const auto & vertex) { return Vertex {point(vertex), normal(vertex)}; }); @@ -75,21 +78,10 @@ void Terrain::copyIndicesToBuffers(const SurfaceIndices & surfaceIndices) { for (const auto & [surfaceKey, indices] : surfaceIndices) { - auto meshItr = meshes.find(surfaceKey); - if (meshItr == meshes.end()) { - meshItr = meshes.emplace(surfaceKey, SurfaceArrayBuffer {}).first; - VertexArrayObject {meshItr->second.vertexArray} - .addAttribsFor<Vertex>(verticesBuffer) - .addIndices(meshItr->second.indicesBuffer, indices) - .data(verticesBuffer, GL_ARRAY_BUFFER); - } - else { - VertexArrayObject {meshItr->second.vertexArray} - .addIndices(meshItr->second.indicesBuffer, indices) - .data(verticesBuffer, GL_ARRAY_BUFFER); - } - meshItr->second.count = static_cast<GLsizei>(indices.size()); - meshItr->second.aabb = AxisAlignedBoundingBox<GlobalDistance>::fromPoints( + auto & mesh = meshes[surfaceKey]; + mesh.indicesBuffer.data(indices, GL_DYNAMIC_DRAW); + mesh.count = static_cast<GLsizei>(indices.size()); + mesh.aabb = AxisAlignedBoundingBox<GlobalDistance>::fromPoints( indices | std::views::transform([this](const auto vertex) { return this->point(VertexHandle {static_cast<int>(vertex)}); })); @@ -109,6 +101,7 @@ Terrain::pruneOrphanMeshes(const SurfaceIndices & surfaceIndices) void Terrain::generateMeshes() { + glDebugScope _ {0}; copyVerticesToBuffer(); const auto surfaceIndices = mapSurfaceFacesToIndices(); copyIndicesToBuffers(surfaceIndices); @@ -129,7 +122,9 @@ Terrain::afterChange() void Terrain::render(const SceneShader & shader, const Frustum & frustum) const { - grass->bind(); + glDebugScope _ {0}; + glBindVertexArray(vertexArray); + grass->bind(0); const auto chunkBySurface = std::views::chunk_by([](const auto & itr1, const auto & itr2) { return itr1.first.surface == itr2.first.surface; @@ -139,7 +134,7 @@ Terrain::render(const SceneShader & shader, const Frustum & frustum) const shader.landmass.use(surface ? surface->colorBias : OPEN_SURFACE); for (const auto & sab : surfaceRange) { if (frustum.contains(sab.second.aabb)) { - glBindVertexArray(sab.second.vertexArray); + glVertexArrayElementBuffer(vertexArray, sab.second.indicesBuffer); glDrawElements(GL_TRIANGLES, sab.second.count, GL_UNSIGNED_INT, nullptr); } } @@ -150,10 +145,12 @@ Terrain::render(const SceneShader & shader, const Frustum & frustum) const void Terrain::shadows(const ShadowMapper & shadowMapper, const Frustum & frustum) const { + glDebugScope _ {0}; + glBindVertexArray(vertexArray); shadowMapper.landmess.use(); for (const auto & [surface, sab] : meshes) { - if (frustum.shadedBy(sab.aabb)) { - glBindVertexArray(sab.vertexArray); + if (frustum.contains(sab.aabb)) { + glVertexArrayElementBuffer(vertexArray, sab.indicesBuffer); glDrawElements(GL_TRIANGLES, sab.count, GL_UNSIGNED_INT, nullptr); } } |
