SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
LightManager.hpp
Go to the documentation of this file.
1#ifndef _LIGHT_MANAGER_HPP_
2#define _LIGHT_MANAGER_HPP_
3
4#include <Core/OSDef.hpp>
6#include <Math/Vector.hpp>
7#include <Memory/RefPtr.hpp>
8
9namespace Sleak {
10
11 class Light;
12
13 namespace RenderEngine {
14 class BufferBase;
15 struct LightCBData;
16 }
17
18 /// Owns the registered light list and the GPU-side light/fog constant buffers; one instance per scene.
19 ///
20 /// Each Scene owns one, reachable through
21 /// SceneBase::GetLightManager(). Lights register themselves when their
22 /// GameObject is added to the scene, so you almost never call
23 /// RegisterLight() directly. What you do use it for is the scene-wide
24 /// atmosphere that is not attached to any single light: ambient color
25 /// and intensity, distance fog, and height fog.
26 ///
27 /// Ambient light is the floor value that keeps shadow-side surfaces
28 /// from going flat black. Tinting it toward the sky color, then keeping
29 /// the intensity low, reads as sky bounce without washing out the
30 /// directional lights doing the real work.
31 ///
32 /// Fog has two layers that stack. Distance fog blends between a horizon
33 /// color and a zenith color as the view tilts upward, fading in between
34 /// the start and end distances. Height fog adds exponential density
35 /// below a world height, which is what grounds figures on a floor plane
36 /// and pools haze in valleys.
37 ///
38 /// @code{.cpp}
39 /// if (auto* lm = GetLightManager()) {
40 /// lm->SetAmbientColor(0.30f, 0.34f, 0.40f);
41 /// lm->SetAmbientIntensity(0.75f);
42 ///
43 /// lm->SetFogColor(0.62f, 0.70f, 0.82f); // horizon
44 /// lm->SetFogZenithColor(0.42f, 0.55f, 0.80f); // straight up
45 /// lm->SetFogDistances(60.0f, 220.0f);
46 /// lm->SetFogEnabled(true);
47 ///
48 /// lm->SetHeightFogEnabled(true);
49 /// lm->SetHeightFogTop(4.0f);
50 /// lm->SetHeightFogDensity(0.15f);
51 /// lm->SetHeightFogFalloff(0.25f);
52 /// }
53 /// @endcode
54 ///
55 /// @see Light, DirectionalLight, PointLight, SpotLight, SceneBase
56 /// @ingroup lighting
57 class ENGINE_API LightManager {
58 public:
61
62 /// Allocates the GPU light buffer; call once before the first UpdateAndBind.
63 void Initialize();
64
65 void RegisterLight(Light* light);
66 void UnregisterLight(Light* light);
67
68 /// Packs every registered light and the fog/ambient parameters into the light buffer and binds it.
69 void UpdateAndBind();
70 /// Picks the active shadow-casting light and refreshes its shadow-space matrices.
71 void UpdateShadowData();
72 /// Refreshes the deferred-pass constant buffer (fog, ambient) independent of the per-light data.
73 void UpdateDeferredCB();
74
75 /// Light view-projection the next shadow pass renders with (row-major,
76 /// row vectors), or nullptr when no light casts shadows.
77 const float* GetShadowLightVP() const {
78 return m_hasShadowLightVP ? m_shadowLightVP : nullptr;
79 }
80
81 void SetAmbientColor(float r, float g, float b);
82 void SetAmbientIntensity(float intensity) {
83 m_ambientIntensity = intensity;
84 }
85
86 float GetAmbientIntensity() const {
87 return m_ambientIntensity;
88 }
89
90 size_t GetLightCount() const {
91 return m_lights.GetSize();
92 }
93
94 void SetFogColor(float r, float g, float b) {
95 m_fogR = r; m_fogG = g; m_fogB = b;
96 }
97 void SetFogDistances(float start, float end) {
98 m_fogStart = start; m_fogEnd = end;
99 }
100 void SetFogEnabled(bool enabled) { m_fogEnabled = enabled; }
101 bool IsFogEnabled() const { return m_fogEnabled; }
102 float GetFogStart() const { return m_fogStart; }
103 float GetFogEnd() const { return m_fogEnd; }
104
105 /// Two-color sky-matched fog gradient. SetFogColor(r,g,b) above sets
106 /// the horizon color; the zenith color blends in as the view direction
107 /// tilts upward.
108 void SetFogZenithColor(float r, float g, float b) {
109 m_fogZenithR = r; m_fogZenithG = g; m_fogZenithB = b;
110 }
111
112 /// Height fog: exponential density that thickens below HeightFogTop.
113 /// density(y) = HeightFogDensity * exp(-max(0, y - HeightFogTop) * HeightFogFalloff)
114 void SetHeightFogEnabled(bool enabled) { m_heightFogEnabled = enabled; }
115 void SetHeightFogTop(float worldY) { m_heightFogTop = worldY; }
116 void SetHeightFogDensity(float d) { m_heightFogDensity = d; }
117 void SetHeightFogFalloff(float f) { m_heightFogFalloff = f; }
118 bool IsHeightFogEnabled() const { return m_heightFogEnabled; }
119 float GetHeightFogTop() const { return m_heightFogTop; }
120 float GetHeightFogDensity() const { return m_heightFogDensity; }
121 float GetHeightFogFalloff() const { return m_heightFogFalloff; }
122
123 private:
124 List<Light*> m_lights;
125
127
128 float m_ambientR = 0.1f;
129 float m_ambientG = 0.1f;
130 float m_ambientB = 0.1f;
131 float m_ambientIntensity = 1.0f;
132
133 // Last light VP staged on the renderer
134 float m_shadowLightVP[16] = {};
135 bool m_hasShadowLightVP = false;
136
137 // Fog
138 bool m_fogEnabled = true;
139 float m_fogR = 0.62f, m_fogG = 0.74f, m_fogB = 0.88f; // horizon (warm-cool sky tint)
140 float m_fogZenithR = 0.42f, m_fogZenithG = 0.58f, m_fogZenithB = 0.86f; // zenith
141 float m_fogStart = 80.0f;
142 float m_fogEnd = 256.0f;
143
144 // Height fog (denser in valleys / over water)
145 bool m_heightFogEnabled = true;
146 float m_heightFogTop = 80.0f;
147 float m_heightFogDensity = 0.55f;
148 float m_heightFogFalloff = 0.04f;
149
150 // Previous frame's lightVP — sent to lighting UBO so it matches what
151 // is actually in the shadow map (which was rendered this frame using
152 // the renderer's m_lightVP, set at the END of the previous frame).
153 // Without this lag, the lighting pass samples shadow texels using a
154 // different transform than what produced them → static-geometry shake
155 // when the camera moves.
156 float m_prevLightVP[16] = {
157 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1
158 };
159 bool m_hasPrevLightVP = false;
160 };
161
162} // namespace Sleak
163
164#endif // _LIGHT_MANAGER_HPP_
float GetFogStart() const
void SetFogColor(float r, float g, float b)
size_t GetLightCount() const
float GetFogEnd() const
void SetFogZenithColor(float r, float g, float b)
void SetAmbientIntensity(float intensity)
void SetHeightFogEnabled(bool enabled)
void SetHeightFogDensity(float d)
void SetHeightFogTop(float worldY)
float GetHeightFogTop() const
float GetHeightFogDensity() const
bool IsFogEnabled() const
void RegisterLight(Light *light)
void UpdateDeferredCB()
Refreshes the deferred-pass constant buffer (fog, ambient) independent of the per-light data.
void SetFogEnabled(bool enabled)
void SetFogDistances(float start, float end)
float GetHeightFogFalloff() const
void Initialize()
Allocates the GPU light buffer; call once before the first UpdateAndBind.
void UpdateShadowData()
Picks the active shadow-casting light and refreshes its shadow-space matrices.
float GetAmbientIntensity() const
void SetHeightFogFalloff(float f)
void UpdateAndBind()
Packs every registered light and the fog/ambient parameters into the light buffer and binds it.
const float * GetShadowLightVP() const
void UnregisterLight(Light *light)
bool IsHeightFogEnabled() const
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:21
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Full per-frame lighting UBO: camera, ambient, fog, and the packed light array.