GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Lights3D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_3D_LIGHTS_3D_HPP
5#define GREM_GRAPHICS_3D_LIGHTS_3D_HPP
6
8
17#include <GREM/core/math.hpp>
23
24namespace grem::graphics {
25
26class Device; // Forward declaration, to avoid including Device.hpp.
27class Renderer3D; // Forward declaration, to avoid a circular include of Renderer3D.hpp.
28
35
39enum class LightType : uint32_t { // NOLINT(performance-enum-size)
45};
46
57
65 [[nodiscard]] constexpr bool operator==(const AmbientLightOptions3D& other) const noexcept = default;
66};
67
77 vec3 direction{0.0f, -1.0f, 0.0f};
78
84
90
96
100 bool shadowMapped = true;
101
109 [[nodiscard]] constexpr bool operator==(const SunLightOptions3D& other) const noexcept = default;
110};
111
120 vec3 direction{0.0f, -1.0f, 0.0f};
121
127
133
139
143 bool shadowMapped = true;
144
152 [[nodiscard]] constexpr bool operator==(const DirectionalLightOptions3D& other) const noexcept = default;
153};
154
163 vec3 position{0.0f, 0.0f, 0.0f};
164
170 float range = 10.0f;
171
177
183 float shadowNearZ = 0.01f;
184
190 float shadowFarZ = 100.0f;
191
197
203
207 bool shadowMapped = true;
208
216 [[nodiscard]] constexpr bool operator==(const PointLightOptions3D& other) const noexcept = default;
217};
218
227 vec3 position{0.0f, 0.0f, 0.0f};
228
232 vec3 direction{0.0f, -1.0f, 0.0f};
233
239 float range = 10.0f;
240
247
254
260
266 float shadowNearZ = 0.01f;
267
273 float shadowFarZ = 100.0f;
274
280
286
290 bool shadowMapped = true;
291
299 [[nodiscard]] constexpr bool operator==(const SpotLightOptions3D& other) const noexcept = default;
300};
301
310 static constexpr size_t MAX_SHADOW_CASCADE_COUNT = 8;
311
317
323
329
335 5.0f,
336 13.0f,
337 35.0f,
338 100.0f,
339 300.0f,
340 };
341
349
357 [[nodiscard]] constexpr bool operator==(const Lights3DOptions& other) const noexcept = default;
358};
359
363class Lights3D {
364public:
367
379
381 using ParameterBuffer = UniformBuffer<Parameters, "Lights3DParameters">;
382
388
390 using ShadowMatrixBuffer = StorageBuffer<ShadowMatrixFields, "Lights3DShadowMatrices">;
391
403 GREM_API(graphics_3d) explicit Lights3D(Device& device, const Lights3DOptions& options = {});
404
408 GREM_API(graphics_3d) void clearLights() noexcept {
409 lights.clear();
410 shadowMapIndicesDirty = true;
411 shadowMatrixIndicesDirty = true;
412 }
413
428 GREM_API(graphics_3d) LightID createAmbientLight(const AmbientLightOptions3D& options);
429
447 GREM_API(graphics_3d) LightID createSunLight(const SunLightOptions3D& options);
448
465 GREM_API(graphics_3d) LightID createDirectionalLight(const DirectionalLightOptions3D& options);
466
484 GREM_API(graphics_3d) LightID createPointLight(const PointLightOptions3D& options);
485
503 GREM_API(graphics_3d) LightID createSpotLight(const SpotLightOptions3D& options);
504
514 [[nodiscard]] GREM_API(graphics_3d) bool containsLight(LightID id) const noexcept;
515
529 GREM_API(graphics_3d) bool destroyLight(LightID id);
530
539 GREM_API(graphics_3d) Optional<LightType> getLightType(LightID id) const;
540
550
560
570
580
590
600 GREM_API(graphics_3d) void setAmbientLightOptions(LightID id, const AmbientLightOptions3D& newOptions);
601
613 GREM_API(graphics_3d) void setSunLightOptions(LightID id, const SunLightOptions3D& newOptions);
614
626 GREM_API(graphics_3d) void setDirectionalLightOptions(LightID id, const DirectionalLightOptions3D& newOptions);
627
639 GREM_API(graphics_3d) void setPointLightOptions(LightID id, const PointLightOptions3D& newOptions);
640
652 GREM_API(graphics_3d) void setSpotLightOptions(LightID id, const SpotLightOptions3D& newOptions);
653
662 GREM_API(graphics_3d) void setLightPosition(LightID id, vec3 newPosition);
663
673 GREM_API(graphics_3d) void setLightDirection(LightID id, vec3 newDirection);
674
685 GREM_API(graphics_3d) void setLightRange(LightID id, float newRange);
686
697 GREM_API(graphics_3d) void setLightInnerConeAngle(LightID id, float newInnerConeAngle);
698
709 GREM_API(graphics_3d) void setLightOuterConeAngle(LightID id, float newOuterConeAngle);
710
720 GREM_API(graphics_3d) void setLightColor(LightID id, Color newColor);
721
731 GREM_API(graphics_3d) void setLightShadowNearZ(LightID id, float newShadowNearZ);
732
742 GREM_API(graphics_3d) void setLightShadowFarZ(LightID id, float newShadowFarZ);
743
754 GREM_API(graphics_3d) void setLightShadowMapNormalOffsetBiasConstantFactor(LightID id, float newShadowMapNormalOffsetBiasConstantFactor);
755
766 GREM_API(graphics_3d) void setLightShadowMapNormalOffsetBiasSlopeFactor(LightID id, float newShadowMapNormalOffsetBiasSlopeFactor);
767
779 GREM_API(graphics_3d) void setLightShadowMapped(LightID id, bool newShadowMapped);
780
789 void setOptions(const Lights3DOptions& newOptions) {
790 if (newOptions.cascadedShadowMapResolution != options.cascadedShadowMapResolution) {
791 cascadedShadowMapCapacity = 0;
792 }
793 if (newOptions.pointLightShadowMapResolution != options.pointLightShadowMapResolution) {
794 pointLightShadowMapCapacity = 0;
795 }
796 if (newOptions.spotLightShadowMapResolution != options.spotLightShadowMapResolution) {
797 spotLightShadowMapCapacity = 0;
798 }
799 options = newOptions;
800 shadowMapIndicesDirty = true;
801 shadowMatrixIndicesDirty = true;
802 parameterBufferDirty = true;
803 }
804
810 [[nodiscard]] Lights3DOptions getOptions() const noexcept {
811 return options;
812 }
813
835 auto forEachLight(auto callback) {
836 constexpr bool CALLBACK_RETURNS_BOOL = convertible_to<decltype(callback(LightID{})), bool>;
837 for (const auto& [lightID, light] : lights) {
838 if constexpr (CALLBACK_RETURNS_BOOL) {
839 if (callback(lightID)) {
840 return true;
841 }
842 } else {
843 callback(lightID);
844 }
845 }
846 if constexpr (CALLBACK_RETURNS_BOOL) {
847 return false;
848 }
849 }
850
851private:
852 friend Renderer3D;
853
854 struct Light {
855 vec3 direction{0.0f, 0.0f, 0.0f};
856 float range = 0.0f;
857 Color color;
858 vec3 position{0.0f, 0.0f, 0.0f};
859 float type;
860 float innerConeCosine = 0.0f;
861 float outerConeCosine = 0.0f;
862 float shadowMapIndex = -1.0f;
863 float shadowMatrixIndex = -1.0f;
864 float shadowNearZ = 0.01f;
865 float shadowFarZ = 100.0f;
866 float shadowMapNormalOffsetBiasConstantFactor = 0.04f;
867 float shadowMapNormalOffsetBiasSlopeFactor = 1.0f;
868 };
869
870 GREM_API(graphics_3d) void flushIndices(Device& device) const;
871 GREM_API(graphics_3d) void flush(Device& device) const;
872
873 Lights3DOptions options;
874 mutable Registry<Light, LightID> lights{};
875 mutable uint32_t cascadedShadowMapCapacity = 0;
876 mutable uint32_t pointLightShadowMapCapacity = 0;
877 mutable uint32_t spotLightShadowMapCapacity = 0;
878 mutable Texture cascadedShadowMaps{};
879 mutable Texture pointLightShadowMaps{};
880 mutable Texture spotLightShadowMaps{};
881 mutable Buffer<ShadowMatrixFields> shadowMatrices{};
882 mutable ParameterBuffer parameterBuffer;
883 mutable ShadowMatrixBuffer shadowMatrixBuffer;
884 mutable bool shadowMapIndicesDirty = true;
885 mutable bool shadowMatrixIndicesDirty = true;
886 mutable bool parameterBufferDirty = true;
887 mutable bool shadowMatrixBufferDirty = true;
888};
889
890} // namespace grem::graphics
891
892#endif
Floating-point linear RGBA color in the Rec.
Definition Color.hpp:60
static const Color WHITE
Definition Color.hpp:215
Definition InplaceBuffer.hpp:24
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr RegistryElementIDBase() noexcept=default
Construct an invalid element handle.
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
void setAmbientLightOptions(LightID id, const AmbientLightOptions3D &newOptions)
Update the options of an ambient light.
StorageBuffer< ShadowMatrixFields, "Lights3DShadowMatrices"> ShadowMatrixBuffer
Shader buffer for shadow matrices.
Definition Lights3D.hpp:390
void setLightDirection(LightID id, vec3 newDirection)
Update the direction of a light.
LightID createDirectionalLight(const DirectionalLightOptions3D &options)
Create a purely directional light with infinite range.
void setSunLightOptions(LightID id, const SunLightOptions3D &newOptions)
Update the options of a sun light.
static constexpr size_t MAX_SHADOW_CASCADE_COUNT
Maximum number of cascade levels used for cascaded shadow mapping of directional lights.
Definition Lights3D.hpp:366
void setLightShadowFarZ(LightID id, float newShadowFarZ)
Update the shadow far plane distance of a light.
void setLightShadowNearZ(LightID id, float newShadowNearZ)
Update the shadow near plane distance of a light.
void setSpotLightOptions(LightID id, const SpotLightOptions3D &newOptions)
Update the options of a spot light.
void setLightShadowMapNormalOffsetBiasConstantFactor(LightID id, float newShadowMapNormalOffsetBiasConstantFactor)
Update the shadow map constant normal offset bias factor of a light.
Optional< PointLightOptions3D > getPointLightOptions(LightID id) const
Get the options of a specific point light.
void setLightPosition(LightID id, vec3 newPosition)
Update the position of a light.
Optional< LightType > getLightType(LightID id) const
Get the light type of a specific light.
Optional< AmbientLightOptions3D > getAmbientLightOptions(LightID id) const
Get the options of a specific ambient light.
void clearLights() noexcept
Destroy all lights and remove them from the set.
Definition Lights3D.hpp:408
Lights3D(Device &device, const Lights3DOptions &options={})
Construct a set of lights.
void setLightShadowMapNormalOffsetBiasSlopeFactor(LightID id, float newShadowMapNormalOffsetBiasSlopeFactor)
Update the shadow map slope-scaled normal offset bias factor of a light.
void setOptions(const Lights3DOptions &newOptions)
Set the configuration options of the light set.
Definition Lights3D.hpp:789
Optional< DirectionalLightOptions3D > getDirectionalLightOptions(LightID id) const
Get the options of a specific directional light.
void setPointLightOptions(LightID id, const PointLightOptions3D &newOptions)
Update the options of a point light.
LightID createPointLight(const PointLightOptions3D &options)
Create an infinitesimally small point light, which can be used to approximate e.g....
void setLightInnerConeAngle(LightID id, float newInnerConeAngle)
Update the inner cone angle of a light.
void setLightRange(LightID id, float newRange)
Update the range of a light.
void setLightOuterConeAngle(LightID id, float newOuterConeAngle)
Update the outer cone angle of a light.
Optional< SunLightOptions3D > getSunLightOptions(LightID id) const
Get the options of a specific sun light.
bool containsLight(LightID id) const noexcept
Check if a specific light handle is still valid, meaning that the light has not yet been destroyed.
void setLightColor(LightID id, Color newColor)
Update the color of a light.
void setDirectionalLightOptions(LightID id, const DirectionalLightOptions3D &newOptions)
Update the options of a directional light.
Lights3DOptions getOptions() const noexcept
Get the configuration options of the light set.
Definition Lights3D.hpp:810
LightID createAmbientLight(const AmbientLightOptions3D &options)
Create an ambient light source, which adds an (unrealistic) constant amount of light to everything.
LightID createSunLight(const SunLightOptions3D &options)
Create a sun light source, or a directional light that is tied to the skybox and replaces its baked l...
bool destroyLight(LightID id)
Destroy a light and remove it from the set.
auto forEachLight(auto callback)
Execute a callback function for each light in the light set.
Definition Lights3D.hpp:835
LightID createSpotLight(const SpotLightOptions3D &options)
Create a point light that is limited to a cone shape, which can be used to approximate e....
void setLightShadowMapped(LightID id, bool newShadowMapped)
Update whether a light should use shadow mapping or not.
Optional< SpotLightOptions3D > getSpotLightOptions(LightID id) const
Get the options of a specific spot light.
UniformBuffer< Parameters, "Lights3DParameters"> ParameterBuffer
Shader buffer for light parameters.
Definition Lights3D.hpp:381
Persistent system that provides 3D rendering capabilities.
Definition Renderer3D.hpp:120
GPU memory buffer containing an array of structs to be read by a shader program.
Definition buffers.hpp:493
GPU memory buffer containing a fixed-size struct of uniform parameters to be read by a shader program...
Definition buffers.hpp:426
Definition concepts.hpp:47
Definition concepts.hpp:243
Definition buffer_layouts.hpp:20
LightType
Type of a light in a Lights3D set.
Definition Lights3D.hpp:39
@ DIRECTIONAL_LIGHT
See DirectionalLightOptions3D.
Definition Lights3D.hpp:42
@ POINT_LIGHT
See PointLightOptions3D.
Definition Lights3D.hpp:43
@ SPOT_LIGHT
See SpotLightOptions3D.
Definition Lights3D.hpp:44
@ SUN_LIGHT
See SunLightOptions3D.
Definition Lights3D.hpp:41
@ AMBIENT_LIGHT
See AmbientLightOptions3D.
Definition Lights3D.hpp:40
vec< 3, float > vec3
Definition math.hpp:439
mat< 4, 4, float > mat4
Definition math.hpp:806
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertDegreesToRadians(T angleInDegrees)
Definition math.hpp:3390
Definition Array.hpp:20
Configuration options for a 3D light source representing the ambient light in a scene,...
Definition Lights3D.hpp:51
constexpr bool operator==(const AmbientLightOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.
Color color
Color of the light, where the alpha component controls the light's intensity.
Definition Lights3D.hpp:56
Configuration options for a 3D light source representing a purely directional light with infinite ran...
Definition Lights3D.hpp:116
bool shadowMapped
Whether the light should use shadow mapping or not.
Definition Lights3D.hpp:143
vec3 direction
Vector representing the direction of the light, in world coordinates.
Definition Lights3D.hpp:120
Color color
Color of the light, where the alpha component controls the light's intensity.
Definition Lights3D.hpp:126
constexpr bool operator==(const DirectionalLightOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.
float shadowMapNormalOffsetBiasSlopeFactor
Slope-scaled normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:138
float shadowMapNormalOffsetBiasConstantFactor
Constant normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:132
Opaque handle to a specific light in a Lights3D set.
Definition Lights3D.hpp:32
constexpr RegistryElementIDBase() noexcept=default
Construct an invalid element handle.
Configuration options for a Lights3D set.
Definition Lights3D.hpp:305
uint32_t cascadedShadowMapResolution
Width, in texels, of each cascade level of the shadow maps of sun/directional lights.
Definition Lights3D.hpp:316
static constexpr size_t MAX_SHADOW_CASCADE_COUNT
Maximum number of cascade levels used for cascaded shadow mapping of directional lights.
Definition Lights3D.hpp:310
uint32_t spotLightShadowMapResolution
Width, in texels, of the shadow maps of spot lights.
Definition Lights3D.hpp:328
float shadowCascadeBlendSize
Length of the blend area between cascade levels for cascaded shadow mapping of sun/directional lights...
Definition Lights3D.hpp:348
constexpr bool operator==(const Lights3DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
uint32_t pointLightShadowMapResolution
Width, in texels, of the shadow maps of point lights.
Definition Lights3D.hpp:322
InplaceBuffer< float, MAX_SHADOW_CASCADE_COUNT > shadowCascadeFrustumFarPlaneDistances
Distances of the far planes of each cascade level's view frustum used for cascaded shadow mapping of ...
Definition Lights3D.hpp:334
Struct of shader parameters for a set of lights.
Definition Lights3D.hpp:369
float lightsShadowCascadeBlendSize
Length of the blend area between cascade levels for cascaded shadow mapping of directional lights.
Definition Lights3D.hpp:377
Array< float, Lights3D::MAX_SHADOW_CASCADE_COUNT > lightsShadowCascadeFrustumFarPlaneDistances
Distances to the far planes of each cascade level's view frustum used for cascaded shadow mapping of ...
Definition Lights3D.hpp:371
uint32_t lightsShadowCascadeCount
Number of shadow cascade levels for cascaded shadow mapping of directional lights.
Definition Lights3D.hpp:374
Struct of shader fields representing the shadow matrix of a light.
Definition Lights3D.hpp:384
mat4 shadowMatrix
Combined view-projection matrix of the light.
Definition Lights3D.hpp:386
Configuration options for an infinitesimally small point light source, which can be used to approxima...
Definition Lights3D.hpp:159
float shadowNearZ
Distance to the near plane of the light's shadow map.
Definition Lights3D.hpp:183
vec3 position
Position of the light, in world coordinates.
Definition Lights3D.hpp:163
float shadowMapNormalOffsetBiasSlopeFactor
Slope-scaled normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:202
Color color
Color of the light, where the alpha component controls the light's intensity.
Definition Lights3D.hpp:176
constexpr bool operator==(const PointLightOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.
float shadowMapNormalOffsetBiasConstantFactor
Constant normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:196
float shadowFarZ
Distance to the near plane of the light's shadow map.
Definition Lights3D.hpp:190
bool shadowMapped
Whether the light should use shadow mapping or not.
Definition Lights3D.hpp:207
Configuration options for a point light source that is limited to a cone shape, which can be used to ...
Definition Lights3D.hpp:223
float innerConeAngle
Angle, from the light's direction axis, of the inner limit where the light cone is the brightest,...
Definition Lights3D.hpp:246
float shadowNearZ
Distance to the near plane of the light's shadow map.
Definition Lights3D.hpp:266
float shadowFarZ
Distance to the near plane of the light's shadow map.
Definition Lights3D.hpp:273
vec3 direction
Vector representing the direction of the light, in world coordinates.
Definition Lights3D.hpp:232
bool shadowMapped
Whether the light should use shadow mapping or not.
Definition Lights3D.hpp:290
vec3 position
Position of the light, in world coordinates.
Definition Lights3D.hpp:227
float shadowMapNormalOffsetBiasSlopeFactor
Slope-scaled normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:285
float outerConeAngle
Angle, from the light's direction axis, of the outer limit where the light cone fades to zero,...
Definition Lights3D.hpp:253
float shadowMapNormalOffsetBiasConstantFactor
Constant normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:279
Color color
Color of the light, where the alpha component controls the light's intensity.
Definition Lights3D.hpp:259
constexpr bool operator==(const SpotLightOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.
Configuration options for a 3D light source representing a directional light that is tied to the skyb...
Definition Lights3D.hpp:73
bool shadowMapped
Whether the light should use shadow mapping or not.
Definition Lights3D.hpp:100
vec3 direction
Vector representing the direction of the light, in world coordinates.
Definition Lights3D.hpp:77
Color color
Color of the light, where the alpha component controls the light's intensity.
Definition Lights3D.hpp:83
float shadowMapNormalOffsetBiasConstantFactor
Constant normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:89
float shadowMapNormalOffsetBiasSlopeFactor
Slope-scaled normal offset bias factor to use when sampling the shadow map of the light.
Definition Lights3D.hpp:95
constexpr bool operator==(const SunLightOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.