diff options
| author | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2026-03-30 13:04:36 +0100 |
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2026-03-31 00:37:24 +0100 |
| commit | fb78c88576d9fed90ee69dfa35a9fbd3179ff486 (patch) | |
| tree | 3666e8c7a7ebf56b5d9e6919093c3a9c7766a750 | |
| parent | Use uint32_t for indexes in InstanceVertices (diff) | |
| download | ilt-fb78c88576d9fed90ee69dfa35a9fbd3179ff486.tar.bz2 ilt-fb78c88576d9fed90ee69dfa35a9fbd3179ff486.tar.xz ilt-fb78c88576d9fed90ee69dfa35a9fbd3179ff486.zip | |
Use a single buffer for the location/rotation data of all renderable objects
Removes the BufferedLocation and BufferedLocationUpdater mess.
Note: appears to break bogie rendering in asset factory test only, same
symptom as broken network render test? (out of date buffer data)
33 files changed, 219 insertions, 238 deletions
diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index ff00af4..5902e09 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -25,8 +25,8 @@ std::weak_ptr<glVertexArray> Foliage::commonInstanceVAO, Foliage::commonInstance std::any Foliage::createAt(const Location & position) const { - return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>( - instances.acquire(position.getRotationTransform(), position.rot.y, position.pos)); + return std::make_shared<InstanceVertices<InstanceVertex>::InstanceProxy>( + instances.acquire(locationData->acquire(position))); } bool @@ -42,12 +42,11 @@ Foliage::postLoad() glDebugScope _ {0}; if (!(instanceVAO = commonInstanceVAO.lock())) { commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>(); - bodyMesh->configureVAO(*instanceVAO, 0) - .addAttribs<LocationVertex, &LocationVertex::rotation, &LocationVertex::position>(1); + bodyMesh->configureVAO(*instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::location>(1); } if (!(instancePointVAO = commonInstancePointVAO.lock())) { commonInstancePointVAO = instancePointVAO = std::make_shared<glVertexArray>(); - instancePointVAO->configure().addAttribs<LocationVertex, &LocationVertex::position, &LocationVertex::yaw>(0); + instancePointVAO->configure().addAttribs<InstanceVertex, &InstanceVertex::location>(0); } const auto & size = bodyMesh->getDimensions().size; billboardSize = billboardTextureSizeForObject(size); @@ -82,14 +81,14 @@ Foliage::preFrame(const Frustum & frustum, const Frustum & lighting) if (instances.size() > 0) { const auto & dims = bodyMesh->getDimensions(); instancePartitions = instances.partition( - [&frustum, &dims](const auto & location) { - return frustum.contains(location.position + dims.centre, dims.size); + [&frustum, &dims](const auto & instance) { + return frustum.contains(instance.location->position.xyz() + dims.centre, dims.size); }, - [&frustum, this](const auto & location) { - return distance(frustum.getPosition(), location.position) < useMeshClipDist; + [&frustum, this](const auto & instance) { + return distance(frustum.getPosition(), instance.location->position.xyz()) < useMeshClipDist; }, - [&lighting, &dims](const auto & location) { - return lighting.contains(location.position + dims.centre, dims.size); + [&lighting, &dims](const auto & instance) { + return lighting.contains(instance.location->position.xyz() + dims.centre, dims.size); }); // In view frustum / Outside view frustum / // Close to view / Far from view / Casts shadow into view / No shadow in view / diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index c599649..f424ffc 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -18,13 +18,13 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs<Foliage> { public: [[nodiscard]] std::any createAt(const Location &) const override; - struct LocationVertex { - glm::mat3 rotation; - float yaw; - GlobalPosition3D position; + struct InstanceVertex { + CommonLocationInstance location; + // float scale; + // something colorBias; }; - mutable InstanceVertices<LocationVertex> instances; + mutable InstanceVertices<InstanceVertex> instances; void preFrame(const Frustum &, const Frustum &) override; void render(const SceneShader &, const Frustum &) const override; void shadows(const ShadowMapper &, const Frustum &) const override; @@ -43,5 +43,5 @@ protected: glTextures<GL_TEXTURE_2D_ARRAY, 3> billboard; private: - InstanceVertices<Foliage::LocationVertex>::PartitionResult instancePartitions; + InstanceVertices<InstanceVertex>::PartitionResult instancePartitions; }; diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 4398853..e991baa 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -11,8 +11,8 @@ std::weak_ptr<glVertexArray> Illuminator::commonInstanceVAO, Illuminator::common std::any Illuminator::createAt(const Location & position) const { - return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>( - instances.acquire(position.getRotationTransform(), position.pos)); + return std::make_shared<InstanceVertices<InstanceVertex>::InstanceProxy>( + instances.acquire(locationData->acquire(position))); } bool @@ -46,8 +46,7 @@ Illuminator::postLoad() glDebugScope _ {0}; if (!(instanceVAO = commonInstanceVAO.lock())) { commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>(); - bodyMesh->configureVAO(*instanceVAO, 0) - .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1); + bodyMesh->configureVAO(*instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::location>(1); } if (!spotLight.empty()) { if (!(instancesSpotLightVAO = commonInstancesSpotLightVAO.lock())) { @@ -55,7 +54,7 @@ Illuminator::postLoad() instancesSpotLightVAO->configure() .addAttribs<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction, &SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc>(0) - .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1); + .addAttribs<InstanceVertex, &InstanceVertex::location>(1); } std::transform( spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { @@ -68,7 +67,7 @@ Illuminator::postLoad() instancesPointLightVAO->configure() .addAttribs<PointLightVertex, &PointLightVertex::position, &PointLightVertex::colour, &PointLightVertex::kq>(0) - .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1); + .addAttribs<InstanceVertex, &InstanceVertex::location>(1); } std::transform( pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 7b6e7ad..53a7981 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -42,8 +42,11 @@ public: bool persist(Persistence::PersistenceStore & store) override; }; - using LocationVertex = std::pair<glm::mat3, GlobalPosition3D>; - mutable InstanceVertices<LocationVertex> instances; + struct InstanceVertex { + CommonLocationInstance location; + }; + + mutable InstanceVertices<InstanceVertex> instances; mutable InstanceVertices<SpotLightVertex> instancesSpotLight; mutable InstanceVertices<PointLightVertex> instancesPointLight; void render(const SceneShader &, const Frustum &) const override; diff --git a/game/scenary/light.cpp b/game/scenary/light.cpp index 6207497..7c38ca2 100644 --- a/game/scenary/light.cpp +++ b/game/scenary/light.cpp @@ -2,6 +2,7 @@ #include "location.h" Light::Light(std::shared_ptr<const Illuminator> type, const Location & position) : - type {std::move(type)}, location {this->type->instances.acquire(position.getRotationTransform(), position.pos)} + type {std::move(type)}, + instance {this->type->instances.acquire(Renderable::commonLocationData.lock()->acquire(position))} { } diff --git a/game/scenary/light.h b/game/scenary/light.h index 0b19535..afb7e03 100644 --- a/game/scenary/light.h +++ b/game/scenary/light.h @@ -7,7 +7,7 @@ class Location; class Light : public WorldObject { std::shared_ptr<const Illuminator> type; - InstanceVertices<Illuminator::LocationVertex>::InstanceProxy location; + InstanceVertices<Illuminator::InstanceVertex>::InstanceProxy instance; void tick(TickDuration) override diff --git a/game/scenary/plant.cpp b/game/scenary/plant.cpp index 2006225..b0e7d16 100644 --- a/game/scenary/plant.cpp +++ b/game/scenary/plant.cpp @@ -3,6 +3,6 @@ Plant::Plant(std::shared_ptr<const Foliage> type, const Location & position) : type {std::move(type)}, - location {this->type->instances.acquire(position.getRotationTransform(), position.rot.y, position.pos)} + instance {this->type->instances.acquire(Renderable::commonLocationData.lock()->acquire(position))} { } diff --git a/game/scenary/plant.h b/game/scenary/plant.h index 77c9ff7..cc690c5 100644 --- a/game/scenary/plant.h +++ b/game/scenary/plant.h @@ -7,7 +7,7 @@ class Location; class Plant : public WorldObject { std::shared_ptr<const Foliage> type; - InstanceVertices<Foliage::LocationVertex>::InstanceProxy location; + InstanceVertices<Foliage::InstanceVertex>::InstanceProxy instance; void tick(TickDuration) override diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 61f6c93..5f92066 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -11,24 +11,14 @@ #include <maths.h> #include <ray.h> -RailVehicle::RailVehicle(RailVehicleClassPtr rvc) : - RailVehicleClass::Instance {rvc->instances.acquire()}, rvClass {std::move(rvc)}, - location {[this](const BufferedLocation * l) { - this->get()->body.rotation = l->getRotationTransform(); - this->get()->body.position = l->position(); - }}, - bogies {{ - {[this](const BufferedLocation * l) { - this->get()->front.rotation = l->getRotationTransform(); - this->get()->front.position = l->position(); - }, - GlobalPosition3D {0, rvClass->wheelBase / 2.F, 0}}, - {[this](const BufferedLocation * l) { - this->get()->back.rotation = l->getRotationTransform(); - this->get()->back.position = l->position(); - }, - GlobalPosition3D {0, -rvClass->wheelBase / 2.F, 0}}, - }} +RailVehicle::RailVehicle(RailVehicleClassPtr rvc, GlobalPosition3D position) : + RailVehicleClass::Instance {rvc->instances.acquire( + RailVehicleClass::commonLocationData.lock()->acquire(Location {.pos = position, .rot = {}}), + RailVehicleClass::commonLocationData.lock()->acquire( + Location {.pos = position + RelativePosition3D {0, rvc->wheelBase / 2.F, 0}, .rot = {}}), + RailVehicleClass::commonLocationData.lock()->acquire( + Location {.pos = position + RelativePosition3D {0, -rvc->wheelBase / 2.F, 0}, .rot = {}}))}, + rvClass {std::move(rvc)} { } @@ -36,23 +26,30 @@ void 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(RelativePosition3D(b2Pos.position() - b1Pos.position())); - location.setLocation((b1Pos.position() + b2Pos.position()) / 2, {vector_pitch(diff), vector_yaw(diff), 0}); + const auto & b1Pos = *(get()->front = t->getBogiePosition(t->linkDist, trailBy += overhang)); + const auto & b2Pos = *(get()->back = t->getBogiePosition(t->linkDist, trailBy += rvClass->wheelBase)); + const auto diff = glm::normalize(difference(b2Pos.position, b1Pos.position)); + get()->body = Location { + .pos = midpoint(b1Pos.position, b2Pos.position), .rot = {vector_pitch(diff), vector_yaw(diff), 0}}; trailBy += 600.F + overhang; } +Location +RailVehicle::getLocation() const +{ + return {.pos = get()->body->position, .rot = get()->body->rotation}; +} + bool RailVehicle::intersectRay(const Ray<GlobalPosition3D> & ray, BaryPosition & baryPos, RelativeDistance & distance) const { constexpr const auto X = 1350.F; const auto Y = this->rvClass->length / 2.F; constexpr const auto Z = 3900.F; - const glm::mat3 moveBy = location.getRotationTransform(); - const auto cornerVertices = cuboidCorners(-X, X, -Y, Y, 0.F, Z) * [&moveBy, this](const auto & corner) { - return location.position() + (moveBy * corner); - }; + const auto cornerVertices + = cuboidCorners(-X, X, -Y, Y, 0.F, Z) * [body = this->get()->body.get()](const auto & corner) { + return body->position + (body->rotationMatrix * corner); + }; static constexpr const std::array<glm::vec<3, uint8_t>, 10> triangles {{ // Front {0, 1, 2}, diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index bf1e782..0f341f9 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -1,8 +1,6 @@ #pragma once -#include "gfx/gl/bufferedLocation.h" #include "railVehicleClass.h" -#include <array> #include <game/selectable.h> #include <glm/glm.hpp> #include <memory> @@ -12,16 +10,14 @@ class Train; class RailVehicle : Selectable, RailVehicleClass::Instance { public: - explicit RailVehicle(RailVehicleClassPtr rvc); + explicit RailVehicle(RailVehicleClassPtr rvc, GlobalPosition3D = {}); void move(const Train *, float & trailBy); + [[nodiscard]] Location getLocation() const; [[nodiscard]] bool intersectRay(const Ray<GlobalPosition3D> &, BaryPosition &, RelativeDistance &) const override; RailVehicleClassPtr rvClass; - using LV = RailVehicleClass::LocationVertex; - BufferedLocationUpdater location; - std::array<BufferedLocationUpdater, 2> bogies; }; using RailVehiclePtr = std::unique_ptr<RailVehicle>; diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 176fe82..4e9404c 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -7,7 +7,6 @@ #include <location.h> #include <maths.h> #include <memory> -#include <ranges> bool RailVehicleClass::persist(Persistence::PersistenceStore & store) @@ -20,12 +19,10 @@ RailVehicleClass::persist(Persistence::PersistenceStore & store) std::any RailVehicleClass::createAt(const Location & position) const { - return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>(instances.acquire(LocationVertex { - .body = {.rotation = position.getRotationTransform(), .position = position.pos}, - .front = {.rotation = position.getRotationTransform(), - .position = {sincos(position.rot.x) * wheelBase * 0.5F, position.pos.z}}, - .back = {.rotation = position.getRotationTransform(), - .position = {sincos(position.rot.x) * wheelBase * -0.5F, position.pos.z}}, + return std::make_shared<InstanceVertices<InstanceVertex>::InstanceProxy>(instances.acquire(InstanceVertex { + .body = locationData->acquire(position), + .front = locationData->acquire(position + ((sincos(position.rot.x) * wheelBase * 0.5F) || 0.F)), + .back = locationData->acquire(position + ((sincos(position.rot.x) * wheelBase * -0.5F) || 0.F)), })); } @@ -34,22 +31,20 @@ RailVehicleClass::postLoad() { texture = getTexture(); glDebugScope _ {0}; - bodyMesh->configureVAO(instanceVAO, 0) - .addAttribs<LocationVertex, &LocationVertex::Part::rotation, &LocationVertex::Part::position>(1); - static_assert(sizeof(LocationVertex) == 144UL); + bodyMesh->configureVAO(instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::body>(1); } void RailVehicleClass::renderAllParts(const size_t count) const { - using PartPair = std::pair<Mesh::Ptr, LocationVertex::Part LocationVertex::*>; + using PartPair = std::pair<Mesh::Ptr, CommonLocationInstance InstanceVertex::*>; const auto bufferName = instances.bufferName(); for (const auto & [mesh, part] : { - PartPair {bodyMesh, &LocationVertex::body}, - PartPair {bogies.front(), &LocationVertex::front}, - PartPair {bogies.back(), &LocationVertex::back}, + PartPair {bodyMesh, &InstanceVertex::body}, + PartPair {bogies.front(), &InstanceVertex::front}, + PartPair {bogies.back(), &InstanceVertex::back}, }) { - instanceVAO.useBuffer<LocationVertex>(1, bufferName, part); + instanceVAO.useBuffer<InstanceVertex>(1, bufferName, part); mesh->drawInstanced(instanceVAO, static_cast<GLsizei>(count)); } } diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index fe27230..a635122 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -19,13 +19,8 @@ public: [[nodiscard]] std::any createAt(const Location &) const override; - struct LocationVertex { - struct Part { - glm::mat3 rotation; - GlobalPosition3D position; - }; - - Part body, front, back; + struct InstanceVertex { + CommonLocationInstance body, front, back; }; std::array<Mesh::Ptr, 2> bogies; @@ -35,7 +30,7 @@ public: float length; float maxSpeed; - mutable InstanceVertices<LocationVertex> instances; + mutable InstanceVertices<InstanceVertex> instances; using Instance = decltype(instances)::InstanceProxy; protected: diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 2461d9c..c79fd17 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -21,6 +21,12 @@ Train::getBogiePosition(float linkDist, float dist) const return b2Link.first->positionAt(b2linkDist, b2Link.second); } +Location +Train::getLocation() const +{ + return objects.front()->getLocation(); +} + bool Train::intersectRay(const Ray<GlobalPosition3D> & ray, BaryPosition & baryPos, RelativeDistance & distance) const { diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 88e30f9..9ca53a8 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -19,12 +19,7 @@ class Train : public Vehicle, public UniqueCollection<RailVehicle>, public Can<G public: explicit Train(const Link::CPtr & link, float linkDist = 0) : Vehicle {link, linkDist} { } - [[nodiscard]] const Location & - getLocation() const override - { - return objects.front()->location; - } - + [[nodiscard]] Location getLocation() const override; [[nodiscard]] bool intersectRay(const Ray<GlobalPosition3D> &, BaryPosition &, RelativeDistance &) const override; void tick(TickDuration elapsed) override; diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index c3b35b7..cca8ff0 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -18,7 +18,7 @@ public: float linkDist; // distance along current link float speed {}; // speed in m/s (~75 km/h) - [[nodiscard]] virtual const Location & getLocation() const = 0; + [[nodiscard]] virtual Location getLocation() const = 0; Orders orders; ActivityPtr currentActivity; diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp index d7bbc0b..c3a5d08 100644 --- a/gfx/followCameraController.cpp +++ b/gfx/followCameraController.cpp @@ -4,7 +4,6 @@ #include <glm/glm.hpp> #include <location.h> #include <maths.h> -#include <memory> #include <tuple> #include <utility> @@ -15,7 +14,7 @@ FollowCameraController::updateCamera(Camera * camera) const { const auto [pos, rot] = [this]() { const auto t {target.lock()}; - return std::tie(t->getLocation().pos, t->getLocation().rot); + return std::make_pair(t->getLocation().pos, t->getLocation().rot); }(); switch (mode) { diff --git a/gfx/gl/bufferedLocation.cpp b/gfx/gl/bufferedLocation.cpp deleted file mode 100644 index f1bedfe..0000000 --- a/gfx/gl/bufferedLocation.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "bufferedLocation.h" -#include "location.h" -#include <glm/gtx/transform.hpp> - -BufferedLocation::BufferedLocation(GlobalPosition3D p, Rotation3D r) : BufferedLocation {Location {p, r}} { } - -BufferedLocation::BufferedLocation(const Location & l) : loc {l} { } - -BufferedLocation::operator const Location &() const -{ - return loc; -} - -BufferedLocation & -BufferedLocation::operator=(const Location & l) -{ - loc = l; - updateBuffer(); - return *this; -} - -GlobalPosition3D -BufferedLocation::position() const -{ - return loc.pos; -} - -Rotation3D -BufferedLocation::rotation() const -{ - return loc.rot; -} - -void -BufferedLocation::setPosition(GlobalPosition3D p, bool update) -{ - loc.pos = p; - if (update) { - updateBuffer(); - } -} - -void -BufferedLocation::setRotation(Rotation3D r, bool update) -{ - loc.rot = r; - if (update) { - updateBuffer(); - } -} - -void -BufferedLocation::setLocation(GlobalPosition3D p, Rotation3D r) -{ - loc.pos = p; - loc.rot = r; - updateBuffer(); -} - -glm::mat4 -BufferedLocation::getRotationTransform() const -{ - return loc.getRotationTransform(); -} - -void -BufferedLocationUpdater::updateBuffer() const -{ - onUpdate(this); -} diff --git a/gfx/gl/bufferedLocation.h b/gfx/gl/bufferedLocation.h deleted file mode 100644 index 87b957f..0000000 --- a/gfx/gl/bufferedLocation.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include "location.h" -#include <functional> -#include <glm/mat4x4.hpp> -#include <glm/vec3.hpp> -#include <utility> - -class BufferedLocation { -public: - BufferedLocation(GlobalPosition3D = {}, Rotation3D = {}); - BufferedLocation(const Location &); - virtual ~BufferedLocation() = default; - - BufferedLocation & operator=(const Location &); - - operator const Location &() const; - - [[nodiscard]] GlobalPosition3D position() const; - [[nodiscard]] Rotation3D rotation() const; - void setPosition(GlobalPosition3D, bool update = true); - void setRotation(Rotation3D, bool update = true); - void setLocation(GlobalPosition3D, Rotation3D); - - [[nodiscard]] glm::mat4 getRotationTransform() const; - -private: - virtual void updateBuffer() const = 0; - - Location loc; -}; - -class BufferedLocationUpdater : public BufferedLocation { -public: - template<typename... LocationArgs> - BufferedLocationUpdater(std::function<void(const BufferedLocation *)> onUpdate, LocationArgs &&... t) : - BufferedLocation {std::forward<LocationArgs>(t)...}, onUpdate {std::move(onUpdate)} - { - updateBuffer(); - } - - using BufferedLocation::operator=; - -private: - void updateBuffer() const override; - - std::function<void(const BufferedLocation *)> onUpdate; -}; diff --git a/gfx/gl/instanceVertices.h b/gfx/gl/instanceVertices.h index e31fa83..9963a96 100644 --- a/gfx/gl/instanceVertices.h +++ b/gfx/gl/instanceVertices.h @@ -128,6 +128,12 @@ public: return base::begin().base().bufferName(); } + [[nodiscard]] GLuint + indexBufferName() const + { + return index.begin().base().bufferName(); + } + using typename base::value_type; using base::at; @@ -232,7 +238,7 @@ protected: } // Index into buffer given to nth proxy - std::vector<IndexT> index; + glVector<IndexT> index; std::vector<IndexT> reverseIndex; // List of free spaces in index std::vector<IndexT> unused; diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index ea11bea..a809ed5 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -78,6 +78,10 @@ SceneRenderer::preFrame(const SceneProvider & scene, const LightDirection lightD renderable->preFrame(camera, lightView); renderable->updateBillboard(billboardPainter); }); + if (auto cld = Renderable::commonLocationData.lock()) { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, cld->bufferName()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, cld->indexBufferName()); + } } void diff --git a/gfx/gl/shaders/billboard.vert b/gfx/gl/shaders/billboard.vert index faf8b19..d6d3869 100644 --- a/gfx/gl/shaders/billboard.vert +++ b/gfx/gl/shaders/billboard.vert @@ -1,12 +1,15 @@ #version 460 core +#extension GL_ARB_shading_language_include : enable + +#include "commonLocationData.glsl" uniform mat4 viewProjection; uniform ivec4 viewPort; uniform ivec3 viewPoint; uniform vec3 centre; uniform float size; -layout(location = 0) in ivec3 modelPos; -layout(location = 1) in float yaw; + +layout(location = 0) in uint index; flat out vec3 ModelPos; flat out float Yaw; @@ -15,8 +18,9 @@ flat out float Depth; void main() { + const ivec3 modelPos = locations[cldIndex[index]].position.xyz; ModelPos = modelPos - viewPoint; - Yaw = yaw; + Yaw = locations[cldIndex[index]].rotation.x; gl_Position = viewProjection * vec4(ModelPos + centre, 1); Depth = gl_Position.w; gl_PointSize = (viewPort.w * size * 2) / gl_Position.w; diff --git a/gfx/gl/shaders/commonLocationData.glsl b/gfx/gl/shaders/commonLocationData.glsl new file mode 100644 index 0000000..4939b9b --- /dev/null +++ b/gfx/gl/shaders/commonLocationData.glsl @@ -0,0 +1,20 @@ +#ifndef COMMON_LOCATION_DATA_INCLUDED +#define COMMON_LOCATION_DATA_INCLUDED + +struct CommonLocationData { + ivec4 position; + vec4 rotation; + mat3x4 rotationMatrix; +}; + +layout(binding = 0, std430) restrict readonly buffer commonLocationData +{ + CommonLocationData locations[]; +}; + +layout(binding = 1, std430) restrict readonly buffer commonLocationDataIndex +{ + uint cldIndex[]; +}; + +#endif diff --git a/gfx/gl/shaders/dynamicPointInst.vert b/gfx/gl/shaders/dynamicPointInst.vert index 7a00099..7c50706 100644 --- a/gfx/gl/shaders/dynamicPointInst.vert +++ b/gfx/gl/shaders/dynamicPointInst.vert @@ -3,12 +3,14 @@ layout(binding = 1) uniform usampler2DRect materialData; +#include "commonLocationData.glsl" #include "materialInterface.glsl" #include "meshIn.glsl" uniform mat4 viewProjection; uniform ivec3 viewPoint; -layout(location = 5) in mat3 model; -layout(location = 8) in ivec3 modelPos; +layout(location = 5) in uint index; +mat3 model = mat3(locations[cldIndex[index]].rotationMatrix); +ivec3 modelPos = locations[cldIndex[index]].position.xyz; #include "commonPoint.glsl" diff --git a/gfx/gl/shaders/pointLight.vert b/gfx/gl/shaders/pointLight.vert index b8ddecb..61953ad 100644 --- a/gfx/gl/shaders/pointLight.vert +++ b/gfx/gl/shaders/pointLight.vert @@ -1,10 +1,14 @@ #version 460 core +#extension GL_ARB_shading_language_include : enable + +#include "commonLocationData.glsl" layout(location = 0) in vec3 v_position; layout(location = 1) in vec3 v_colour; layout(location = 2) in float v_kq; -layout(location = 3) in mat3 model; -layout(location = 6) in ivec3 modelPos; +layout(location = 3) in uint index; +mat3 model = mat3(locations[cldIndex[index]].rotationMatrix); +ivec3 modelPos = locations[cldIndex[index]].position.xyz; uniform ivec3 viewPoint; diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vert b/gfx/gl/shaders/shadowDynamicPointInst.vert index d020717..b978e4a 100644 --- a/gfx/gl/shaders/shadowDynamicPointInst.vert +++ b/gfx/gl/shaders/shadowDynamicPointInst.vert @@ -1,10 +1,12 @@ #version 460 core #extension GL_ARB_shading_language_include : enable +#include "commonLocationData.glsl" #include "meshIn.glsl" uniform ivec3 viewPoint; -layout(location = 5) in mat3 model; -layout(location = 8) in ivec3 modelPos; +layout(location = 5) in uint index; +mat3 model = mat3(locations[cldIndex[index]].rotationMatrix); +ivec3 modelPos = locations[cldIndex[index]].position.xyz; #include "commonShadowPoint.glsl" diff --git a/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vert b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vert index 51b05c8..a5e8245 100644 --- a/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vert +++ b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vert @@ -4,12 +4,14 @@ layout(binding = 4) uniform usampler2DRect materialData; +#include "commonLocationData.glsl" #include "getMaterialDetail.glsl" #include "materialInterface.glsl" #include "meshIn.glsl" uniform ivec3 viewPoint; -layout(location = 5) in mat3 model; -layout(location = 8) in ivec3 modelPos; +layout(location = 5) in uint index; +mat3 model = mat3(locations[cldIndex[index]].rotationMatrix); +ivec3 modelPos = locations[cldIndex[index]].position.xyz; #include "commonShadowPoint.glsl" diff --git a/gfx/gl/shaders/shadowDynamicPointStencil.vert b/gfx/gl/shaders/shadowDynamicPointStencil.vert index b750b3e..0a41143 100644 --- a/gfx/gl/shaders/shadowDynamicPointStencil.vert +++ b/gfx/gl/shaders/shadowDynamicPointStencil.vert @@ -1,7 +1,9 @@ #version 460 core +#extension GL_ARB_shading_language_include : enable -layout(location = 0) in ivec3 worldPos; -layout(location = 1) in float modelYaw; +#include "commonLocationData.glsl" + +layout(location = 0) in uint index; uniform ivec3 viewPoint; uniform vec3 centre; @@ -11,6 +13,6 @@ out ivec3 vworldPos; void main() { - vmodelYaw = modelYaw; - vworldPos = worldPos - viewPoint + ivec3(centre); + vmodelYaw = locations[cldIndex[index]].rotation.x; + vworldPos = locations[cldIndex[index]].position.xyz - viewPoint + ivec3(centre); } diff --git a/gfx/gl/shaders/spotLight.vert b/gfx/gl/shaders/spotLight.vert index 83f3859..6f72a63 100644 --- a/gfx/gl/shaders/spotLight.vert +++ b/gfx/gl/shaders/spotLight.vert @@ -1,12 +1,16 @@ #version 460 core +#extension GL_ARB_shading_language_include : enable + +#include "commonLocationData.glsl" layout(location = 0) in vec3 v_position; layout(location = 1) in vec3 v_direction; layout(location = 2) in vec3 v_colour; layout(location = 3) in float v_kq; layout(location = 4) in float v_arc; -layout(location = 5) in mat3 model; -layout(location = 8) in ivec3 modelPos; +layout(location = 5) in uint index; +mat3 model = mat3(locations[cldIndex[index]].rotationMatrix); +ivec3 modelPos = locations[cldIndex[index]].position.xyz; uniform ivec3 viewPoint; diff --git a/gfx/renderable.cpp b/gfx/renderable.cpp index 8caab93..4597597 100644 --- a/gfx/renderable.cpp +++ b/gfx/renderable.cpp @@ -1,4 +1,39 @@ #include "renderable.h" +#include "gl_traits.h" +#include "location.h" +#include "maths.h" + +std::weak_ptr<Renderable::CommonLocationData> Renderable::commonLocationData; + +Renderable::CommonLocation::CommonLocation(Location const & location) : + position {location.pos, 0}, rotation {location.rot, 0}, rotationMatrix {location.getRotationTransform()} +{ +} + +Renderable::CommonLocation & +Renderable::CommonLocation ::operator=(Location const & location) +{ + position = location.pos || 0; + rotation = location.rot || 0.F; + rotationMatrix = location.getRotationTransform(); + return *this; +} + +Renderable::Renderable() +{ + if (!(locationData = commonLocationData.lock())) { + commonLocationData = locationData = std::make_shared<CommonLocationData>(); + } +} + +GLuint +gl_traits<InstanceVertices<Renderable::CommonLocation>::InstanceProxy>::vertexArrayAttribFormat( + GLuint vao, GLuint index, GLuint offset) +{ + return gl_traits< + decltype(InstanceVertices<Renderable::CommonLocation>::InstanceProxy::index)>::vertexArrayAttribFormat(vao, + index, offset + offsetof(InstanceVertices<Renderable::CommonLocation>::InstanceProxy, index)); +}; void Renderable::preFrame(const Frustum &, const Frustum &) diff --git a/gfx/renderable.h b/gfx/renderable.h index d0870ad..b0a42f2 100644 --- a/gfx/renderable.h +++ b/gfx/renderable.h @@ -1,5 +1,8 @@ #pragma once +#include "gfx/gl/instanceVertices.h" +#include "gl_traits.h" +#include <glm/mat3x3.hpp> #include <special_members.h> class SceneShader; @@ -7,10 +10,11 @@ class Frustum; class ShadowMapper; class ShadowStenciller; class BillboardPainter; +class Location; class Renderable { public: - Renderable() = default; + Renderable(); virtual ~Renderable() = default; DEFAULT_MOVE_COPY(Renderable); @@ -21,4 +25,25 @@ public: virtual void updateStencil(const ShadowStenciller & lightDir) const; virtual void updateBillboard(const BillboardPainter &) const; + + struct CommonLocation { + CommonLocation(const Location &); + CommonLocation & operator=(const Location &); + + glm::ivec4 position; + glm::vec4 rotation; + glm::mat3x4 rotationMatrix; + }; + + using CommonLocationData = InstanceVertices<CommonLocation>; + using CommonLocationInstance = CommonLocationData::InstanceProxy; + + std::shared_ptr<CommonLocationData> locationData; + + static std::weak_ptr<CommonLocationData> commonLocationData; +}; + +template<> struct gl_traits<InstanceVertices<Renderable::CommonLocation>::InstanceProxy> { + static GLuint vertexArrayAttribFormat(GLuint vao, GLuint index, GLuint offset); }; + diff --git a/lib/location.cpp b/lib/location.cpp index 13acfde..2138f0a 100644 --- a/lib/location.cpp +++ b/lib/location.cpp @@ -2,6 +2,14 @@ #include "maths.h" #include <glm/gtx/transform.hpp> +Location +Location::operator+(RelativePosition3D offset) const +{ + Location ret {*this}; + ret.pos += offset; + return ret; +} + glm::mat3 Location::getRotationTransform() const { diff --git a/lib/location.h b/lib/location.h index 016aee7..e642b41 100644 --- a/lib/location.h +++ b/lib/location.h @@ -11,6 +11,8 @@ public: [[nodiscard]] glm::mat3 getRotationTransform() const; + Location operator+(RelativePosition3D) const; + GlobalPosition3D pos; Rotation3D rot; }; diff --git a/test/test-render.cpp b/test/test-render.cpp index 35d71f3..b21f440 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -49,14 +49,8 @@ namespace { std::uniform_int_distribution<GlobalDistance> positionOffsetDistribution {-1500, +1500}; std::uniform_int_distribution<int> treeDistribution {1, 3}; std::uniform_int_distribution<int> treeVariantDistribution {1, 4}; - train1 = std::make_shared<RailVehicle>(brush47rvc); - train1->location.setPosition({52000, 50000, 2000}); - train1->bogies.front().setPosition(train1->bogies.front().position() + train1->location.position()); - train1->bogies.back().setPosition(train1->bogies.back().position() + train1->location.position()); - train2 = std::make_shared<RailVehicle>(brush47rvc); - train2->location.setPosition({52000, 30000, 2000}); - train2->bogies.front().setPosition(train2->bogies.front().position() + train2->location.position()); - train2->bogies.back().setPosition(train2->bogies.back().position() + train2->location.position()); + train1 = std::make_shared<RailVehicle>(brush47rvc, GlobalPosition3D {52000, 50000, 2000}); + train2 = std::make_shared<RailVehicle>(brush47rvc, GlobalPosition3D {52000, 30000, 2000}); for (auto posX = 40000; posX < 100000; posX += 5000) { for (auto posY = 65000; posY < 125000; posY += 5000) { gameState->world.create<Plant>( |
