From a2e87cf25f15ec8d6184a0aefc7ad949fb89b52b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Apr 2023 21:10:46 +0100 Subject: Use Location::getTransform in Plant instead of duplicating --- game/scenary/plant.cpp | 6 +++--- game/scenary/plant.h | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'game') diff --git a/game/scenary/plant.cpp b/game/scenary/plant.cpp index 678d4a7..2a79807 100644 --- a/game/scenary/plant.cpp +++ b/game/scenary/plant.cpp @@ -1,7 +1,7 @@ #include "plant.h" +#include "location.hpp" -Plant::Plant(std::shared_ptr type, Location position) : - type {std::move(type)}, - location {this->type->instances.acquire(glm::translate(position.pos) * rotate_ypr(position.rot))} +Plant::Plant(std::shared_ptr type, const Location & position) : + type {std::move(type)}, location {this->type->instances.acquire(position.getTransform())} { } diff --git a/game/scenary/plant.h b/game/scenary/plant.h index 77c5979..82ab0e5 100644 --- a/game/scenary/plant.h +++ b/game/scenary/plant.h @@ -2,9 +2,8 @@ #include "foliage.h" #include "game/worldobject.h" -#include "location.hpp" -#include "maths.h" -#include + +class Location; class Plant : public WorldObject { std::shared_ptr type; @@ -16,5 +15,5 @@ class Plant : public WorldObject { } public: - Plant(std::shared_ptr type, Location position); + Plant(std::shared_ptr type, const Location & position); }; -- cgit v1.2.3 From 687afb09ada73fb4aee932242a3b7ee492e63cb0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Apr 2023 21:31:41 +0100 Subject: Handle rendering of RailVehicles through RailVehicleClass instancing Trains no longer need rendering as their parts are all RVC assets and that just works now. This commit creates some irritating duplication in RVC wrt body/2 bogies all of which are stored separately, but function in exactly the same way. This breaks RV shadows as we have no instancing shadow shaders yet. --- game/vehicles/railVehicle.cpp | 29 +++++++++---------------- game/vehicles/railVehicle.h | 12 ++++------- game/vehicles/railVehicleClass.cpp | 44 ++++++++++++++++++++++---------------- game/vehicles/railVehicleClass.h | 15 ++++++++++--- game/vehicles/train.cpp | 11 ---------- game/vehicles/train.h | 3 --- game/vehicles/vehicle.h | 2 +- 7 files changed, 53 insertions(+), 63 deletions(-) (limited to 'game') diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 77907be..b77b93c 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -11,22 +11,14 @@ #include #include -RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : rvClass {std::move(rvc)} +RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : + rvClass {std::move(rvc)}, location {rvClass->instancesBody}, bogies { + rvClass->instancesBogies.front(), + rvClass->instancesBogies.back(), + } { - bogies.front().pos.y = rvClass->wheelBase / 2.F; - bogies.back().pos.y = -bogies.front().pos.y; -} - -void -RailVehicle::render(const SceneShader & shader) const -{ - rvClass->render(shader, location, bogies); -} - -void -RailVehicle::shadows(const ShadowMapper & shadowMapper) const -{ - rvClass->shadows(shadowMapper, location, bogies); + bogies.front().setPosition({0, rvClass->wheelBase / 2.F, 0}); + bogies.back().setPosition({0, -bogies.front().position().y, 0}); } void @@ -35,9 +27,8 @@ RailVehicle::move(const Train * t, float & trailBy) const auto overhang {(rvClass->length - rvClass->wheelBase) / 2}; const auto & b1Pos = bogies[0] = t->getBogiePosition(t->linkDist, trailBy += overhang); const auto & b2Pos = bogies[1] = t->getBogiePosition(t->linkDist, trailBy += rvClass->wheelBase); - const auto diff = glm::normalize(b2Pos.pos - b1Pos.pos); - location.pos = (b1Pos.pos + b2Pos.pos) / 2.F; - location.rot = {vector_pitch(diff), vector_yaw(diff), 0}; + const auto diff = glm::normalize(b2Pos.position() - b1Pos.position()); + location.setLocation((b1Pos.position() + b2Pos.position()) / 2.F, {vector_pitch(diff), vector_yaw(diff), 0}); trailBy += 0.6F + overhang; } @@ -47,7 +38,7 @@ RailVehicle::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * distance constexpr const auto X = 1.35F; const auto Y = this->rvClass->length / 2.F; constexpr const auto Z = 3.9F; - const auto moveBy = glm::translate(location.pos) * rotate_ypr(location.rot); + const auto moveBy = location.getTransform(); const std::array cornerVertices {{ moveBy * glm::vec4 {-X, Y, 0, 1}, // LFB moveBy * glm::vec4 {X, Y, 0, 1}, // RFB diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index bbf4df9..247bf66 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -1,11 +1,10 @@ #pragma once -#include "gfx/renderable.h" +#include "gfx/gl/bufferedLocation.h" #include "railVehicleClass.h" #include #include #include -#include #include #include @@ -14,19 +13,16 @@ class ShadowMapper; class Ray; class Train; -class RailVehicle : public Renderable, Selectable { +class RailVehicle : Selectable { public: explicit RailVehicle(RailVehicleClassPtr rvc); void move(const Train *, float & trailBy); - void render(const SceneShader & shader) const override; - void shadows(const ShadowMapper & shadowMapper) const override; [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; - Location location; - RailVehicleClassPtr rvClass; - std::array bogies; + BufferedLocation location; + std::array bogies; }; using RailVehiclePtr = std::unique_ptr; diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index b7a3b1e..3e62bf1 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -1,6 +1,7 @@ #include "railVehicleClass.h" #include "gfx/gl/sceneShader.h" #include "gfx/gl/shadowMapper.h" +#include "gfx/gl/vertexArrayObject.hpp" #include "gfx/models/mesh.h" #include "gfx/models/texture.h" #include @@ -33,30 +34,37 @@ void RailVehicleClass::postLoad() { texture = getTexture(); + bodyMesh->configureVAO(instanceVAO).addAttribs(instancesBody.bufferName(), 1); + bogies.front() + ->configureVAO(instancesBogiesVAO.front()) + .addAttribs(instancesBogies.front().bufferName(), 1); + bogies.back() + ->configureVAO(instancesBogiesVAO.back()) + .addAttribs(instancesBogies.back().bufferName(), 1); } void -RailVehicleClass::render( - const SceneShader & shader, const Location & location, const std::array & bl) const +RailVehicleClass::render(const SceneShader & shader) const { - shader.basic.use(location); - if (texture) { - texture->bind(); - } - bodyMesh->Draw(); - for (auto b = 0U; b < bogies.size(); ++b) { - shader.basic.setModel(bl[b]); - bogies[b]->Draw(); + if (const auto count = instancesBody.count()) { + if (texture) { + texture->bind(); + } + shader.basicInst.use(); + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(instancesBogiesVAO.front()); + glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.front().count())); + glBindVertexArray(instancesBogiesVAO.back()); + glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.back().count())); + glBindVertexArray(0); } } + void -RailVehicleClass::shadows( - const ShadowMapper & shadowMapper, const Location & location, const std::array & bl) const +RailVehicleClass::shadows(const ShadowMapper &) const { - shadowMapper.dynamicPoint.use(location); - bodyMesh->Draw(); - for (auto b = 0U; b < bogies.size(); ++b) { - shadowMapper.dynamicPoint.setModel(bl[b]); - bogies[b]->Draw(); - } } diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 1a52e2b..2fda8f7 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -1,7 +1,9 @@ #pragma once #include "assetFactory/asset.h" +#include "gfx/gl/instanceVertices.h" #include "gfx/models/mesh.h" +#include "gfx/renderable.h" #include #include #include @@ -11,10 +13,10 @@ class ShadowMapper; class Texture; class Location; -class RailVehicleClass : public Asset { +class RailVehicleClass : public Renderable, public Asset { public: - void render(const SceneShader &, const Location &, const std::array &) const; - void shadows(const ShadowMapper &, const Location &, const std::array &) const; + void render(const SceneShader & shader) const override; + void shadows(const ShadowMapper & shadowMapper) const override; std::array bogies; Mesh::Ptr bodyMesh; @@ -23,9 +25,16 @@ public: float length; float maxSpeed; + mutable InstanceVertices instancesBody; + mutable std::array, 2> instancesBogies; + protected: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; void postLoad() override; + +private: + glVertexArray instanceVAO; + std::array instancesBogiesVAO; }; using RailVehicleClassPtr = std::shared_ptr; diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 0332c04..fdfc3e1 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -11,17 +11,6 @@ class Ray; -void -Train::render(const SceneShader & shader) const -{ - apply(&Renderable::render, shader); -} -void -Train::shadows(const ShadowMapper & shadowMapper) const -{ - apply(&Renderable::shadows, shadowMapper); -} - Location Train::getBogiePosition(float linkDist, float dist) const { diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 91f7ddb..15e2f6e 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -27,9 +27,6 @@ public: return objects.front()->location; } - void render(const SceneShader & shader) const override; - void shadows(const ShadowMapper & shadowMapper) const override; - [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; void tick(TickDuration elapsed) override; diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index 3fa3092..369bd7a 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -12,7 +12,7 @@ class Location; -class Vehicle : public WorldObject, public Renderable, public Selectable { +class Vehicle : public WorldObject, public Selectable { public: explicit Vehicle(const Link::Ptr & link, float linkDist = 0); float linkDist; // distance along current link -- cgit v1.2.3 From e751ab465bbbf1365ec6e108c64dafd03ce42f1d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Apr 2023 21:46:21 +0100 Subject: Pop and complete instanced shadow support --- game/scenary/foliage.cpp | 9 ++++++++- game/vehicles/railVehicleClass.cpp | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 35be051..e54dfbd 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -34,6 +34,13 @@ Foliage::render(const SceneShader & shader) const } void -Foliage::shadows(const ShadowMapper &) const +Foliage::shadows(const ShadowMapper & mapper) const { + if (const auto count = instances.count()) { + mapper.dynamicPointInst.use(); + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(0); + } } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 3e62bf1..66d7ba3 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -65,6 +65,19 @@ RailVehicleClass::render(const SceneShader & shader) const } void -RailVehicleClass::shadows(const ShadowMapper &) const +RailVehicleClass::shadows(const ShadowMapper & mapper) const { + if (const auto count = instancesBody.count()) { + mapper.dynamicPointInst.use(); + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(instancesBogiesVAO.front()); + glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.front().count())); + glBindVertexArray(instancesBogiesVAO.back()); + glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.back().count())); + glBindVertexArray(0); + } } -- cgit v1.2.3 From c96f4f89905f96492bdfc87bc3d45bf5cbd7b005 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 26 Apr 2023 01:42:45 +0100 Subject: Draw rail vehicle classes and foliage with new mesh instance helper --- game/scenary/foliage.cpp | 10 ++-------- game/vehicles/railVehicleClass.cpp | 28 ++++++++-------------------- 2 files changed, 10 insertions(+), 28 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index e54dfbd..0913c6e 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -26,10 +26,7 @@ Foliage::render(const SceneShader & shader) const if (texture) { texture->bind(); } - glBindVertexArray(instanceVAO); - glDrawElementsInstanced( - bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); - glBindVertexArray(0); + bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); } } @@ -38,9 +35,6 @@ Foliage::shadows(const ShadowMapper & mapper) const { if (const auto count = instances.count()) { mapper.dynamicPointInst.use(); - glBindVertexArray(instanceVAO); - glDrawElementsInstanced( - bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); - glBindVertexArray(0); + bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); } } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 66d7ba3..82a442e 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -51,16 +51,10 @@ RailVehicleClass::render(const SceneShader & shader) const texture->bind(); } shader.basicInst.use(); - glBindVertexArray(instanceVAO); - glDrawElementsInstanced( - bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); - glBindVertexArray(instancesBogiesVAO.front()); - glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr, - static_cast(instancesBogies.front().count())); - glBindVertexArray(instancesBogiesVAO.back()); - glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr, - static_cast(instancesBogies.back().count())); - glBindVertexArray(0); + bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + bogies.front()->DrawInstanced( + instancesBogiesVAO.front(), static_cast(instancesBogies.front().count())); + bogies.back()->DrawInstanced(instancesBogiesVAO.back(), static_cast(instancesBogies.back().count())); } } @@ -69,15 +63,9 @@ RailVehicleClass::shadows(const ShadowMapper & mapper) const { if (const auto count = instancesBody.count()) { mapper.dynamicPointInst.use(); - glBindVertexArray(instanceVAO); - glDrawElementsInstanced( - bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); - glBindVertexArray(instancesBogiesVAO.front()); - glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr, - static_cast(instancesBogies.front().count())); - glBindVertexArray(instancesBogiesVAO.back()); - glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr, - static_cast(instancesBogies.back().count())); - glBindVertexArray(0); + bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + bogies.front()->DrawInstanced( + instancesBogiesVAO.front(), static_cast(instancesBogies.front().count())); + bogies.back()->DrawInstanced(instancesBogiesVAO.back(), static_cast(instancesBogies.back().count())); } } -- cgit v1.2.3