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 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 game/scenary/illuminator.cpp (limited to 'game/scenary/illuminator.cpp') 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)); + } +} -- 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 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'game/scenary/illuminator.cpp') 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); } } -- 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/scenary/illuminator.cpp') 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 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'game/scenary/illuminator.cpp') 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); } -- 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/scenary/illuminator.cpp') 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 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 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'game/scenary/illuminator.cpp') 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)); } -- cgit v1.2.3