From 538af692b66e67dfa4f347cc9ddd784a36f9a991 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jan 2024 01:23:53 +0000 Subject: Update network with vertex array --- game/network/network.h | 5 ++++- game/network/rail.cpp | 37 +++++++++++++++++++++++++++++-------- game/network/rail.h | 3 +++ test/test-network.cpp | 2 ++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/game/network/network.h b/game/network/network.h index 49322f9..5725360 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -60,8 +60,11 @@ protected: }; template class NetworkLinkHolder { -protected: +public: + // Implemented per LinkType to configure vao + NetworkLinkHolder(); friend LinkType; + glVertexArray vao; mutable InstanceVertices vertices; }; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 9766851..efe4e2d 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -186,20 +186,41 @@ RailLink::vehiclePositionOffset() const return RAIL_HEIGHT; } -void -RailLinks::render(const SceneShader & shader) const +template<> NetworkLinkHolder::NetworkLinkHolder() +{ + VertexArrayObject {vao} + .addAttribs( + vertices.bufferName()); +} + +template<> NetworkLinkHolder::NetworkLinkHolder() { - auto renderType = [](auto & v, auto & s) { - if (auto count = v.size()) { + VertexArrayObject {vao} + .addAttribs(vertices.bufferName()); +} + +namespace { + template + void + renderType(const NetworkLinkHolder & n, auto & s) + { + if (auto count = n.vertices.size()) { s.use(); - glBindBuffer(GL_VERTEX_ARRAY, v.bufferName()); + glBindVertexArray(n.vao); glDrawArrays(GL_POINTS, 0, static_cast(count)); } }; +} + +void +RailLinks::render(const SceneShader & shader) const +{ if (!links.objects.empty()) { texture->bind(); - renderType(NetworkLinkHolder::vertices, shader.networkStraight); - renderType(NetworkLinkHolder::vertices, shader.networkCurve); - glBindBuffer(GL_VERTEX_ARRAY, 0); + renderType(*this, shader.networkStraight); + renderType(*this, shader.networkCurve); + glBindVertexArray(0); } } diff --git a/game/network/rail.h b/game/network/rail.h index 5191b90..b6a9ebe 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -72,6 +72,9 @@ private: InstanceVertices::InstanceProxy instance; }; +template<> NetworkLinkHolder::NetworkLinkHolder(); +template<> NetworkLinkHolder::NetworkLinkHolder(); + class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); diff --git a/test/test-network.cpp b/test/test-network.cpp index df61b94..174e2a5 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -52,6 +52,8 @@ struct TestLinkS : public TestLink, public LinkStraight { constexpr GlobalPosition3D p000 {0, 0, 0}, p100 {10000, 0, 0}, p200 {20000, 0, 0}, p300 {30000, 0, 0}; constexpr GlobalPosition3D p110 {10000, 10000, 0}; +template<> NetworkLinkHolder::NetworkLinkHolder() = default; + struct TestNetwork : public NetworkOf { TestNetwork() : NetworkOf {RESDIR "rails.jpg"} { -- cgit v1.2.3