diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-02-09 20:14:51 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-02-09 20:14:54 +0000 |
commit | 42e16ad8ad853c6e97d7eb6718ee0f78b868be30 (patch) | |
tree | 3a91a5922b3557132da271339a665576a7ad714a /game | |
parent | Split GeoData mesh basics into a subclass (diff) | |
download | ilt-42e16ad8ad853c6e97d7eb6718ee0f78b868be30.tar.bz2 ilt-42e16ad8ad853c6e97d7eb6718ee0f78b868be30.tar.xz ilt-42e16ad8ad853c6e97d7eb6718ee0f78b868be30.zip |
Combine GeoData and Terrain class hierarchies
Diffstat (limited to 'game')
-rw-r--r-- | game/gamestate.h | 4 | ||||
-rw-r--r-- | game/terrain.cpp | 54 | ||||
-rw-r--r-- | game/terrain.h | 13 |
3 files changed, 30 insertions, 41 deletions
diff --git a/game/gamestate.h b/game/gamestate.h index 892aa69..189417d 100644 --- a/game/gamestate.h +++ b/game/gamestate.h @@ -6,7 +6,7 @@ #include <special_members.h> class WorldObject; -class GeoData; +class Terrain; class Environment; class GameState { @@ -17,7 +17,7 @@ public: NO_COPY(GameState); Collection<WorldObject> world; - std::shared_ptr<GeoData> geoData; + std::shared_ptr<Terrain> terrain; std::shared_ptr<Environment> environment; AssetFactory::Assets assets; }; diff --git a/game/terrain.cpp b/game/terrain.cpp index c834379..786b9b0 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -1,6 +1,5 @@ #include "terrain.h" #include "game/geoData.h" -#include "gfx/models/texture.h" #include <algorithm> #include <cstddef> #include <gfx/gl/sceneShader.h> @@ -15,12 +14,7 @@ #include <utility> #include <vector> -static constexpr RGB openSurface {-1}; - -Terrain::Terrain(std::shared_ptr<GeoData> tm) : geoData {std::move(tm)}, grass {std::make_shared<Texture>("grass.png")} -{ - generateMeshes(); -} +static constexpr RGB OPEN_SURFACE {-1}; template<> VertexArrayObject & @@ -35,38 +29,34 @@ Terrain::generateMeshes() { meshes.removeAll(); std::vector<unsigned int> indices; - indices.reserve(geoData->n_faces() * 3); + indices.reserve(n_faces() * 3); std::vector<Vertex> vertices; - vertices.reserve(geoData->n_vertices()); - std::map<std::pair<GeoData::VertexHandle, const Surface *>, size_t> vertexIndex; - std::for_each(geoData->vertices_sbegin(), geoData->vertices_end(), - [this, &vertexIndex, &vertices](const GeoData::VertexHandle v) { - std::for_each(geoData->vf_begin(v), geoData->vf_end(v), - [&vertexIndex, v, this, &vertices](const GeoData::FaceHandle f) { - const auto surface = geoData->getSurface(f); - if (const auto vertexIndexRef = vertexIndex.emplace(std::make_pair(v, surface), 0); - vertexIndexRef.second) { - vertexIndexRef.first->second = vertices.size(); + vertices.reserve(n_vertices()); + std::map<std::pair<VertexHandle, const Surface *>, size_t> vertexIndex; + std::ranges::for_each(this->vertices(), [this, &vertexIndex, &vertices](const auto vertex) { + std::ranges::for_each(vf_range(vertex), [&vertexIndex, vertex, this, &vertices](const auto face) { + const auto * const surface = getSurface(face); + if (const auto vertexIndexRef = vertexIndex.emplace(std::make_pair(vertex, surface), 0); + vertexIndexRef.second) { + vertexIndexRef.first->second = vertices.size(); - vertices.emplace_back(geoData->point(v), geoData->normal(v), - surface ? surface->colorBias : openSurface); - } - }); - }); - 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, f, this](const GeoData::VertexHandle v) { - return vertexIndex[std::make_pair(v, geoData->getSurface(f))]; - }); - }); + vertices.emplace_back(point(vertex), normal(vertex), surface ? surface->colorBias : OPEN_SURFACE); + } + }); + }); + std::ranges::for_each(faces(), [this, &vertexIndex, &indices](const auto face) { + std::ranges::transform( + fv_range(face), std::back_inserter(indices), [&vertexIndex, face, this](const auto vertex) { + return vertexIndex[std::make_pair(vertex, getSurface(face))]; + }); + }); meshes.create<MeshT<Vertex>>(vertices, indices); } void Terrain::tick(TickDuration) { - if (const auto newGeneration = geoData->getGeneration(); newGeneration != geoGeneration) { + if (const auto newGeneration = getGeneration(); newGeneration != geoGeneration) { generateMeshes(); geoGeneration = newGeneration; } @@ -77,9 +67,7 @@ Terrain::render(const SceneShader & shader) const { shader.landmass.use(); grass->bind(); - // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); meshes.apply(&Mesh::Draw); - // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } void diff --git a/game/terrain.h b/game/terrain.h index c9d09fe..7464bdd 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -4,17 +4,19 @@ #include "collection.h" #include "config/types.h" #include "game/worldobject.h" +#include "geoData.h" #include "gfx/models/mesh.h" #include "gfx/models/texture.h" #include "gfx/renderable.h" -#include <memory> class SceneShader; -class GeoData; -class Terrain : public WorldObject, public Renderable { +class Terrain : public GeoData, public WorldObject, public Renderable { public: - explicit Terrain(std::shared_ptr<GeoData>); + template<typename... P> explicit Terrain(P &&... params) : GeoData {std::forward<P>(params)...} + { + generateMeshes(); + } void render(const SceneShader & shader) const override; void shadows(const ShadowMapper &) const override; @@ -30,8 +32,7 @@ public: private: void generateMeshes(); - std::shared_ptr<GeoData> geoData; Collection<MeshT<Vertex>, false> meshes; - Texture::Ptr grass; + Texture::Ptr grass = std::make_shared<Texture>("grass.png"); size_t geoGeneration {}; }; |