summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--game/mixins/lights.cpp30
-rw-r--r--game/mixins/lights.h26
-rw-r--r--game/scenary/illuminator.h8
-rw-r--r--game/scenary/light.cpp9
-rw-r--r--game/scenary/light.h2
-rw-r--r--game/vehicles/railVehicle.cpp1
-rw-r--r--game/vehicles/railVehicle.h2
-rw-r--r--game/vehicles/railVehicleClass.cpp4
-rw-r--r--game/vehicles/railVehicleClass.h6
9 files changed, 65 insertions, 23 deletions
diff --git a/game/mixins/lights.cpp b/game/mixins/lights.cpp
new file mode 100644
index 0000000..6829bbb
--- /dev/null
+++ b/game/mixins/lights.cpp
@@ -0,0 +1,30 @@
+#include "lights.h"
+#include "gfx/renderable.h"
+
+bool
+AssetLights::persist(Persistence::PersistenceStore & store)
+{
+ return STORE_HELPER(pointLight, Persistence::Appender<decltype(pointLight)>)
+ && STORE_HELPER(spotLight, Persistence::Appender<decltype(spotLight)>);
+}
+
+void
+InstanceLights::lightsEnable(AnyPtr<const AssetLights> asset, uint32_t owner)
+{
+ auto createLights = [owner](const auto & assetLights, auto & lightInstances, auto commonLights) {
+ std::ranges::transform(assetLights | std::views::enumerate, std::inserter(lightInstances, lightInstances.end()),
+ [&commonLights, owner](const auto & idxAndLight) {
+ const auto & [idx, light] = idxAndLight;
+ return std::make_pair(idx, commonLights->acquire(*light, owner));
+ });
+ };
+ createLights(asset->spotLight, spotLightInstances, Renderable::commonSpotLights.lock());
+ createLights(asset->pointLight, pointLightInstances, Renderable::commonPointLights.lock());
+}
+
+void
+InstanceLights::lightsDisable()
+{
+ spotLightInstances.clear();
+ pointLightInstances.clear();
+}
diff --git a/game/mixins/lights.h b/game/mixins/lights.h
new file mode 100644
index 0000000..c2207a5
--- /dev/null
+++ b/game/mixins/lights.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include "assetFactory/lights.h"
+#include "gfx/gl/instanceVertices.h"
+#include <flat_map>
+
+class AssetLights {
+protected:
+ bool persist(Persistence::PersistenceStore & store);
+ template<typename T> using LightVec = std::vector<typename T::Ptr>;
+
+ LightVec<SpotLight> spotLight;
+ LightVec<PointLight> pointLight;
+
+ friend class InstanceLights;
+};
+
+class InstanceLights {
+protected:
+ template<typename V> using LightInstanceMap = std::flat_map<size_t, typename InstanceVertices<V>::InstanceProxy>;
+ LightInstanceMap<SpotLightVertex> spotLightInstances;
+ LightInstanceMap<PointLightVertex> pointLightInstances;
+
+ void lightsEnable(AnyPtr<const AssetLights>, uint32_t);
+ void lightsDisable();
+};
diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h
index 216b536..2373812 100644
--- a/game/scenary/illuminator.h
+++ b/game/scenary/illuminator.h
@@ -1,7 +1,7 @@
#pragma once
#include "assetFactory/asset.h"
-#include "assetFactory/lights.h"
+#include "game/mixins/lights.h"
#include "gfx/gl/instanceVertices.h"
#include "gfx/models/texture.h"
#include "gfx/renderable.h"
@@ -9,7 +9,7 @@
class SceneShader;
class Location;
-class Illuminator : public Asset, public Renderable, public StdTypeDefs<Illuminator> {
+class Illuminator : public Asset, public Renderable, public AssetLights, public StdTypeDefs<Illuminator> {
Mesh::Ptr bodyMesh;
Texture::Ptr texture;
std::shared_ptr<glVertexArray> instanceVAO;
@@ -29,8 +29,4 @@ 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;
};
diff --git a/game/scenary/light.cpp b/game/scenary/light.cpp
index c51efda..455d5b5 100644
--- a/game/scenary/light.cpp
+++ b/game/scenary/light.cpp
@@ -5,12 +5,5 @@ Light::Light(std::shared_ptr<const Illuminator> type, const Location & position)
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);
- });
+ lightsEnable(this->type, instance->location.index);
}
diff --git a/game/scenary/light.h b/game/scenary/light.h
index b1ea469..0b9320c 100644
--- a/game/scenary/light.h
+++ b/game/scenary/light.h
@@ -5,7 +5,7 @@
class Location;
-class Light : public WorldObject {
+class Light : public WorldObject, public InstanceLights {
std::shared_ptr<const Illuminator> type;
InstanceVertices<Illuminator::InstanceVertex>::InstanceProxy instance;
diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp
index c11d817..b5de833 100644
--- a/game/vehicles/railVehicle.cpp
+++ b/game/vehicles/railVehicle.cpp
@@ -20,6 +20,7 @@ RailVehicle::RailVehicle(RailVehicleClassPtr rvc, GlobalPosition3D position) :
Location {.pos = position + RelativePosition3D {0, -rvc->wheelBase / 2.F, 0}, .rot = {}}))},
rvClass {std::move(rvc)}
{
+ lightsEnable(rvClass, get()->body.index);
}
void
diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h
index 0f341f9..c02c19e 100644
--- a/game/vehicles/railVehicle.h
+++ b/game/vehicles/railVehicle.h
@@ -8,7 +8,7 @@
template<typename> class Ray;
class Train;
-class RailVehicle : Selectable, RailVehicleClass::Instance {
+class RailVehicle : Selectable, RailVehicleClass::Instance, public InstanceLights {
public:
explicit RailVehicle(RailVehicleClassPtr rvc, GlobalPosition3D = {});
diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp
index 5ee778b..cfdc52d 100644
--- a/game/vehicles/railVehicleClass.cpp
+++ b/game/vehicles/railVehicleClass.cpp
@@ -12,9 +12,7 @@ bool
RailVehicleClass::persist(Persistence::PersistenceStore & store)
{
return STORE_TYPE && STORE_MEMBER(length) && STORE_MEMBER(wheelBase) && STORE_MEMBER(maxSpeed)
- && STORE_NAME_HELPER("bogie", bogies, Asset::MeshArrayConstruct)
- && STORE_HELPER(pointLight, Persistence::Appender<decltype(pointLight)>)
- && STORE_HELPER(spotLight, Persistence::Appender<decltype(spotLight)>)
+ && STORE_NAME_HELPER("bogie", bogies, Asset::MeshArrayConstruct) && AssetLights::persist(store)
&& STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store);
}
diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h
index b7f6c59..1ea87cd 100644
--- a/game/vehicles/railVehicleClass.h
+++ b/game/vehicles/railVehicleClass.h
@@ -1,7 +1,7 @@
#pragma once
#include "assetFactory/asset.h"
-#include "assetFactory/lights.h"
+#include "game/mixins/lights.h"
#include "gfx/gl/instanceVertices.h"
#include "gfx/models/mesh.h"
#include "gfx/models/texture.h"
@@ -13,7 +13,7 @@ class SceneShader;
class ShadowMapper;
class Location;
-class RailVehicleClass : public Renderable, public Asset {
+class RailVehicleClass : public Renderable, public Asset, public AssetLights {
public:
void render(const SceneShader & shader, const Frustum &) const override;
void shadows(const ShadowMapper & shadowMapper, const Frustum &) const override;
@@ -27,8 +27,6 @@ public:
std::array<Mesh::Ptr, 2> bogies;
Mesh::Ptr bodyMesh;
Texture::Ptr texture;
- std::vector<SpotLight::Ptr> spotLight;
- std::vector<PointLight::Ptr> pointLight;
float wheelBase;
float length;
float maxSpeed;