From bb693756b9aace889f806a768b54655023d163ab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 9 Jan 2024 20:17:55 +0000 Subject: Add the Illuminator (type) and Light (instance) classes --- game/scenary/illuminator.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ game/scenary/illuminator.h | 26 ++++++++++++++++++++++++++ game/scenary/light.cpp | 7 +++++++ game/scenary/light.h | 19 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 game/scenary/illuminator.cpp create mode 100644 game/scenary/illuminator.h create mode 100644 game/scenary/light.cpp create mode 100644 game/scenary/light.h (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp new file mode 100644 index 0000000..4989933 --- /dev/null +++ b/game/scenary/illuminator.cpp @@ -0,0 +1,40 @@ +#include "illuminator.h" +#include "gfx/gl/sceneShader.h" +#include "gfx/gl/vertexArrayObject.h" +#include "gfx/models/texture.h" +#include "location.h" + +bool +Illuminator::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store); +} + +void +Illuminator::postLoad() +{ + texture = getTexture(); + bodyMesh->configureVAO(instanceVAO) + .addAttribs(instances.bufferName(), 1); +} + +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(count)); + } +} + +void +Illuminator::lights(const SceneShader &) const +{ + if (const auto count = instances.size()) { + // shader.pointLight.use(); + // bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + } +} diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h new file mode 100644 index 0000000..db9acfb --- /dev/null +++ b/game/scenary/illuminator.h @@ -0,0 +1,26 @@ +#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 { + Mesh::Ptr bodyMesh; + std::shared_ptr texture; + glVertexArray instanceVAO; + +public: + using LocationVertex = std::pair; + mutable InstanceVertices instances; + void render(const SceneShader &) const override; + void lights(const SceneShader &) const override; + +protected: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + void postLoad() override; +}; 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 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 type; + InstanceVertices::InstanceProxy location; + + void + tick(TickDuration) override + { + } + +public: + Light(std::shared_ptr type, const Location & position); +}; -- cgit v1.2.3 From 8740d30b01f6c12e2fcea0450df73736cd17dc87 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Jan 2024 01:10:44 +0000 Subject: Add spot light definition, loader, and rendering Rendering is untested, data is passed to whatever GL program is currently active. --- game/scenary/illuminator.cpp | 27 ++++++++++++++++++++++++--- game/scenary/illuminator.h | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 4989933..43b6e4e 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -4,10 +4,18 @@ #include "gfx/models/texture.h" #include "location.h" +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::persist(Persistence::PersistenceStore & store) { - return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store); + return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) + && STORE_HELPER(spotLight, Persistence::Appender) && Asset::persist(store); } void @@ -16,6 +24,14 @@ Illuminator::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); + VertexArrayObject {instancesSpotLightVAO} + .addAttribs(instances.bufferName(), 0) + .addAttribs( + instancesSpotLight.bufferName(), 1); + std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { + return instancesSpotLight.acquire(*s); + }); } void @@ -34,7 +50,12 @@ void Illuminator::lights(const SceneShader &) const { if (const auto count = instances.size()) { - // shader.pointLight.use(); - // bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + if (const auto scount = instancesSpotLight.size()) { + // shader.pointLight.use(); + glBindVertexArray(instancesSpotLightVAO); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(count), static_cast(scount)); + } + + glBindVertexArray(0); } } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index db9acfb..99b87eb 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,11 +11,27 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO; + glVertexArray instanceVAO, instancesSpotLightVAO; + +public: + struct SpotLightVertex { + RelativePosition3D position; + Direction3D direction; + RGB colour; + RelativeDistance kq; + Angle arc; + }; + + struct SpotLight : Persistence::Persistable, SpotLightVertex, StdTypeDefs { + private: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + }; public: using LocationVertex = std::pair; mutable InstanceVertices instances; + mutable InstanceVertices instancesSpotLight; void render(const SceneShader &) const override; void lights(const SceneShader &) const override; @@ -23,4 +39,7 @@ protected: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; void postLoad() override; + + std::vector spotLight; + std::vector::InstanceProxy> spotLightInstances; }; -- cgit v1.2.3 From b8401062e1d3f5e6554ab7fd9b983ea63cfb05c5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Jan 2024 19:04:30 +0000 Subject: Initial commit with working light instancing --- game/scenary/illuminator.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 43b6e4e..6f51506 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -25,10 +25,10 @@ Illuminator::postLoad() bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); VertexArrayObject {instancesSpotLightVAO} - .addAttribs(instances.bufferName(), 0) .addAttribs( - instancesSpotLight.bufferName(), 1); + instancesSpotLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { return instancesSpotLight.acquire(*s); }); @@ -47,13 +47,13 @@ Illuminator::render(const SceneShader & shader) const } void -Illuminator::lights(const SceneShader &) const +Illuminator::lights(const SceneShader & shader) const { if (const auto count = instances.size()) { if (const auto scount = instancesSpotLight.size()) { - // shader.pointLight.use(); + shader.spotLightInst.use(); glBindVertexArray(instancesSpotLightVAO); - glDrawArraysInstanced(GL_POINTS, 0, static_cast(count), static_cast(scount)); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } glBindVertexArray(0); -- cgit v1.2.3 From 51eb25ea0f1373ca0442b02049406af38eae3b33 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 12 Jan 2024 19:35:58 +0000 Subject: Add model support for point lights Still invokes non-instanced point light shader --- game/scenary/illuminator.cpp | 20 ++++++++++++++++++++ game/scenary/illuminator.h | 17 ++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 6f51506..3de4e52 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -11,10 +11,17 @@ Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) && 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) && STORE_HELPER(spotLight, Persistence::Appender) && Asset::persist(store); } @@ -32,6 +39,14 @@ Illuminator::postLoad() std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { return instancesSpotLight.acquire(*s); }); + VertexArrayObject {instancesPointLightVAO} + .addAttribs(instancesPointLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); + std::transform( + pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { + return instancesPointLight.acquire(*s); + }); } void @@ -55,6 +70,11 @@ Illuminator::lights(const SceneShader & shader) const glBindVertexArray(instancesSpotLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } + if (const auto pcount = instancesPointLight.size()) { + shader.pointLightInst.use(); + glBindVertexArray(instancesPointLightVAO); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(pcount), static_cast(count)); + } glBindVertexArray(0); } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 99b87eb..a2b287d 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,7 +11,7 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO, instancesSpotLightVAO; + glVertexArray instanceVAO, instancesSpotLightVAO, instancesPointLightVAO; public: struct SpotLightVertex { @@ -22,16 +22,29 @@ public: Angle arc; }; + struct PointLightVertex { + RelativePosition3D position; + RGB colour; + RelativeDistance kq; + }; + struct SpotLight : Persistence::Persistable, SpotLightVertex, StdTypeDefs { private: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; + struct PointLight : Persistence::Persistable, PointLightVertex, StdTypeDefs { + private: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + }; + public: using LocationVertex = std::pair; mutable InstanceVertices instances; mutable InstanceVertices instancesSpotLight; + mutable InstanceVertices instancesPointLight; void render(const SceneShader &) const override; void lights(const SceneShader &) const override; @@ -41,5 +54,7 @@ protected: void postLoad() override; std::vector spotLight; + std::vector pointLight; std::vector::InstanceProxy> spotLightInstances; + std::vector::InstanceProxy> pointLightInstances; }; -- cgit v1.2.3 From 9a3ab8a6ae24e6e70ed7d69587c44a6fabb0a558 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:14:26 +0000 Subject: Minor tidy up --- game/scenary/illuminator.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 3de4e52..7d072f9 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -1,8 +1,7 @@ #include "illuminator.h" #include "gfx/gl/sceneShader.h" #include "gfx/gl/vertexArrayObject.h" -#include "gfx/models/texture.h" -#include "location.h" +#include "gfx/models/texture.h" // IWYU pragma: keep bool Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) @@ -28,6 +27,9 @@ Illuminator::persist(Persistence::PersistenceStore & store) void Illuminator::postLoad() { + if (spotLight.empty() && pointLight.empty()) { + throw std::logic_error {"Illuminator has no lights"}; + } texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); -- cgit v1.2.3 From e942bec77c77201ed50a2f6ecc77f3279a6bb4df Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:32:47 +0000 Subject: Merge common parts of light type vertices --- game/scenary/illuminator.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'game') diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index a2b287d..1c493b2 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -14,20 +14,19 @@ class Illuminator : public Asset, public Renderable, public StdTypeDefs { private: friend Persistence::SelectionPtrBase>; -- cgit v1.2.3 From e31f69ec26f5c128ae421eb418628ebcdfafb8f3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 11:23:12 +0000 Subject: Only create VAOs for the light type(s) in use --- game/scenary/illuminator.cpp | 43 +++++++++++++++++++++++++------------------ game/scenary/illuminator.h | 3 ++- 2 files changed, 27 insertions(+), 19 deletions(-) (limited to 'game') diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 7d072f9..e3810ec 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -33,22 +33,29 @@ Illuminator::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); - VertexArrayObject {instancesSpotLightVAO} - .addAttribs( - instancesSpotLight.bufferName(), 0) - .addAttribs(instances.bufferName(), 1); - std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { - return instancesSpotLight.acquire(*s); - }); - VertexArrayObject {instancesPointLightVAO} - .addAttribs(instancesPointLight.bufferName(), 0) - .addAttribs(instances.bufferName(), 1); - std::transform( - pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { - return instancesPointLight.acquire(*s); - }); + if (!spotLight.empty()) { + instancesSpotLightVAO.emplace(); + VertexArrayObject {*instancesSpotLightVAO} + .addAttribs( + instancesSpotLight.bufferName(), 0) + .addAttribs(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(instancesPointLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); + std::transform( + pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { + return instancesPointLight.acquire(*s); + }); + } } void @@ -69,12 +76,12 @@ Illuminator::lights(const SceneShader & shader) const if (const auto count = instances.size()) { if (const auto scount = instancesSpotLight.size()) { shader.spotLightInst.use(); - glBindVertexArray(instancesSpotLightVAO); + glBindVertexArray(*instancesSpotLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } if (const auto pcount = instancesPointLight.size()) { shader.pointLightInst.use(); - glBindVertexArray(instancesPointLightVAO); + glBindVertexArray(*instancesPointLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(pcount), static_cast(count)); } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 1c493b2..893e5c7 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,7 +11,8 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO, instancesSpotLightVAO, instancesPointLightVAO; + glVertexArray instanceVAO; + std::optional instancesSpotLightVAO, instancesPointLightVAO; public: struct LightCommonVertex { -- cgit v1.2.3