From 768a3274e58b7b3cd2f6059edd5953c626fa1703 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Feb 2021 17:00:49 +0000 Subject: Single function for generating the rail link mesh --- game/network/rail.cpp | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'game/network/rail.cpp') diff --git a/game/network/rail.cpp b/game/network/rail.cpp index e66b5c8..299c647 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,19 @@ RailLinks::render(const Shader & shader) const links.apply(&RailLink::render, shader); } +void +RailLink::defaultMesh() +{ + for (auto n = 4U; n < vertices.size(); n += 1) { + indices.push_back(n - 4); + indices.push_back(n); + } + + assert(vertices.capacity() == vertices.size()); + assert(indices.capacity() == indices.size()); + meshes.create(vertices, indices, GL_TRIANGLE_STRIP); +} + void RailLink::render(const Shader &) const { @@ -59,13 +73,9 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) for (const auto & rcs : railCrossSection) { const glm::vec3 m {(trans * glm::vec4 {rcs.first, 1})}; vertices.emplace_back(m, glm::vec2 {rcs.second, ei ? len : 0.F}, up); - if (vertices.size() > railCrossSection.size()) { - indices.push_back(vertices.size() - railCrossSection.size() - 1); - indices.push_back(vertices.size() - 1); - } } } - meshes.create(vertices, indices, GL_TRIANGLE_STRIP); + defaultMesh(); } RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec2 c) : @@ -86,21 +96,15 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, const auto step {glm::vec3 {arc_length(arc), e1p.y - e0p.y, slength} / segs}; const auto trans {glm::translate(centreBase)}; - auto addRcs = [this, trans, radius](auto arc) { - const auto t {trans * glm::rotate(half_pi - arc.x, up) * glm::translate(glm::vec3 {radius, arc.y, 0.F})}; + int segCount = segs; + vertices.reserve((segCount + 1) * railCrossSection.size()); + indices.reserve(segCount * 2 * railCrossSection.size()); + for (glm::vec3 swing = {arc.first, 0.F, 0.F}; segCount >= 0; swing += step, --segCount) { + const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, swing.y, 0.F})}; for (const auto & rcs : railCrossSection) { const glm::vec3 m {(t * glm::vec4 {rcs.first, 1})}; - vertices.emplace_back(m, glm::vec2 {rcs.second, arc.z}, up); + vertices.emplace_back(m, glm::vec2 {rcs.second, swing.z}, up); } - }; - for (glm::vec3 swing = {arc.first, 0.F, 0.F}; swing.x < arc.second; swing += step) { - addRcs(swing); - } - addRcs(glm::vec3 {arc.second, e1p.y - e0p.y, slength}); - - for (auto n = 4U; n < vertices.size(); n += 1) { - indices.push_back(n - 4); - indices.push_back(n); } - meshes.create(vertices, indices, GL_TRIANGLE_STRIP); + defaultMesh(); } -- cgit v1.2.3