diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-10 01:05:35 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-10 01:05:35 +0000 |
commit | 55e41471e7d4bac221aa82ca9504df7bec43e666 (patch) | |
tree | cf644dafd56b4be81f594afa7ca435f91466cfa5 /game | |
parent | Push RailLinks functionality into new base classes Network and NetworkOf<> (diff) | |
download | ilt-55e41471e7d4bac221aa82ca9504df7bec43e666.tar.bz2 ilt-55e41471e7d4bac221aa82ca9504df7bec43e666.tar.xz ilt-55e41471e7d4bac221aa82ca9504df7bec43e666.zip |
Vertices and indices vectors not required once mesh is created
Diffstat (limited to 'game')
-rw-r--r-- | game/network/rail.cpp | 22 | ||||
-rw-r--r-- | game/network/rail.h | 11 |
2 files changed, 15 insertions, 18 deletions
diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 133b80c..c84f97f 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -2,7 +2,7 @@ #include "network.h" #include <GL/glew.h> #include <array> -#include <cassert> +#include <collection.hpp> #include <game/network/link.h> #include <game/network/network.impl.h> // IWYU pragma: keep #include <gfx/models/vertex.hpp> @@ -12,6 +12,7 @@ #include <maths.h> #include <stdexcept> #include <utility> +#include <vector> template class NetworkOf<RailLink>; @@ -102,23 +103,22 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) return addLink<RailLinkCurve>(start, end, centre.first); } -void -RailLink::defaultMesh() +MeshPtr +RailLink::defaultMesh(const std::span<Vertex> vertices) { + std::vector<unsigned int> indices; for (auto n = RAIL_CROSSSECTION_VERTICES; n < vertices.size(); n += 1) { indices.push_back(n - RAIL_CROSSSECTION_VERTICES); indices.push_back(n); } - assert(vertices.capacity() == vertices.size()); - assert(indices.capacity() == indices.size()); - meshes.create<Mesh>(vertices, indices, GL_TRIANGLE_STRIP); + return std::make_unique<Mesh>(vertices, indices, GL_TRIANGLE_STRIP); } void RailLink::render(const Shader &) const { - meshes.apply(&Mesh::Draw); + mesh->Draw(); } constexpr const std::array<std::pair<glm::vec3, float>, RAIL_CROSSSECTION_VERTICES> railCrossSection {{ @@ -144,8 +144,8 @@ RailLinkStraight::RailLinkStraight(const NodePtr & a, const NodePtr & b) : RailL RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) : RailLink({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) { + std::vector<Vertex> vertices; vertices.reserve(2 * railCrossSection.size()); - indices.reserve(2 * railCrossSection.size()); const auto len = round_sleepers(length / 2.F); const auto e {flat_orientation(diff)}; for (int ei = 0; ei < 2; ei++) { @@ -155,7 +155,7 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) vertices.emplace_back(m, glm::vec2 {rcs.second, ei ? len : 0.F}, up); } } - defaultMesh(); + mesh = defaultMesh(vertices); } Location @@ -185,8 +185,8 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, const auto trans {glm::translate(centreBase)}; int segCount = segs; + std::vector<Vertex> vertices; vertices.reserve((segCount + 1) * railCrossSection.size()); - indices.reserve(segCount * 2 * railCrossSection.size()); for (glm::vec3 swing = {arc.second, e1p.y - centreBase.y, 0.F}; segCount >= 0; swing += step, --segCount) { const auto t {trans * glm::rotate(swing.x - half_pi, up) * glm::translate(glm::vec3 {radius, swing.y, 0.F})}; for (const auto & rcs : railCrossSection) { @@ -194,7 +194,7 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, vertices.emplace_back(m, glm::vec2 {rcs.second, swing.z}, up); } } - defaultMesh(); + mesh = defaultMesh(vertices); } Location diff --git a/game/network/rail.h b/game/network/rail.h index 1d0ef68..c67dc67 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -1,10 +1,8 @@ #ifndef RAILLINKS_H #define RAILLINKS_H -#include "collection.hpp" #include "game/worldobject.h" #include "gfx/models/mesh.h" -#include "gfx/models/vertex.hpp" #include "gfx/renderable.h" #include "link.h" #include "network.h" @@ -13,10 +11,11 @@ #include <maths.h> #include <memory> #include <set> +#include <span> #include <utility> -#include <vector> class Shader; +class Vertex; // A piece of rail track class RailLink : public Link, public Renderable { @@ -26,11 +25,9 @@ public: void render(const Shader &) const override; protected: - void defaultMesh(); + [[nodiscard]] static MeshPtr defaultMesh(const std::span<Vertex> vertices); - Collection<Mesh, false> meshes; - std::vector<Vertex> vertices; - std::vector<unsigned int> indices; + MeshPtr mesh; }; class RailLinkStraight : public RailLink { |