summaryrefslogtreecommitdiff
path: root/game/network/rail.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-07 17:00:49 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-07 17:00:49 +0000
commit768a3274e58b7b3cd2f6059edd5953c626fa1703 (patch)
tree817619560a86dec41522036f78bba02b8bfd6200 /game/network/rail.cpp
parentAdd missing virtual destructor in test base class (diff)
downloadilt-768a3274e58b7b3cd2f6059edd5953c626fa1703.tar.bz2
ilt-768a3274e58b7b3cd2f6059edd5953c626fa1703.tar.xz
ilt-768a3274e58b7b3cd2f6059edd5953c626fa1703.zip
Single function for generating the rail link mesh
Diffstat (limited to 'game/network/rail.cpp')
-rw-r--r--game/network/rail.cpp40
1 files changed, 22 insertions, 18 deletions
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 <GL/glew.h>
#include <array>
#include <cache.h>
+#include <cassert>
#include <gfx/gl/shader.h>
#include <gfx/models/texture.h>
#include <gfx/models/vertex.hpp>
@@ -23,6 +24,19 @@ RailLinks::render(const Shader & shader) const
}
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<Mesh>(vertices, indices, GL_TRIANGLE_STRIP);
+}
+
+void
RailLink::render(const Shader &) const
{
meshes.apply(&Mesh::Draw);
@@ -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<Mesh>(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<Mesh>(vertices, indices, GL_TRIANGLE_STRIP);
+ defaultMesh();
}