summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2026-04-03 12:04:14 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2026-04-03 12:04:14 +0100
commit4d04572e48950493ce85255d181be43967f362fd (patch)
treef74d055f151472cda3fa3fc0f760eb6219dd9fad
parentUse std::chrono for worldTime (diff)
parentFix direction of difference vector calculating railVehicle new position (diff)
downloadilt-4d04572e48950493ce85255d181be43967f362fd.tar.bz2
ilt-4d04572e48950493ce85255d181be43967f362fd.tar.xz
ilt-4d04572e48950493ce85255d181be43967f362fd.zip
Merge branch 'shared-locations'
-rw-r--r--application/main.cpp4
-rw-r--r--assetFactory/lights.cpp14
-rw-r--r--assetFactory/lights.h18
-rw-r--r--game/scenary/foliage.cpp28
-rw-r--r--game/scenary/foliage.h12
-rw-r--r--game/scenary/illuminator.cpp79
-rw-r--r--game/scenary/illuminator.h39
-rw-r--r--game/scenary/light.cpp11
-rw-r--r--game/scenary/light.h5
-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
-rw-r--r--gfx/followCameraController.cpp3
-rw-r--r--gfx/gl/bufferedLocation.cpp70
-rw-r--r--gfx/gl/bufferedLocation.h48
-rw-r--r--gfx/gl/glVertexArray.h19
-rw-r--r--gfx/gl/instanceVertices.h35
-rw-r--r--gfx/gl/sceneRenderer.cpp4
-rw-r--r--gfx/gl/shaders/billboard.vert10
-rw-r--r--gfx/gl/shaders/commonLocationData.glsl20
-rw-r--r--gfx/gl/shaders/dynamicPointInst.vert6
-rw-r--r--gfx/gl/shaders/pointLight.vert8
-rw-r--r--gfx/gl/shaders/shadowDynamicPointInst.vert6
-rw-r--r--gfx/gl/shaders/shadowDynamicPointInstWithTextures.vert6
-rw-r--r--gfx/gl/shaders/shadowDynamicPointStencil.vert10
-rw-r--r--gfx/gl/shaders/spotLight.vert8
-rw-r--r--gfx/models/lights.cpp11
-rw-r--r--gfx/models/lights.h29
-rw-r--r--gfx/renderable.cpp76
-rw-r--r--gfx/renderable.h38
-rw-r--r--lib/location.cpp8
-rw-r--r--lib/location.h2
-rw-r--r--lib/util.cpp1
-rw-r--r--lib/util.h22
-rw-r--r--test/Jamfile.jam1
-rw-r--r--test/test-assetFactory.cpp2
-rw-r--r--test/test-render.cpp18
-rw-r--r--test/test-static-util.cpp29
-rw-r--r--ui/gameMainWindow.cpp4
45 files changed, 447 insertions, 369 deletions
diff --git a/application/main.cpp b/application/main.cpp
index f5acb3b..d64d799 100644
--- a/application/main.cpp
+++ b/application/main.cpp
@@ -1,3 +1,4 @@
+#include "game/scenary/light.h"
#include "ui/mainApplication.h"
#include "ui/mainWindow.h"
#include <array>
@@ -108,6 +109,9 @@ public:
{0, rotationDistribution(randomdev), 0}});
}
}
+
+ world.create<Light>(assets.at("old-lamp").dynamicCast<Illuminator>(),
+ Location {.pos = terrain->positionAt({{311000000, 491100000}})});
}
mainLoop();
diff --git a/assetFactory/lights.cpp b/assetFactory/lights.cpp
new file mode 100644
index 0000000..ec8e17e
--- /dev/null
+++ b/assetFactory/lights.cpp
@@ -0,0 +1,14 @@
+#include "lights.h"
+
+bool
+SpotLight::persist(Persistence::PersistenceStore & store)
+{
+ return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(direction) && STORE_MEMBER(colour) && STORE_MEMBER(kq)
+ && STORE_MEMBER(arc);
+}
+
+bool
+PointLight::persist(Persistence::PersistenceStore & store)
+{
+ return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(colour) && STORE_MEMBER(kq);
+}
diff --git a/assetFactory/lights.h b/assetFactory/lights.h
new file mode 100644
index 0000000..8657d85
--- /dev/null
+++ b/assetFactory/lights.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "gfx/models/lights.h"
+#include "persistence.h"
+#include "stdTypeDefs.h"
+
+struct SpotLight : Persistence::Persistable, SpotLightDef, StdTypeDefs<SpotLight> {
+private:
+ friend Persistence::SelectionPtrBase<std::shared_ptr<SpotLight>>;
+ bool persist(Persistence::PersistenceStore & store) override;
+};
+
+struct PointLight : Persistence::Persistable, PointLightDef, StdTypeDefs<PointLight> {
+private:
+ friend Persistence::SelectionPtrBase<std::shared_ptr<PointLight>>;
+ bool persist(Persistence::PersistenceStore & store) override;
+};
+
diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp
index ff00af4..f27ac26 100644
--- a/game/scenary/foliage.cpp
+++ b/game/scenary/foliage.cpp
@@ -4,6 +4,7 @@
#include "gfx/gl/sceneShader.h"
#include "gfx/gl/shadowMapper.h"
#include "gfx/gl/shadowStenciller.h"
+#include "util.h"
#include <location.h>
static_assert(std::is_constructible_v<Foliage>);
@@ -25,8 +26,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
@@ -40,14 +41,11 @@ Foliage::postLoad()
{
texture = getTexture();
glDebugScope _ {0};
- if (!(instanceVAO = commonInstanceVAO.lock())) {
- commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>();
- bodyMesh->configureVAO(*instanceVAO, 0)
- .addAttribs<LocationVertex, &LocationVertex::rotation, &LocationVertex::position>(1);
+ if (createIfRequired(instanceVAO, commonInstanceVAO)) {
+ 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);
+ if (createIfRequired(instancePointVAO, commonInstancePointVAO)) {
+ instancePointVAO->configure().addAttribs<InstanceVertex, &InstanceVertex::location>(0);
}
const auto & size = bodyMesh->getDimensions().size;
billboardSize = billboardTextureSizeForObject(size);
@@ -82,14 +80,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..7f0c7c2 100644
--- a/game/scenary/illuminator.cpp
+++ b/game/scenary/illuminator.cpp
@@ -1,31 +1,18 @@
#include "illuminator.h"
#include "gfx/gl/sceneShader.h"
#include "gfx/models/texture.h" // IWYU pragma: keep
+#include "util.h"
#include <location.h>
static_assert(std::is_constructible_v<Illuminator>);
-std::weak_ptr<glVertexArray> Illuminator::commonInstanceVAO, Illuminator::commonInstancesSpotLightVAO,
- Illuminator::commonInstancesPointLightVAO;
+std::weak_ptr<glVertexArray> Illuminator::commonInstanceVAO;
std::any
Illuminator::createAt(const Location & position) const
{
- return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>(
- instances.acquire(position.getRotationTransform(), position.pos));
-}
-
-bool
-Illuminator::SpotLight::persist(Persistence::PersistenceStore & store)
-{
- return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(direction) && STORE_MEMBER(colour) && STORE_MEMBER(kq)
- && STORE_MEMBER(arc);
-}
-
-bool
-Illuminator::PointLight::persist(Persistence::PersistenceStore & store)
-{
- return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(colour) && STORE_MEMBER(kq);
+ return std::make_shared<InstanceVertices<InstanceVertex>::InstanceProxy>(
+ instances.acquire(locationData->acquire(position)));
}
bool
@@ -44,36 +31,8 @@ Illuminator::postLoad()
}
texture = getTexture();
glDebugScope _ {0};
- if (!(instanceVAO = commonInstanceVAO.lock())) {
- commonInstanceVAO = instanceVAO = std::make_shared<glVertexArray>();
- bodyMesh->configureVAO(*instanceVAO, 0)
- .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1);
- }
- if (!spotLight.empty()) {
- if (!(instancesSpotLightVAO = commonInstancesSpotLightVAO.lock())) {
- commonInstancesSpotLightVAO = instancesSpotLightVAO = std::make_shared<glVertexArray>();
- instancesSpotLightVAO->configure()
- .addAttribs<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction,
- &SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc>(0)
- .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1);
- }
- std::transform(
- spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) {
- return instancesSpotLight.acquire(*s);
- });
- }
- if (!pointLight.empty()) {
- if (!(instancesPointLightVAO = commonInstancesPointLightVAO.lock())) {
- commonInstancesPointLightVAO = instancesPointLightVAO = std::make_shared<glVertexArray>();
- instancesPointLightVAO->configure()
- .addAttribs<PointLightVertex, &PointLightVertex::position, &PointLightVertex::colour,
- &PointLightVertex::kq>(0)
- .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(1);
- }
- std::transform(
- pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) {
- return instancesPointLight.acquire(*s);
- });
+ if (createIfRequired(instanceVAO, commonInstanceVAO)) {
+ bodyMesh->configureVAO(*instanceVAO, 0).addAttribs<InstanceVertex, &InstanceVertex::location>(1);
}
}
@@ -90,29 +49,3 @@ Illuminator::render(const SceneShader & shader, const Frustum &) const
bodyMesh->drawInstanced(*instanceVAO, static_cast<GLsizei>(count));
}
}
-
-void
-Illuminator::lights(const SceneShader & shader) const
-{
- if (const auto count = instances.size()) {
- glDebugScope _ {*instanceVAO};
- if (const auto scount = instancesSpotLight.size()) {
- glDebugScope _ {*instancesSpotLightVAO, "Spot lights"};
- shader.spotLightInst.use();
- glBindVertexArray(*instancesSpotLightVAO);
- instancesSpotLightVAO->useBuffer(0, instancesSpotLight);
- instancesSpotLightVAO->useBuffer(1, instances);
- glDrawArraysInstanced(GL_POINTS, 0, static_cast<GLsizei>(scount), static_cast<GLsizei>(count));
- }
- if (const auto pcount = instancesPointLight.size()) {
- glDebugScope _ {*instancesPointLightVAO, "Point llights"};
- shader.pointLightInst.use();
- glBindVertexArray(*instancesPointLightVAO);
- instancesPointLightVAO->useBuffer(0, instancesPointLight);
- instancesPointLightVAO->useBuffer(1, instances);
- glDrawArraysInstanced(GL_POINTS, 0, static_cast<GLsizei>(pcount), static_cast<GLsizei>(count));
- }
-
- glBindVertexArray(0);
- }
-}
diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h
index 7b6e7ad..216b536 100644
--- a/game/scenary/illuminator.h
+++ b/game/scenary/illuminator.h
@@ -1,6 +1,7 @@
#pragma once
#include "assetFactory/asset.h"
+#include "assetFactory/lights.h"
#include "gfx/gl/instanceVertices.h"
#include "gfx/models/texture.h"
#include "gfx/renderable.h"
@@ -11,51 +12,25 @@ class Location;
class Illuminator : public Asset, public Renderable, public StdTypeDefs<Illuminator> {
Mesh::Ptr bodyMesh;
Texture::Ptr texture;
- std::shared_ptr<glVertexArray> instanceVAO, instancesSpotLightVAO, instancesPointLightVAO;
- static std::weak_ptr<glVertexArray> commonInstanceVAO, commonInstancesSpotLightVAO, commonInstancesPointLightVAO;
+ std::shared_ptr<glVertexArray> instanceVAO;
+ static std::weak_ptr<glVertexArray> commonInstanceVAO;
public:
[[nodiscard]] std::any createAt(const Location &) const override;
- struct LightCommonVertex {
- RelativePosition3D position;
- RGB colour;
- RelativeDistance kq;
+ struct InstanceVertex {
+ CommonLocationInstance location;
};
- struct SpotLightVertex : LightCommonVertex {
- Direction3D direction;
- Angle arc;
- };
-
- struct PointLightVertex : LightCommonVertex { };
-
- struct SpotLight : Persistence::Persistable, SpotLightVertex, StdTypeDefs<SpotLight> {
- private:
- friend Persistence::SelectionPtrBase<std::shared_ptr<SpotLight>>;
- bool persist(Persistence::PersistenceStore & store) override;
- };
-
- struct PointLight : Persistence::Persistable, PointLightVertex, StdTypeDefs<PointLight> {
- private:
- friend Persistence::SelectionPtrBase<std::shared_ptr<PointLight>>;
- bool persist(Persistence::PersistenceStore & store) override;
- };
-
- using LocationVertex = std::pair<glm::mat3, GlobalPosition3D>;
- mutable InstanceVertices<LocationVertex> instances;
- mutable InstanceVertices<SpotLightVertex> instancesSpotLight;
- mutable InstanceVertices<PointLightVertex> instancesPointLight;
+ mutable InstanceVertices<InstanceVertex> instances;
void render(const SceneShader &, const Frustum &) const override;
- void lights(const SceneShader &) const override;
protected:
friend Persistence::SelectionPtrBase<std::shared_ptr<Illuminator>>;
bool persist(Persistence::PersistenceStore & store) override;
void postLoad() override;
+public:
std::vector<SpotLight::Ptr> spotLight;
std::vector<PointLight::Ptr> pointLight;
- std::vector<InstanceVertices<SpotLightVertex>::InstanceProxy> spotLightInstances;
- std::vector<InstanceVertices<PointLightVertex>::InstanceProxy> pointLightInstances;
};
diff --git a/game/scenary/light.cpp b/game/scenary/light.cpp
index 6207497..c51efda 100644
--- a/game/scenary/light.cpp
+++ b/game/scenary/light.cpp
@@ -2,6 +2,15 @@
#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))}
{
+ std::ranges::transform(this->type->spotLight, std::back_inserter(spotLightInstances),
+ [spotLights = Renderable::commonSpotLights.lock(), this](const auto & spotLight) {
+ return spotLights->acquire(*spotLight, instance->location.index);
+ });
+ std::ranges::transform(this->type->pointLight, std::back_inserter(pointLightInstances),
+ [pointLights = Renderable::commonPointLights.lock(), this](const auto & pointLight) {
+ return pointLights->acquire(*pointLight, instance->location.index);
+ });
}
diff --git a/game/scenary/light.h b/game/scenary/light.h
index 0b19535..b1ea469 100644
--- a/game/scenary/light.h
+++ b/game/scenary/light.h
@@ -7,13 +7,16 @@ 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
{
}
+ std::vector<InstanceVertices<SpotLightVertex>::InstanceProxy> spotLightInstances;
+ std::vector<InstanceVertices<PointLightVertex>::InstanceProxy> pointLightInstances;
+
public:
Light(std::shared_ptr<const Illuminator> type, const Location & position);
};
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..c11d817 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(b1Pos.position, b2Pos.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/glVertexArray.h b/gfx/gl/glVertexArray.h
index 323303c..4e8113f 100644
--- a/gfx/gl/glVertexArray.h
+++ b/gfx/gl/glVertexArray.h
@@ -4,6 +4,7 @@
#include "glArrays.h"
#include "glBuffer.h"
#include "gl_traits.h"
+#include "util.h"
namespace Impl {
class VertexArrayConfigurator {
@@ -20,11 +21,8 @@ namespace Impl {
}
M T::* ptr;
- using ValueType = M;
};
- template<typename M, typename T> MP(M T::*) -> MP<M, T>;
-
explicit VertexArrayConfigurator(GLuint name) : name {name} { }
VertexArrayConfigurator &
@@ -52,7 +50,7 @@ namespace Impl {
return addAttribsFor<VertexT>(divisor);
}
- template<typename VertexT, MP... Attribs>
+ template<typename VertexT, auto... Attribs>
VertexArrayConfigurator &
addAttribs(const GLuint divisor)
{
@@ -60,7 +58,7 @@ namespace Impl {
return *this;
}
- template<typename VertexT, MP... Attribs>
+ template<typename VertexT, auto... Attribs>
VertexArrayConfigurator &
addAttribs(const GLuint divisor, const glBuffer & buffer)
{
@@ -68,7 +66,7 @@ namespace Impl {
return addAttribs<VertexT, Attribs...>(divisor);
}
- template<typename VertexT, MP... Attribs>
+ template<typename VertexT, auto... Attribs>
VertexArrayConfigurator &
addAttribs(const GLuint divisor, glBuffer & buffer, const SequentialCollection<VertexT> auto & data)
{
@@ -93,14 +91,15 @@ namespace Impl {
setPointerMeta(attrib + gl_traits<T>::vertexArrayAttribFormat(name, attrib, offset));
}
- template<MP Attrib>
+ template<typename VertexT, auto Attrib>
void
setPointer()
{
- setPointer<typename decltype(Attrib)::ValueType>(Attrib);
+ using Mbr = MemberValueType<Attrib>;
+ setPointer<Mbr>(MP<Mbr, VertexT> {Attrib});
}
- template<typename VertexT, MP... Attribs>
+ template<typename VertexT, auto... Attribs>
void
configureAttribs(const GLuint divisor)
{
@@ -108,7 +107,7 @@ namespace Impl {
setPointer<VertexT>(0);
}
else {
- ((setPointer<Attribs>()), ...);
+ ((setPointer<VertexT, Attribs>()), ...);
}
glVertexArrayBindingDivisor(name, binding++, divisor);
}
diff --git a/gfx/gl/instanceVertices.h b/gfx/gl/instanceVertices.h
index d984938..9963a96 100644
--- a/gfx/gl/instanceVertices.h
+++ b/gfx/gl/instanceVertices.h
@@ -9,11 +9,12 @@
template<typename T> class InstanceVertices : protected glVector<T> {
using base = glVector<T>;
+ using IndexT = uint32_t;
public:
class [[nodiscard]] InstanceProxy {
public:
- InstanceProxy(InstanceVertices * iv, std::size_t idx) : instances {iv}, index {idx} { }
+ InstanceProxy(InstanceVertices * iv, IndexT idx) : instances {iv}, index {idx} { }
InstanceProxy(InstanceProxy && other) noexcept :
instances {std::exchange(other.instances, nullptr)}, index {other.index}
@@ -98,9 +99,9 @@ public:
return instances->lookup(index);
}
- private:
+ // private:
InstanceVertices<T> * instances;
- std::size_t index;
+ IndexT index;
};
template<typename... Params>
@@ -110,15 +111,15 @@ public:
if (!unused.empty()) {
auto idx = unused.back();
unused.pop_back();
- index[idx] = base::size();
+ index[idx] = static_cast<IndexT>(base::size());
reverseIndex.emplace_back(idx);
base::emplace_back(std::forward<Params>(params)...);
return InstanceProxy {this, idx};
}
- index.emplace_back(base::size());
- reverseIndex.push_back(base::size());
+ index.emplace_back(static_cast<IndexT>(base::size()));
+ reverseIndex.push_back(static_cast<IndexT>(base::size()));
base::emplace_back(std::forward<Params>(params)...);
- return InstanceProxy {this, index.size() - 1};
+ return InstanceProxy {this, static_cast<IndexT>(index.size() - 1)};
}
[[nodiscard]] GLuint
@@ -127,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;
@@ -176,7 +183,7 @@ public:
}
protected:
- static constexpr auto npos = static_cast<size_t>(-1);
+ static constexpr auto npos = static_cast<IndexT>(-1);
friend InstanceProxy;
base::size_type
@@ -186,9 +193,9 @@ protected:
}
void
- release(const size_t pidx)
+ release(const IndexT pidx)
{
- if (const size_t last = base::size() - 1; last != index[pidx]) {
+ if (const auto last = static_cast<IndexT>(base::size() - 1); last != index[pidx]) {
lookup(pidx) = std::move(base::back());
const auto movedKey = reverseIndex[last];
index[movedKey] = std::exchange(index[pidx], npos);
@@ -207,7 +214,7 @@ protected:
}
[[nodiscard]] T &
- lookup(size_t pindex)
+ lookup(IndexT pindex)
{
return base::data()[index[pindex]];
}
@@ -231,8 +238,8 @@ protected:
}
// Index into buffer given to nth proxy
- std::vector<size_t> index;
- std::vector<size_t> reverseIndex;
+ glVector<IndexT> index;
+ std::vector<IndexT> reverseIndex;
// List of free spaces in index
- std::vector<size_t> unused;
+ 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/models/lights.cpp b/gfx/models/lights.cpp
new file mode 100644
index 0000000..8c0e9e6
--- /dev/null
+++ b/gfx/models/lights.cpp
@@ -0,0 +1,11 @@
+#include "lights.h"
+
+SpotLightVertex::SpotLightVertex(const SpotLightDef & light, uint32_t parentObjectIdx) :
+ SpotLightDef {light}, LightCommonVertex {parentObjectIdx}
+{
+}
+
+PointLightVertex::PointLightVertex(const PointLightDef & light, uint32_t parentObjectIdx) :
+ PointLightDef {light}, LightCommonVertex {parentObjectIdx}
+{
+}
diff --git a/gfx/models/lights.h b/gfx/models/lights.h
new file mode 100644
index 0000000..586b3ef
--- /dev/null
+++ b/gfx/models/lights.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "config/types.h"
+
+struct LightCommon {
+ RelativePosition3D position;
+ RGB colour;
+ RelativeDistance kq;
+};
+
+struct LightCommonVertex {
+ uint32_t parentObject;
+};
+
+struct SpotLightDef : LightCommon {
+ Direction3D direction;
+ Angle arc;
+};
+
+struct PointLightDef : LightCommon { };
+
+struct SpotLightVertex : SpotLightDef, LightCommonVertex {
+ SpotLightVertex(const SpotLightDef &, uint32_t);
+};
+
+struct PointLightVertex : PointLightDef, LightCommonVertex {
+ PointLightVertex(const PointLightDef &, uint32_t);
+};
+
diff --git a/gfx/renderable.cpp b/gfx/renderable.cpp
index 8caab93..8523118 100644
--- a/gfx/renderable.cpp
+++ b/gfx/renderable.cpp
@@ -1,4 +1,55 @@
#include "renderable.h"
+#include "gfx/gl/sceneShader.h"
+#include "gl_traits.h"
+#include "location.h"
+#include "maths.h"
+#include "util.h"
+
+std::weak_ptr<Renderable::CommonLocationData> Renderable::commonLocationData;
+std::weak_ptr<Renderable::CommonSpotLights> Renderable::commonSpotLights;
+std::weak_ptr<Renderable::CommonPointLights> Renderable::commonPointLights;
+std::weak_ptr<glVertexArray> Renderable::commonInstancesSpotLightVAO, Renderable::commonInstancesPointLightVAO;
+
+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()
+{
+ createIfRequired(locationData, commonLocationData);
+ createIfRequired(spotLights, commonSpotLights);
+ createIfRequired(pointLights, commonPointLights);
+ if (createIfRequired(instancesSpotLightVAO, commonInstancesSpotLightVAO)) {
+ instancesSpotLightVAO->configure()
+ .addAttribs<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction,
+ &SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc,
+ &SpotLightVertex::parentObject>(0);
+ }
+ if (createIfRequired(instancesPointLightVAO, commonInstancesPointLightVAO)) {
+ instancesPointLightVAO->configure()
+ .addAttribs<PointLightVertex, &PointLightVertex::position, &PointLightVertex::colour,
+ &PointLightVertex::kq, &PointLightVertex::parentObject>(0);
+ }
+}
+
+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 &)
@@ -6,8 +57,31 @@ Renderable::preFrame(const Frustum &, const Frustum &)
}
void
-Renderable::lights(const SceneShader &) const
+Renderable::lights(const SceneShader & shader)
{
+ glDebugScope _ {0};
+ if (const auto instancesSpotLight = commonSpotLights.lock()) {
+ if (const auto scount = instancesSpotLight->size()) {
+ if (const auto instancesSpotLightVAO = commonInstancesSpotLightVAO.lock()) {
+ glDebugScope _ {*instancesSpotLightVAO, "Spot lights"};
+ shader.spotLightInst.use();
+ glBindVertexArray(*instancesSpotLightVAO);
+ instancesSpotLightVAO->useBuffer(0, *instancesSpotLight);
+ glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(scount));
+ }
+ }
+ }
+ if (const auto instancesPointLight = commonPointLights.lock()) {
+ if (const auto pcount = instancesPointLight->size()) {
+ if (const auto instancesPointLightVAO = commonInstancesPointLightVAO.lock()) {
+ glDebugScope _ {*instancesPointLightVAO, "Point lights"};
+ shader.pointLightInst.use();
+ glBindVertexArray(*instancesPointLightVAO);
+ instancesPointLightVAO->useBuffer(0, *instancesPointLight);
+ glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(pcount));
+ }
+ }
+ }
}
void
diff --git a/gfx/renderable.h b/gfx/renderable.h
index d0870ad..7f4f52e 100644
--- a/gfx/renderable.h
+++ b/gfx/renderable.h
@@ -1,5 +1,10 @@
#pragma once
+#include "gfx/gl/glVertexArray.h"
+#include "gfx/gl/instanceVertices.h"
+#include "gfx/models/lights.h"
+#include "gl_traits.h"
+#include <glm/mat3x3.hpp>
#include <special_members.h>
class SceneShader;
@@ -7,18 +12,47 @@ class Frustum;
class ShadowMapper;
class ShadowStenciller;
class BillboardPainter;
+class Location;
class Renderable {
public:
- Renderable() = default;
+ Renderable();
virtual ~Renderable() = default;
DEFAULT_MOVE_COPY(Renderable);
virtual void preFrame(const Frustum &, const Frustum &);
virtual void render(const SceneShader & shader, const Frustum &) const = 0;
- virtual void lights(const SceneShader & shader) const;
+ static void lights(const SceneShader & shader);
virtual void shadows(const ShadowMapper & shadowMapper, const Frustum &) const;
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;
+
+ using CommonSpotLights = InstanceVertices<SpotLightVertex>;
+ std::shared_ptr<CommonSpotLights> spotLights;
+ static std::weak_ptr<CommonSpotLights> commonSpotLights;
+ using CommonPointLights = InstanceVertices<PointLightVertex>;
+ std::shared_ptr<CommonPointLights> pointLights;
+ static std::weak_ptr<CommonPointLights> commonPointLights;
+ std::shared_ptr<glVertexArray> instancesSpotLightVAO, instancesPointLightVAO;
+ static std::weak_ptr<glVertexArray> commonInstancesSpotLightVAO, commonInstancesPointLightVAO;
};
+
+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/lib/util.cpp b/lib/util.cpp
deleted file mode 100644
index 408a76a..0000000
--- a/lib/util.cpp
+++ /dev/null
@@ -1 +0,0 @@
-#include "util.h"
diff --git a/lib/util.h b/lib/util.h
index cd7971b..093462d 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -3,6 +3,7 @@
#include <algorithm> // IWYU pragma: keep
#include <array>
#include <cstddef>
+#include <memory>
#include <tuple>
template<typename T, std::size_t N>
@@ -33,3 +34,24 @@ template<size_t... N> inline constexpr auto Nth = GetNth<N...> {};
inline constexpr auto GetFirst = Nth<0>;
inline constexpr auto GetSecond = Nth<1>;
inline constexpr auto GetSwapped = Nth<0, 1>;
+
+template<typename T, typename M> struct Decompose {
+ consteval Decompose(M T::*) { }
+
+ using ValueType = M;
+ using ContainerType = T;
+};
+
+template<auto MbrPtr> using MemberValueType = typename decltype(Decompose {MbrPtr})::ValueType;
+template<auto MbrPtr> using ContainerType = typename decltype(Decompose {MbrPtr})::ContainerType;
+
+template<typename T>
+bool
+createIfRequired(std::shared_ptr<T> & instance, std::weak_ptr<T> & common)
+{
+ if (!instance && !(instance = common.lock())) {
+ common = instance = std::make_shared<T>();
+ return true;
+ }
+ return false;
+}
diff --git a/test/Jamfile.jam b/test/Jamfile.jam
index da2a61a..baf0db4 100644
--- a/test/Jamfile.jam
+++ b/test/Jamfile.jam
@@ -79,5 +79,6 @@ run test-environment.cpp : : : <library>test ;
run test-ui.cpp : : : <library>test ;
compile test-static-enumDetails.cpp ;
compile test-static-stream_support.cpp ;
+compile test-static-util.cpp ;
alias perf : perf-assetFactory perf-persistence perf-geoData perf-instancing perf-terrain ;
explicit perf ;
diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp
index 90fae9b..b1cd822 100644
--- a/test/test-assetFactory.cpp
+++ b/test/test-assetFactory.cpp
@@ -51,7 +51,7 @@ namespace {
void
lights(const SceneShader & shader) const override
{
- objects.apply(&Renderable::lights, shader);
+ Renderable::lights(shader);
}
void
diff --git a/test/test-render.cpp b/test/test-render.cpp
index 35d71f3..688275c 100644
--- a/test/test-render.cpp
+++ b/test/test-render.cpp
@@ -1,3 +1,4 @@
+#include "game/scenary/light.h"
#define BOOST_TEST_MODULE test_render
#include "testHelpers.h"
@@ -49,14 +50,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>(
@@ -71,6 +66,10 @@ namespace {
}
rail->addLinksBetween({42000, 50000, 1000}, {65000, 50000, 1000});
rail->addLinksBetween({65000, 50000, 1000}, {75000, 45000, 2000});
+ gameState->world.create<Light>(gameState->assets.at("old-lamp").dynamicCast<Illuminator>(),
+ Location {.pos = {25000, 52000, 1}, .rot = {}});
+ gameState->world.create<Light>(gameState->assets.at("r-light").dynamicCast<Illuminator>(),
+ Location {.pos = {20000, 57000, 1}, .rot = {}});
}
void
@@ -100,8 +99,9 @@ namespace {
}
void
- lights(const SceneShader &) const override
+ lights(const SceneShader & shader) const override
{
+ Renderable::lights(shader);
}
void
diff --git a/test/test-static-util.cpp b/test/test-static-util.cpp
new file mode 100644
index 0000000..2a8aa81
--- /dev/null
+++ b/test/test-static-util.cpp
@@ -0,0 +1,29 @@
+#include "util.h"
+
+namespace {
+ struct Base1 {
+ int a;
+ float b;
+ };
+
+ struct Base2 {
+ int x;
+ float y;
+ };
+
+ struct Sub : Base1, Base2 {
+ double value;
+ };
+
+ static_assert(std::is_same_v<MemberValueType<&Base1::a>, int>);
+ static_assert(std::is_same_v<MemberValueType<&Base2::y>, float>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::a>, int>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::y>, float>);
+ static_assert(std::is_same_v<MemberValueType<&Sub::value>, double>);
+
+ static_assert(std::is_same_v<ContainerType<&Base1::a>, Base1>);
+ static_assert(std::is_same_v<ContainerType<&Base2::y>, Base2>);
+ static_assert(std::is_same_v<ContainerType<&Sub::a>, Base1>);
+ static_assert(std::is_same_v<ContainerType<&Sub::y>, Base2>);
+ static_assert(std::is_same_v<ContainerType<&Sub::value>, Sub>);
+}
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp
index cc81a9b..01b93bb 100644
--- a/ui/gameMainWindow.cpp
+++ b/ui/gameMainWindow.cpp
@@ -124,9 +124,9 @@ GameMainWindow::environment(const SceneShader &, const SceneRenderer & r) const
}
void
-GameMainWindow::lights(const SceneShader & shader) const
+GameMainWindow::lights(const SceneShader & sceneShader) const
{
- gameState->world.apply<const Renderable>(&Renderable::lights, shader);
+ Renderable::lights(sceneShader);
}
void