diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-05-06 16:42:08 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-05-06 16:42:08 +0100 |
commit | 72ab29786f6d4c97a92cb95c75091537490e2d0f (patch) | |
tree | 18bf013e3be660b0769cd1a601e670eb79355324 | |
parent | Refactor InstanceVertices in terms of glContainer (diff) | |
download | ilt-72ab29786f6d4c97a92cb95c75091537490e2d0f.tar.bz2 ilt-72ab29786f6d4c97a92cb95c75091537490e2d0f.tar.xz ilt-72ab29786f6d4c97a92cb95c75091537490e2d0f.zip |
Templated BufferedLocation and single buffer storage for RVC locations
-rw-r--r-- | game/vehicles/railVehicle.cpp | 11 | ||||
-rw-r--r-- | game/vehicles/railVehicle.h | 12 | ||||
-rw-r--r-- | game/vehicles/railVehicleClass.cpp | 23 | ||||
-rw-r--r-- | game/vehicles/railVehicleClass.h | 8 | ||||
-rw-r--r-- | gfx/gl/bufferedLocation.cpp | 16 | ||||
-rw-r--r-- | gfx/gl/bufferedLocation.h | 32 | ||||
-rw-r--r-- | test/test-render.cpp | 2 |
7 files changed, 59 insertions, 45 deletions
diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index c4a9e96..2d820b6 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -12,13 +12,12 @@ #include <ray.h> RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : - rvClass {std::move(rvc)}, location {rvClass->instancesBody}, bogies { - rvClass->instancesBogies.front(), - rvClass->instancesBogies.back(), - } + RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, location {&LV::body, *this}, + bogies {{ + {&LV::front, *this, glm::vec3 {0, rvClass->wheelBase / 2.F, 0}}, + {&LV::back, *this, glm::vec3 {0, -rvClass->wheelBase / 2.F, 0}}, + }} { - bogies.front().setPosition({0, rvClass->wheelBase / 2.F, 0}); - bogies.back().setPosition({0, -bogies.front().position().y, 0}); } void diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index 247bf66..f34643e 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -8,12 +8,10 @@ #include <memory> #include <utility> -class SceneShader; -class ShadowMapper; class Ray; - class Train; -class RailVehicle : Selectable { + +class RailVehicle : Selectable, RailVehicleClass::Instance { public: explicit RailVehicle(RailVehicleClassPtr rvc); @@ -22,7 +20,9 @@ public: [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; RailVehicleClassPtr rvClass; - BufferedLocation location; - std::array<BufferedLocation, 2> bogies; + using LV = RailVehicleClass::LocationVertex; + using BLocation = BufferedLocationT<glm::mat4 LV::*, RailVehicleClass::Instance &>; + BLocation location; + std::array<BLocation, 2> bogies; }; using RailVehiclePtr = std::unique_ptr<RailVehicle>; diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 148081f..324148c 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -4,6 +4,7 @@ #include "gfx/gl/vertexArrayObject.h" #include "gfx/models/mesh.h" #include "gfx/models/texture.h" +#include "stream_support.h" #include <algorithm> #include <array> #include <cache.h> @@ -34,36 +35,36 @@ void RailVehicleClass::postLoad() { texture = getTexture(); - bodyMesh->configureVAO(instanceVAO).addAttribs<glm::mat4>(instancesBody.bufferName(), 1); + bodyMesh->configureVAO(instanceVAO).addAttribs<LocationVertex, &LocationVertex::body>(instances.bufferName(), 1); bogies.front() ->configureVAO(instancesBogiesVAO.front()) - .addAttribs<glm::mat4>(instancesBogies.front().bufferName(), 1); + .addAttribs<LocationVertex, &LocationVertex::front>(instances.bufferName(), 1); bogies.back() ->configureVAO(instancesBogiesVAO.back()) - .addAttribs<glm::mat4>(instancesBogies.back().bufferName(), 1); + .addAttribs<LocationVertex, &LocationVertex::back>(instances.bufferName(), 1); } void RailVehicleClass::render(const SceneShader & shader) const { - if (const auto count = instancesBody.size()) { + if (const auto count = static_cast<GLsizei>(instances.size())) { if (texture) { texture->bind(); } shader.basicInst.use(); - bodyMesh->DrawInstanced(instanceVAO, static_cast<GLsizei>(count)); - bogies.front()->DrawInstanced(instancesBogiesVAO.front(), static_cast<GLsizei>(instancesBogies.front().size())); - bogies.back()->DrawInstanced(instancesBogiesVAO.back(), static_cast<GLsizei>(instancesBogies.back().size())); + bodyMesh->DrawInstanced(instanceVAO, count); + bogies.front()->DrawInstanced(instancesBogiesVAO.front(), count); + bogies.back()->DrawInstanced(instancesBogiesVAO.back(), count); } } void RailVehicleClass::shadows(const ShadowMapper & mapper) const { - if (const auto count = instancesBody.size()) { + if (const auto count = static_cast<GLsizei>(instances.size())) { mapper.dynamicPointInst.use(); - bodyMesh->DrawInstanced(instanceVAO, static_cast<GLsizei>(count)); - bogies.front()->DrawInstanced(instancesBogiesVAO.front(), static_cast<GLsizei>(instancesBogies.front().size())); - bogies.back()->DrawInstanced(instancesBogiesVAO.back(), static_cast<GLsizei>(instancesBogies.back().size())); + bodyMesh->DrawInstanced(instanceVAO, count); + bogies.front()->DrawInstanced(instanceVAO, count); + bogies.back()->DrawInstanced(instanceVAO, count); } } diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 2fda8f7..80b3fda 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -18,6 +18,10 @@ public: void render(const SceneShader & shader) const override; void shadows(const ShadowMapper & shadowMapper) const override; + struct LocationVertex { + glm::mat4 body, front, back; + }; + std::array<Mesh::Ptr, 2> bogies; Mesh::Ptr bodyMesh; std::shared_ptr<Texture> texture; @@ -25,8 +29,8 @@ public: float length; float maxSpeed; - mutable InstanceVertices<glm::mat4> instancesBody; - mutable std::array<InstanceVertices<glm::mat4>, 2> instancesBogies; + mutable InstanceVertices<LocationVertex> instances; + using Instance = decltype(instances)::InstanceProxy; protected: friend Persistence::SelectionPtrBase<std::shared_ptr<RailVehicleClass>>; diff --git a/gfx/gl/bufferedLocation.cpp b/gfx/gl/bufferedLocation.cpp index 7027b4c..eb3dac3 100644 --- a/gfx/gl/bufferedLocation.cpp +++ b/gfx/gl/bufferedLocation.cpp @@ -3,15 +3,9 @@ #include "maths.h" #include <glm/gtx/transform.hpp> -BufferedLocation::BufferedLocation(InstanceVertices<glm::mat4> & i, glm::vec3 p, glm::vec3 r) : - BufferedLocation {i, Location {p, r}} -{ -} +BufferedLocation::BufferedLocation(glm::vec3 p, glm::vec3 r) : BufferedLocation {Location {p, r}} { } -BufferedLocation::BufferedLocation(InstanceVertices<glm::mat4> & i, const Location & l) : - loc {l}, buffer {i.acquire(getTransform())} -{ -} +BufferedLocation::BufferedLocation(const Location & l) : loc {l} { } BufferedLocation::operator const Location &() const { @@ -64,12 +58,6 @@ BufferedLocation::setLocation(glm::vec3 p, glm::vec3 r) updateBuffer(); } -void -BufferedLocation::updateBuffer() -{ - buffer = getTransform(); -} - glm::mat4 BufferedLocation::getTransform() const { diff --git a/gfx/gl/bufferedLocation.h b/gfx/gl/bufferedLocation.h index 6d148cd..8096489 100644 --- a/gfx/gl/bufferedLocation.h +++ b/gfx/gl/bufferedLocation.h @@ -1,14 +1,16 @@ #pragma once -#include "instanceVertices.h" #include "location.h" +#include <functional> #include <glm/mat4x4.hpp> #include <glm/vec3.hpp> +#include <tuple> class BufferedLocation { public: - BufferedLocation(InstanceVertices<glm::mat4> &, glm::vec3 = {}, glm::vec3 = {}); - BufferedLocation(InstanceVertices<glm::mat4> &, const Location &); + BufferedLocation(glm::vec3 = {}, glm::vec3 = {}); + BufferedLocation(const Location &); + virtual ~BufferedLocation() = default; BufferedLocation & operator=(const Location &); @@ -23,8 +25,28 @@ public: glm::mat4 getTransform() const; private: - void updateBuffer(); + virtual void updateBuffer() = 0; Location loc; - InstanceVertices<glm::mat4>::InstanceProxy buffer; +}; + +template<typename... Target> class BufferedLocationT : public BufferedLocation { +public: + template<typename... LocationArgs> + BufferedLocationT(Target &&... target, LocationArgs &&... t) : + BufferedLocation {std::forward<LocationArgs>(t)...}, target {std::forward<Target>(target)...} + { + updateBuffer(); + } + + using BufferedLocation::operator=; + +private: + void + updateBuffer() override + { + std::apply(std::invoke<const Target &...>, target) = getTransform(); + } + + std::tuple<Target...> target; }; diff --git a/test/test-render.cpp b/test/test-render.cpp index 4bfcad2..0d384a3 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -21,9 +21,9 @@ #include <ui/window.h> class TestScene : public SceneProvider { - std::shared_ptr<RailVehicle> train1, train2; const RailVehicleClassPtr brush47rvc = std::dynamic_pointer_cast<RailVehicleClass>( AssetFactory::loadXML(RESDIR "/brush47.xml")->assets.at("brush-47")); + std::shared_ptr<RailVehicle> train1, train2; Terrain terrain {[]() { auto gd = std::make_shared<GeoData>(GeoData::Limits {{0, 0}, {100, 100}}); |