From 55e41471e7d4bac221aa82ca9504df7bec43e666 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Mar 2021 01:05:35 +0000 Subject: Vertices and indices vectors not required once mesh is created --- game/network/rail.cpp | 22 +++++++++++----------- game/network/rail.h | 11 ++++------- 2 files changed, 15 insertions(+), 18 deletions(-) (limited to 'game/network') 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 #include -#include +#include #include #include // IWYU pragma: keep #include @@ -12,6 +12,7 @@ #include #include #include +#include template class NetworkOf; @@ -102,23 +103,22 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) return addLink(start, end, centre.first); } -void -RailLink::defaultMesh() +MeshPtr +RailLink::defaultMesh(const std::span vertices) { + std::vector 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(vertices, indices, GL_TRIANGLE_STRIP); + return std::make_unique(vertices, indices, GL_TRIANGLE_STRIP); } void RailLink::render(const Shader &) const { - meshes.apply(&Mesh::Draw); + mesh->Draw(); } constexpr const std::array, 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 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 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 #include #include +#include #include -#include 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 vertices); - Collection meshes; - std::vector vertices; - std::vector indices; + MeshPtr mesh; }; class RailLinkStraight : public RailLink { -- cgit v1.2.3