From 30fcb8bce7b60b6754a0d35f2f15b627d0fba4cb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 1 Apr 2026 20:21:23 +0100 Subject: Have Renderable manage all instance lights Spot and point lights now existing in Renderable, the vertex data contains the owning parent object's index in CommonLocationData, the render step is just a single draw call. --- gfx/gl/lights.h | 16 ---------------- gfx/models/lights.cpp | 11 +++++++++++ gfx/models/lights.h | 29 +++++++++++++++++++++++++++++ gfx/renderable.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- gfx/renderable.h | 15 ++++++++++++--- 5 files changed, 93 insertions(+), 20 deletions(-) delete mode 100644 gfx/gl/lights.h create mode 100644 gfx/models/lights.cpp create mode 100644 gfx/models/lights.h (limited to 'gfx') diff --git a/gfx/gl/lights.h b/gfx/gl/lights.h deleted file mode 100644 index 3247e25..0000000 --- a/gfx/gl/lights.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "config/types.h" - -struct LightCommonVertex { - RelativePosition3D position; - RGB colour; - RelativeDistance kq; -}; - -struct SpotLightVertex : LightCommonVertex { - Direction3D direction; - Angle arc; -}; - -struct PointLightVertex : LightCommonVertex { }; 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 90035c3..8523118 100644 --- a/gfx/renderable.cpp +++ b/gfx/renderable.cpp @@ -1,10 +1,14 @@ #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; +std::weak_ptr Renderable::commonSpotLights; +std::weak_ptr Renderable::commonPointLights; +std::weak_ptr Renderable::commonInstancesSpotLightVAO, Renderable::commonInstancesPointLightVAO; Renderable::CommonLocation::CommonLocation(Location const & location) : position {location.pos, 0}, rotation {location.rot, 0}, rotationMatrix {location.getRotationTransform()} @@ -23,6 +27,19 @@ Renderable::CommonLocation ::operator=(Location const & location) Renderable::Renderable() { createIfRequired(locationData, commonLocationData); + createIfRequired(spotLights, commonSpotLights); + createIfRequired(pointLights, commonPointLights); + if (createIfRequired(instancesSpotLightVAO, commonInstancesSpotLightVAO)) { + instancesSpotLightVAO->configure() + .addAttribs(0); + } + if (createIfRequired(instancesPointLightVAO, commonInstancesPointLightVAO)) { + instancesPointLightVAO->configure() + .addAttribs(0); + } } GLuint @@ -40,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(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(pcount)); + } + } + } } void diff --git a/gfx/renderable.h b/gfx/renderable.h index b0a42f2..7f4f52e 100644 --- a/gfx/renderable.h +++ b/gfx/renderable.h @@ -1,6 +1,8 @@ #pragma once +#include "gfx/gl/glVertexArray.h" #include "gfx/gl/instanceVertices.h" +#include "gfx/models/lights.h" #include "gl_traits.h" #include #include @@ -20,7 +22,7 @@ public: 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; @@ -37,10 +39,17 @@ public: using CommonLocationData = InstanceVertices; using CommonLocationInstance = CommonLocationData::InstanceProxy; - std::shared_ptr locationData; - static std::weak_ptr commonLocationData; + + using CommonSpotLights = InstanceVertices; + std::shared_ptr spotLights; + static std::weak_ptr commonSpotLights; + using CommonPointLights = InstanceVertices; + std::shared_ptr pointLights; + static std::weak_ptr commonPointLights; + std::shared_ptr instancesSpotLightVAO, instancesPointLightVAO; + static std::weak_ptr commonInstancesSpotLightVAO, commonInstancesPointLightVAO; }; template<> struct gl_traits::InstanceProxy> { -- cgit v1.3