summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2026-03-30 13:04:36 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2026-03-31 00:37:24 +0100
commitfb78c88576d9fed90ee69dfa35a9fbd3179ff486 (patch)
tree3666e8c7a7ebf56b5d9e6919093c3a9c7766a750 /game
parentUse uint32_t for indexes in InstanceVertices (diff)
downloadilt-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)
Diffstat (limited to 'game')
-rw-r--r--game/scenary/foliage.cpp21
-rw-r--r--game/scenary/foliage.h12
-rw-r--r--game/scenary/illuminator.cpp11
-rw-r--r--game/scenary/illuminator.h7
-rw-r--r--game/scenary/light.cpp3
-rw-r--r--game/scenary/light.h2
-rw-r--r--game/scenary/plant.cpp2
-rw-r--r--game/scenary/plant.h2
-rw-r--r--game/vehicles/railVehicle.cpp49
-rw-r--r--game/vehicles/railVehicle.h8
-rw-r--r--game/vehicles/railVehicleClass.cpp25
-rw-r--r--game/vehicles/railVehicleClass.h11
-rw-r--r--game/vehicles/train.cpp6
-rw-r--r--game/vehicles/train.h7
-rw-r--r--game/vehicles/vehicle.h2
15 files changed, 77 insertions, 91 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;