From a6e28fe377b41500d6ce3e0a9a4d6e6288d97f61 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 17 Jan 2024 01:08:54 +0000 Subject: Copy render vital link data to vertex buffer --- game/network/network.h | 13 ++++++++++--- game/network/network.impl.h | 40 ++++++++++++++++++++-------------------- game/network/rail.cpp | 31 ++++++++++++++++++++----------- game/network/rail.h | 26 +++++++++++++++++++++----- test/test-network.cpp | 40 ++++++++++++++++++++++++++-------------- 5 files changed, 97 insertions(+), 53 deletions(-) diff --git a/game/network/network.h b/game/network/network.h index fa311ac..12c006c 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -1,5 +1,6 @@ #pragma once +#include "gfx/gl/instanceVertices.h" #include "link.h" #include #include @@ -58,7 +59,13 @@ protected: std::shared_ptr texture; }; -template class NetworkOf : public Network, public Renderable { +template class NetworkLinkHolder { + friend LinkType; + mutable InstanceVertices vertices; +}; + +template +class NetworkOf : public Network, public Renderable, public NetworkLinkHolder... { protected: using Network::Network; @@ -75,7 +82,7 @@ public: requires std::is_base_of_v { const auto node1 = candidateNodeAt(a).first, node2 = candidateNodeAt(b).first; - return std::make_shared(node1, node2, std::forward(params)...); + return std::make_shared(*this, node1, node2, std::forward(params)...); } template @@ -84,7 +91,7 @@ public: requires std::is_base_of_v { const auto node1 = nodeAt(a), node2 = nodeAt(b); - auto l {links.template create(node1, node2, std::forward(params)...)}; + auto l {links.template create(*this, node1, node2, std::forward(params)...)}; joinLinks(l); return l; } diff --git a/game/network/network.impl.h b/game/network/network.impl.h index f9595ed..c1f079d 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -2,9 +2,9 @@ #include #include -template +template void -NetworkOf::render(const SceneShader & shader) const +NetworkOf::render(const SceneShader & shader) const { if constexpr (std::is_base_of_v) { shader.absolute.use(); @@ -13,18 +13,18 @@ NetworkOf::render(const SceneShader & shader) const } } -template +template void -NetworkOf::joinLinks(const Link::Ptr & l) const +NetworkOf::joinLinks(const Link::Ptr & l) const { for (const auto & ol : links.objects) { Network::joinLinks(l, ol); } } -template +template Link::Ptr -NetworkOf::intersectRayLinks(const Ray & ray) const +NetworkOf::intersectRayLinks(const Ray & ray) const { // Click link if (const auto link = std::find_if(links.objects.begin(), links.objects.end(), @@ -37,9 +37,9 @@ NetworkOf::intersectRayLinks(const Ray & ray) const return {}; } -template +template float -NetworkOf::findNodeDirection(Node::AnyCPtr n) const +NetworkOf::findNodeDirection(Node::AnyCPtr n) const { for (const auto & l : links.objects) { for (const auto & e : l->ends) { @@ -52,16 +52,16 @@ NetworkOf::findNodeDirection(Node::AnyCPtr n) const throw std::runtime_error("Node exists but couldn't find it"); } -template +template Link::CCollection -NetworkOf::candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2) +NetworkOf::candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2) { return {candidateLink(n1, n2)}; } -template +template Link::CCollection -NetworkOf::candidateJoins(GlobalPosition3D start, GlobalPosition3D end) +NetworkOf::candidateJoins(GlobalPosition3D start, GlobalPosition3D end) { if (glm::length(RelativePosition3D(start - end)) < 2000.F) { return {}; @@ -73,24 +73,24 @@ NetworkOf::candidateJoins(GlobalPosition3D start, GlobalPosition3D end) return {candidateLink(c1s, c1e, c1c), candidateLink(c2s, c2e, c2c)}; } -template +template Link::CCollection -NetworkOf::candidateExtend(GlobalPosition3D start, GlobalPosition3D end) +NetworkOf::candidateExtend(GlobalPosition3D start, GlobalPosition3D end) { const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(candidateNodeAt(start).first)); return {candidateLink(cstart, cend, centre)}; } -template +template Link::CCollection -NetworkOf::addStraight(GlobalPosition3D n1, GlobalPosition3D n2) +NetworkOf::addStraight(GlobalPosition3D n1, GlobalPosition3D n2) { return {addLink(n1, n2)}; } -template +template Link::CCollection -NetworkOf::addJoins(GlobalPosition3D start, GlobalPosition3D end) +NetworkOf::addJoins(GlobalPosition3D start, GlobalPosition3D end) { if (glm::length(RelativePosition3D(start - end)) < 2000.F) { return {}; @@ -101,9 +101,9 @@ NetworkOf::addJoins(GlobalPosition3D start, GlobalPosition3D end) return {addLink(c1s, c1e, c1c), addLink(c2s, c2e, c2c)}; } -template +template Link::CCollection -NetworkOf::addExtend(GlobalPosition3D start, GlobalPosition3D end) +NetworkOf::addExtend(GlobalPosition3D start, GlobalPosition3D end) { const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(nodeAt(start))); return {addLink(cstart, cend, centre)}; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 79aaf97..3d29ea5 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -13,12 +13,12 @@ #include #include -template class NetworkOf; +template class NetworkOf; constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; constexpr Size3D RAIL_HEIGHT {0, 0, 250.F}; -RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } +RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } void RailLinks::tick(TickDuration) @@ -117,15 +117,20 @@ round_sleepers(const float v) return round_frac(v, sleepers); } -RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : RailLinkStraight(a, b, b->pos - a->pos) +RailLinkStraight::RailLinkStraight( + NetworkLinkHolder & instances, const Node::Ptr & a, const Node::Ptr & b) : + RailLinkStraight(instances, a, b, b->pos - a->pos) { } -RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) : - Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) +RailLinkStraight::RailLinkStraight( + NetworkLinkHolder & instances, Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) : + Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)), + instance {instances.vertices.acquire( + ends[0].node->pos, ends[1].node->pos, flat_orientation(diff), round_sleepers(length / 2.F))} { if (glGenVertexArrays) { - std::vector vertices; + std::vector<::Vertex> vertices; vertices.reserve(2 * railCrossSection.size()); const auto len = round_sleepers(length / 2000.F); const glm::mat3 trans {flat_orientation(diff)}; @@ -139,15 +144,19 @@ RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const RelativePosit } } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) : - RailLinkCurve(a, b, c || a->pos.z, {c || 0, a->pos, b->pos}) +RailLinkCurve::RailLinkCurve( + NetworkLinkHolder & instances, const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) : + RailLinkCurve(instances, a, b, c || a->pos.z, {c || 0, a->pos, b->pos}) { } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition3D c, const Arc arc) : +RailLinkCurve::RailLinkCurve(NetworkLinkHolder & instances, const Node::Ptr & a, const Node::Ptr & b, + GlobalPosition3D c, const Arc arc) : Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)}, glm::length(RelativePosition3D(a->pos - c)) * arc_length(arc)), - LinkCurve {c, glm::length(RelativePosition3D(ends[0].node->pos - c)), arc} + LinkCurve {c, glm::length(RelativePosition3D(ends[0].node->pos - c)), arc}, + instance {instances.vertices.acquire(ends[0].node->pos, ends[1].node->pos, c, round_sleepers(length / 2.F))} + { if (glGenVertexArrays) { const auto & e0p {ends[0].node->pos}; @@ -157,7 +166,7 @@ RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPos const auto step {RelativePosition3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs}; auto segCount = static_cast(std::lround(segs)) + 1; - std::vector vertices; + std::vector<::Vertex> vertices; vertices.reserve(segCount * railCrossSection.size()); for (RelativePosition3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { const auto t { diff --git a/game/network/rail.h b/game/network/rail.h index 986b0aa..e06568f 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -40,23 +40,39 @@ protected: RailLink::~RailLink() = default; +class RailLinks; + class RailLinkStraight : public RailLink, public LinkStraight { public: - RailLinkStraight(const Node::Ptr &, const Node::Ptr &); + RailLinkStraight(NetworkLinkHolder &, const Node::Ptr &, const Node::Ptr &); + + struct Vertex { + GlobalPosition3D a, b; + glm::mat2 rotation; + float textureRepeats; + }; private: - RailLinkStraight(Node::Ptr, Node::Ptr, const RelativePosition3D & diff); + RailLinkStraight(NetworkLinkHolder &, Node::Ptr, Node::Ptr, const RelativePosition3D & diff); + InstanceVertices::InstanceProxy instance; }; class RailLinkCurve : public RailLink, public LinkCurve { public: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition2D); + RailLinkCurve(NetworkLinkHolder &, const Node::Ptr &, const Node::Ptr &, GlobalPosition2D); + + struct Vertex { + GlobalPosition3D a, b, c; + float textureRepeats; + }; private: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition3D, const Arc); + RailLinkCurve( + NetworkLinkHolder &, const Node::Ptr &, const Node::Ptr &, GlobalPosition3D, const Arc); + InstanceVertices::InstanceProxy instance; }; -class RailLinks : public NetworkOf, public WorldObject { +class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); diff --git a/test/test-network.cpp b/test/test-network.cpp index 0274b00..dbc184d 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -23,25 +23,37 @@ BOOST_GLOBAL_FIXTURE(ApplicationBase); BOOST_GLOBAL_FIXTURE(TestMainWindow); -struct TestLink : public LinkStraight { - TestLink(const Node::Ptr & a, const Node::Ptr & b) : TestLink {a, b, (a->pos - b->pos)} { } +struct TestLinkS; - TestLink(Node::Ptr a, Node::Ptr b, RelativePosition2D l) : +struct TestLink : public virtual Link { + using StraightLink = TestLinkS; + using CurveLink = TestLinkS; +}; + +struct TestLinkS : public TestLink, public LinkStraight { + TestLinkS(NetworkLinkHolder & network, const Node::Ptr & a, const Node::Ptr & b) : + TestLinkS {network, a, b, (a->pos - b->pos)} + { + } + + TestLinkS(NetworkLinkHolder &, Node::Ptr a, Node::Ptr b, RelativePosition2D l) : Link {{std::move(a), 0}, {std::move(b), pi}, glm::length(l)} { } - TestLink(Node::Ptr a, Node::Ptr b, float l) : Link {{std::move(a), 0}, {std::move(b), pi}, l} { } + struct Vertex { }; - using StraightLink = TestLink; - using CurveLink = TestLink; + TestLinkS(NetworkLinkHolder &, Node::Ptr a, Node::Ptr b, float l) : + Link {{std::move(a), 0}, {std::move(b), pi}, l} + { + } }; constexpr GlobalPosition3D p000 {0, 0, 0}, p100 {10000, 0, 0}, p200 {20000, 0, 0}, p300 {30000, 0, 0}; constexpr GlobalPosition3D p110 {10000, 10000, 0}; -struct TestNetwork : public NetworkOf { - TestNetwork() : NetworkOf {RESDIR "rails.jpg"} +struct TestNetwork : public NetworkOf { + TestNetwork() : NetworkOf {RESDIR "rails.jpg"} { // 0 1 2 // p000 <-> p100 <-> p200 <-> p300 @@ -49,12 +61,12 @@ struct TestNetwork : public NetworkOf { // \ 5 / // 3 | 4 // \-> p110 <-/ - addLink(p000, p100, 1.F); - addLink(p100, p200, 1.F); - addLink(p200, p300, 1.F); - addLink(p000, p110, 2.F); - addLink(p200, p110, 2.F); - addLink(p100, p110, 1.F); + addLink(p000, p100, 1.F); + addLink(p100, p200, 1.F); + addLink(p200, p300, 1.F); + addLink(p000, p110, 2.F); + addLink(p200, p110, 2.F); + addLink(p100, p110, 1.F); } }; -- cgit v1.2.3