summaryrefslogtreecommitdiff
path: root/game/scenary
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-13 11:31:13 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-13 11:31:13 +0000
commit332355a9f4a4199e0ddb51852546d44ec54e176f (patch)
tree4998cde56004f53a0218dc5f00d578f939ffea08 /game/scenary
parentInteger support in persistence (diff)
parentOnly create VAOs for the light type(s) in use (diff)
downloadilt-332355a9f4a4199e0ddb51852546d44ec54e176f.tar.bz2
ilt-332355a9f4a4199e0ddb51852546d44ec54e176f.tar.xz
ilt-332355a9f4a4199e0ddb51852546d44ec54e176f.zip
Merge branch 'model-lights'
Diffstat (limited to 'game/scenary')
-rw-r--r--game/scenary/illuminator.cpp90
-rw-r--r--game/scenary/illuminator.h60
-rw-r--r--game/scenary/light.cpp7
-rw-r--r--game/scenary/light.h19
4 files changed, 176 insertions, 0 deletions
diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp
new file mode 100644
index 0000000..e3810ec
--- /dev/null
+++ b/game/scenary/illuminator.cpp
@@ -0,0 +1,90 @@
+#include "illuminator.h"
+#include "gfx/gl/sceneShader.h"
+#include "gfx/gl/vertexArrayObject.h"
+#include "gfx/models/texture.h" // IWYU pragma: keep
+
+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);
+}
+
+bool
+Illuminator::persist(Persistence::PersistenceStore & store)
+{
+ return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct)
+ && STORE_HELPER(pointLight, Persistence::Appender<decltype(pointLight)>)
+ && STORE_HELPER(spotLight, Persistence::Appender<decltype(spotLight)>) && Asset::persist(store);
+}
+
+void
+Illuminator::postLoad()
+{
+ if (spotLight.empty() && pointLight.empty()) {
+ throw std::logic_error {"Illuminator has no lights"};
+ }
+ texture = getTexture();
+ bodyMesh->configureVAO(instanceVAO)
+ .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 1);
+ if (!spotLight.empty()) {
+ instancesSpotLightVAO.emplace();
+ VertexArrayObject {*instancesSpotLightVAO}
+ .addAttribs<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction,
+ &SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc>(
+ instancesSpotLight.bufferName(), 0)
+ .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 1);
+ std::transform(
+ spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) {
+ return instancesSpotLight.acquire(*s);
+ });
+ }
+ if (!pointLight.empty()) {
+ instancesPointLightVAO.emplace();
+ VertexArrayObject {*instancesPointLightVAO}
+ .addAttribs<PointLightVertex, &PointLightVertex::position, &PointLightVertex::colour,
+ &PointLightVertex::kq>(instancesPointLight.bufferName(), 0)
+ .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 1);
+ std::transform(
+ pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) {
+ return instancesPointLight.acquire(*s);
+ });
+ }
+}
+
+void
+Illuminator::render(const SceneShader & shader) const
+{
+ if (const auto count = instances.size()) {
+ shader.basicInst.use();
+ if (texture) {
+ texture->bind();
+ }
+ bodyMesh->DrawInstanced(instanceVAO, static_cast<GLsizei>(count));
+ }
+}
+
+void
+Illuminator::lights(const SceneShader & shader) const
+{
+ if (const auto count = instances.size()) {
+ if (const auto scount = instancesSpotLight.size()) {
+ shader.spotLightInst.use();
+ glBindVertexArray(*instancesSpotLightVAO);
+ glDrawArraysInstanced(GL_POINTS, 0, static_cast<GLsizei>(scount), static_cast<GLsizei>(count));
+ }
+ if (const auto pcount = instancesPointLight.size()) {
+ shader.pointLightInst.use();
+ glBindVertexArray(*instancesPointLightVAO);
+ 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
new file mode 100644
index 0000000..893e5c7
--- /dev/null
+++ b/game/scenary/illuminator.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "assetFactory/asset.h"
+#include "gfx/gl/instanceVertices.h"
+#include "gfx/renderable.h"
+
+class SceneShader;
+class Location;
+class Texture;
+
+class Illuminator : public Asset, public Renderable, public StdTypeDefs<Illuminator> {
+ Mesh::Ptr bodyMesh;
+ std::shared_ptr<Texture> texture;
+ glVertexArray instanceVAO;
+ std::optional<glVertexArray> instancesSpotLightVAO, instancesPointLightVAO;
+
+public:
+ struct LightCommonVertex {
+ RelativePosition3D position;
+ RGB colour;
+ RelativeDistance kq;
+ };
+
+ 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;
+ };
+
+public:
+ using LocationVertex = std::pair<glm::mat4, GlobalPosition3D>;
+ mutable InstanceVertices<LocationVertex> instances;
+ mutable InstanceVertices<SpotLightVertex> instancesSpotLight;
+ mutable InstanceVertices<PointLightVertex> instancesPointLight;
+ void render(const SceneShader &) 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;
+
+ 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
new file mode 100644
index 0000000..6207497
--- /dev/null
+++ b/game/scenary/light.cpp
@@ -0,0 +1,7 @@
+#include "light.h"
+#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)}
+{
+}
diff --git a/game/scenary/light.h b/game/scenary/light.h
new file mode 100644
index 0000000..0b19535
--- /dev/null
+++ b/game/scenary/light.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "game/worldobject.h"
+#include "illuminator.h"
+
+class Location;
+
+class Light : public WorldObject {
+ std::shared_ptr<const Illuminator> type;
+ InstanceVertices<Illuminator::LocationVertex>::InstanceProxy location;
+
+ void
+ tick(TickDuration) override
+ {
+ }
+
+public:
+ Light(std::shared_ptr<const Illuminator> type, const Location & position);
+};