summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2026-04-01 20:21:23 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2026-04-01 20:21:23 +0100
commit30fcb8bce7b60b6754a0d35f2f15b627d0fba4cb (patch)
tree7e782f51affa8a2a40cf47e6ba69335a1289b2be /gfx
parentFix VertexArrayConfigurator with derived classes (diff)
downloadilt-30fcb8bce7b60b6754a0d35f2f15b627d0fba4cb.tar.bz2
ilt-30fcb8bce7b60b6754a0d35f2f15b627d0fba4cb.tar.xz
ilt-30fcb8bce7b60b6754a0d35f2f15b627d0fba4cb.zip
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.
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/lights.h16
-rw-r--r--gfx/models/lights.cpp11
-rw-r--r--gfx/models/lights.h29
-rw-r--r--gfx/renderable.cpp42
-rw-r--r--gfx/renderable.h15
5 files changed, 93 insertions, 20 deletions
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> Renderable::commonLocationData;
+std::weak_ptr<Renderable::CommonSpotLights> Renderable::commonSpotLights;
+std::weak_ptr<Renderable::CommonPointLights> Renderable::commonPointLights;
+std::weak_ptr<glVertexArray> 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<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction,
+ &SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc,
+ &SpotLightVertex::parentObject>(0);
+ }
+ if (createIfRequired(instancesPointLightVAO, commonInstancesPointLightVAO)) {
+ instancesPointLightVAO->configure()
+ .addAttribs<PointLightVertex, &PointLightVertex::position, &PointLightVertex::colour,
+ &PointLightVertex::kq, &PointLightVertex::parentObject>(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<GLsizei>(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<GLsizei>(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 <glm/mat3x3.hpp>
#include <special_members.h>
@@ -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<CommonLocation>;
using CommonLocationInstance = CommonLocationData::InstanceProxy;
-
std::shared_ptr<CommonLocationData> locationData;
-
static std::weak_ptr<CommonLocationData> commonLocationData;
+
+ using CommonSpotLights = InstanceVertices<SpotLightVertex>;
+ std::shared_ptr<CommonSpotLights> spotLights;
+ static std::weak_ptr<CommonSpotLights> commonSpotLights;
+ using CommonPointLights = InstanceVertices<PointLightVertex>;
+ std::shared_ptr<CommonPointLights> pointLights;
+ static std::weak_ptr<CommonPointLights> commonPointLights;
+ std::shared_ptr<glVertexArray> instancesSpotLightVAO, instancesPointLightVAO;
+ static std::weak_ptr<glVertexArray> commonInstancesSpotLightVAO, commonInstancesPointLightVAO;
};
template<> struct gl_traits<InstanceVertices<Renderable::CommonLocation>::InstanceProxy> {