diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/geoData.cpp | 166 | ||||
-rw-r--r-- | game/geoData.h | 37 | ||||
-rw-r--r-- | game/surface.cpp | 7 | ||||
-rw-r--r-- | game/surface.h | 11 | ||||
-rw-r--r-- | game/terrain.cpp | 38 | ||||
-rw-r--r-- | game/terrain.h | 9 |
6 files changed, 161 insertions, 107 deletions
diff --git a/game/geoData.cpp b/game/geoData.cpp index 2313342..72aa056 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -4,8 +4,14 @@ #include <fstream> #include <glm/gtx/intersect.hpp> #include <maths.h> +#include <ranges> #include <set> +GeoData::GeoData() +{ + add_property(surface); +} + GeoData GeoData::loadFromAsciiGrid(const std::filesystem::path & input) { @@ -404,7 +410,7 @@ GeoData::triangleContainsTriangle(const Triangle<2> & a, const Triangle<2> & b) return triangleContainsPoint(a.x, b) && triangleContainsPoint(a.y, b) && triangleContainsPoint(a.z, b); } -std::array<GeoData::FaceHandle, 4> +void GeoData::split(FaceHandle _fh) { // Collect halfedges of face @@ -452,16 +458,14 @@ GeoData::split(FaceHandle _fh) } // Retriangulate - return { - add_face(v0, p0, v1), - add_face(p2, v0, v2), - add_face(v2, v1, p1), - add_face(v2, v0, v1), - }; + add_face(v0, p0, v1); + add_face(p2, v0, v2); + add_face(v2, v1, p1); + add_face(v2, v0, v1); } void -GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip) +GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip, const Surface & newFaceSurface) { static const RelativeDistance MAX_SLOPE = 1.5F; static const RelativeDistance MIN_ARC = 0.01F; @@ -479,21 +483,23 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip) return add_vertex(tsVert); }); // Create new faces + const auto initialFaceCount = static_cast<int>(n_faces()); std::for_each(strip_begin(newVerts), strip_end(newVerts), [this](const auto & newVert) { const auto [a, b, c] = newVert; add_face(a, b, c); }); - for (auto start = faces_sbegin(); std::any_of(start, faces_end(), [this, &start](const auto fh) { - static constexpr auto MAX_FACE_AREA = 100'000'000.F; - if (triangle<3>(fh).area() > MAX_FACE_AREA) { - split(fh); - start = FaceIter {*this, FaceHandle(fh), true}; - return true; - } - return false; - });) { - ; + for (auto fhi = FaceIter {*this, FaceHandle {initialFaceCount}, true}; fhi != faces_end(); fhi++) { + static constexpr auto MAX_FACE_AREA = 100'000'000.F; + const auto fh = *fhi; + if (triangle<3>(fh).area() > MAX_FACE_AREA) { + split(fh); + } } + std::vector<FaceHandle> newFaces; + std::copy_if(FaceIter {*this, FaceHandle {initialFaceCount}, true}, faces_end(), std::back_inserter(newFaces), + [this](FaceHandle fh) { + return !this->status(fh).deleted(); + }); // Extrude corners struct Extrusion { @@ -566,15 +572,14 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip) // Cut existing terrain extrusionExtents.emplace_back(extrusionExtents.front()); // Circular next std::vector<std::vector<VertexHandle>> boundaryFaces; - std::adjacent_find(extrusionExtents.begin(), extrusionExtents.end(), - [this, &boundaryFaces](const auto & first, const auto & second) { - const auto p0 = point(first.boundaryVertex); - const auto p1 = point(second.boundaryVertex); - const auto bdir = RelativePosition3D(p1 - p0); - const auto make_plane = [p0](auto y, auto z) { - return GeometricPlaneT<GlobalPosition3D> {p0, crossProduct(y, z)}; - }; - const auto planes = ((first.boundaryVertex == second.boundaryVertex) + for (const auto & [first, second] : extrusionExtents | std::views::adjacent<2>) { + const auto p0 = point(first.boundaryVertex); + const auto p1 = point(second.boundaryVertex); + const auto bdir = RelativePosition3D(p1 - p0); + const auto make_plane = [p0](auto y, auto z) { + return GeometricPlaneT<GlobalPosition3D> {p0, crossProduct(y, z)}; + }; + const auto planes = ((first.boundaryVertex == second.boundaryVertex) ? std::array {make_plane(second.lowerLimit, first.lowerLimit), make_plane(second.upperLimit, first.upperLimit), } @@ -582,58 +587,57 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip) make_plane(bdir, second.lowerLimit), make_plane(bdir, second.upperLimit), }); - assert(planes.front().normal.z > 0.F); - assert(planes.back().normal.z > 0.F); - - auto & out = boundaryFaces.emplace_back(); - out.emplace_back(first.boundaryVertex); - out.emplace_back(first.extrusionVertex); - for (auto currentVertex = first.extrusionVertex; - !find_halfedge(currentVertex, second.extrusionVertex).is_valid();) { - [[maybe_unused]] const auto n = std::any_of( - voh_begin(currentVertex), voh_end(currentVertex), [&](const auto currentVertexOut) { - const auto next = next_halfedge_handle(currentVertexOut); - const auto nextVertex = to_vertex_handle(next); - const auto startVertex = from_vertex_handle(next); - if (nextVertex == *++out.rbegin()) { - // This half edge goes back to the previous vertex - return false; - } - const auto edge = edge_handle(next); - const auto ep0 = point(startVertex); - const auto ep1 = point(nextVertex); - if (planes.front().getRelation(ep1) == GeometricPlane::PlaneRelation::Below - || planes.back().getRelation(ep1) == GeometricPlane::PlaneRelation::Above) { - return false; - } - const auto diff = RelativePosition3D(ep1 - ep0); - const auto length = glm::length(diff); - const auto dir = diff / length; - const Ray r {ep1, -dir}; - const auto dists = planes * [r](const auto & plane) { - RelativeDistance dist {}; - if (r.intersectPlane(plane.origin, plane.normal, dist)) { - return dist; - } - return INFINITY; - }; - const auto dist = *std::min_element(dists.begin(), dists.end()); - const auto splitPos = ep1 - (dir * dist); - if (dist <= length) { - currentVertex = split(edge, splitPos); - out.emplace_back(currentVertex); - return true; - } - return false; - }); - assert(n); - } - out.emplace_back(second.extrusionVertex); - if (first.boundaryVertex != second.boundaryVertex) { - out.emplace_back(second.boundaryVertex); - } - return false; - }); + assert(planes.front().normal.z > 0.F); + assert(planes.back().normal.z > 0.F); + + auto & out = boundaryFaces.emplace_back(); + out.emplace_back(first.boundaryVertex); + out.emplace_back(first.extrusionVertex); + for (auto currentVertex = first.extrusionVertex; + !find_halfedge(currentVertex, second.extrusionVertex).is_valid();) { + [[maybe_unused]] const auto n + = std::any_of(voh_begin(currentVertex), voh_end(currentVertex), [&](const auto currentVertexOut) { + const auto next = next_halfedge_handle(currentVertexOut); + const auto nextVertex = to_vertex_handle(next); + const auto startVertex = from_vertex_handle(next); + if (nextVertex == *++out.rbegin()) { + // This half edge goes back to the previous vertex + return false; + } + const auto edge = edge_handle(next); + const auto ep0 = point(startVertex); + const auto ep1 = point(nextVertex); + if (planes.front().getRelation(ep1) == GeometricPlane::PlaneRelation::Below + || planes.back().getRelation(ep1) == GeometricPlane::PlaneRelation::Above) { + return false; + } + const auto diff = RelativePosition3D(ep1 - ep0); + const auto length = glm::length(diff); + const auto dir = diff / length; + const Ray r {ep1, -dir}; + const auto dists = planes * [r](const auto & plane) { + RelativeDistance dist {}; + if (r.intersectPlane(plane.origin, plane.normal, dist)) { + return dist; + } + return INFINITY; + }; + const auto dist = *std::min_element(dists.begin(), dists.end()); + const auto splitPos = ep1 - (dir * dist); + if (dist <= length) { + currentVertex = split(edge, splitPos); + out.emplace_back(currentVertex); + return true; + } + return false; + }); + assert(n); + } + out.emplace_back(second.extrusionVertex); + if (first.boundaryVertex != second.boundaryVertex) { + out.emplace_back(second.boundaryVertex); + } + } // Remove old faces std::set<FaceHandle> visited; @@ -664,4 +668,8 @@ GeoData::setHeights(const std::span<const GlobalPosition3D> triangleStrip) // Tidy up update_vertex_normals_only(VertexIter {*this, vertex_handle(initialVertexCount), true}); + + std::for_each(newFaces.begin(), newFaces.end(), [&newFaceSurface, this](const auto fh) { + property(surface, fh) = &newFaceSurface; + }); } diff --git a/game/geoData.h b/game/geoData.h index 021b4c7..ed1734c 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -3,6 +3,7 @@ #include "collections.h" // IWYU pragma: keep IterableCollection #include "config/types.h" #include "ray.h" +#include "surface.h" #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh> #include <filesystem> #include <glm/vec2.hpp> @@ -20,7 +21,9 @@ struct GeoDataTraits : public OpenMesh::DefaultTraits { class GeoData : public OpenMesh::TriMesh_ArrayKernelT<GeoDataTraits> { private: - GeoData() = default; + GeoData(); + + OpenMesh::FPropHandleT<const Surface *> surface; public: static GeoData loadFromAsciiGrid(const std::filesystem::path &); @@ -61,47 +64,41 @@ public: }); } - [[nodiscard]] - glm::vec<Dim, GlobalDistance> + [[nodiscard]] glm::vec<Dim, GlobalDistance> operator*(BaryPosition bari) const { return p(0) + (difference(p(0), p(1)) * bari.x) + (difference(p(0), p(2)) * bari.y); } - [[nodiscard]] - auto + [[nodiscard]] auto area() const requires(Dim == 3) { return glm::length(crossProduct(difference(p(0), p(1)), difference(p(0), p(2)))) / 2.F; } - [[nodiscard]] - Normal3D + [[nodiscard]] Normal3D normal() const requires(Dim == 3) { return crossProduct(difference(p(0), p(1)), difference(p(0), p(2))); } - [[nodiscard]] - Normal3D + [[nodiscard]] Normal3D nnormal() const requires(Dim == 3) { return glm::normalize(normal()); } - [[nodiscard]] - auto + [[nodiscard]] auto angle(glm::length_t c) const { return Arc {P(c), P(c + 2), P(c + 1)}.length(); } template<glm::length_t D = Dim> - [[nodiscard]] - auto + [[nodiscard]] auto angleAt(const GlobalPosition<D> pos) const requires(D <= Dim) { @@ -113,8 +110,7 @@ public: return 0.F; } - [[nodiscard]] - inline auto + [[nodiscard]] inline auto p(const glm::length_t i) const { return base::operator[](i); @@ -146,7 +142,7 @@ public: [[nodiscard]] HalfedgeHandle findEntry(const GlobalPosition2D from, const GlobalPosition2D to) const; - void setHeights(const std::span<const GlobalPosition3D> triangleStrip); + void setHeights(const std::span<const GlobalPosition3D> triangleStrip, const Surface &); [[nodiscard]] auto getExtents() const @@ -154,6 +150,13 @@ public: return std::tie(lowerExtent, upperExtent); } + template<typename HandleT> + [[nodiscard]] auto + get_surface(const HandleT h) + { + return property(surface, h); + } + protected: template<glm::length_t Dim> [[nodiscard]] Triangle<Dim> @@ -183,7 +186,7 @@ protected: void update_vertex_normals_only(VertexIter start); using OpenMesh::TriMesh_ArrayKernelT<GeoDataTraits>::split; - std::array<FaceHandle, 4> split(FaceHandle); + void split(FaceHandle); private: GlobalPosition3D lowerExtent {}, upperExtent {}; diff --git a/game/surface.cpp b/game/surface.cpp new file mode 100644 index 0000000..007a9a4 --- /dev/null +++ b/game/surface.cpp @@ -0,0 +1,7 @@ +#include "surface.h" + +bool +Surface::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(colorBias) && STORE_MEMBER(quality) && Asset::persist(store); +} diff --git a/game/surface.h b/game/surface.h new file mode 100644 index 0000000..ccc5c6c --- /dev/null +++ b/game/surface.h @@ -0,0 +1,11 @@ +#pragma once + +#include "assetFactory/asset.h" + +struct Surface : public Asset { + friend Persistence::SelectionPtrBase<std::shared_ptr<Surface>>; + bool persist(Persistence::PersistenceStore & store) override; + + glm::vec3 colorBias; + float quality; +}; diff --git a/game/terrain.cpp b/game/terrain.cpp index 201c732..3b16e79 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -15,11 +15,21 @@ #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(); } +template<> +VertexArrayObject & +VertexArrayObject::addAttribsFor<Terrain::Vertex>(const GLuint arrayBuffer, const GLuint divisor) +{ + return addAttribs<Terrain::Vertex, &Terrain::Vertex::pos, &Terrain::Vertex::normal, &Terrain::Vertex::colourBias>( + arrayBuffer, divisor); +} + void Terrain::generateMeshes() { @@ -27,21 +37,29 @@ Terrain::generateMeshes() indices.reserve(geoData->n_faces() * 3); std::vector<Vertex> vertices; vertices.reserve(geoData->n_vertices()); - std::map<GeoData::VertexHandle, size_t> 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::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->get_surface(f); + if (const auto vertexIndexRef = vertexIndex.emplace(std::make_pair(v, 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](const GeoData::VertexHandle v) { - return vertexIndex[v]; + [&vertexIndex, f, this](const GeoData::VertexHandle v) { + return vertexIndex[std::make_pair(v, geoData->get_surface(f))]; }); }); - meshes.create<Mesh>(vertices, indices); + meshes.create<MeshT<Vertex>>(vertices, indices); } void @@ -60,6 +78,6 @@ Terrain::render(const SceneShader & shader) const void Terrain::shadows(const ShadowMapper & shadowMapper) const { - shadowMapper.fixedPoint.use(); + shadowMapper.landmess.use(); meshes.apply(&Mesh::Draw); } diff --git a/game/terrain.h b/game/terrain.h index 54593fc..d088f89 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -2,6 +2,7 @@ #include "chronology.h" #include "collection.h" +#include "config/types.h" #include "game/worldobject.h" #include <gfx/models/mesh.h> #include <gfx/renderable.h> @@ -20,10 +21,16 @@ public: void tick(TickDuration) override; + struct Vertex { + GlobalPosition3D pos; + Normal3D normal; + RGB colourBias; + }; + private: void generateMeshes(); std::shared_ptr<GeoData> geoData; - Collection<Mesh, false> meshes; + Collection<MeshT<Vertex>, false> meshes; std::shared_ptr<Texture> grass; }; |