VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
light.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 10.09.2025.
5//
6#pragma once
7
8#include <array>
9
10#include "camera.h"
11#include "constants.h"
12#include "core/math/vector4.h"
14#include "renderer/shadowMap.h"
15
16namespace visutwin::canvas
17{
18 class Light;
19
25 {
26 public:
28
30
31 // Camera used to cull/render the shadow map
33
35
36 int face;
37
38 // Viewport for the shadow rendering to the texture (x, y, width, height)
40
41 // Scissor rectangle for the shadow rendering to the texture
43 };
44
53 class Light
54 {
55 public:
56 Light(GraphicsDevice* graphicsDevice, bool clusteredLighting);
57
58 bool atlasViewportAllocated() const { return _atlasViewportAllocated; }
59
60 bool atlasSlotUpdated() const { return _atlasSlotUpdated; }
61
62 bool enabled() const { return _enabled; }
63 void setEnabled(const bool value) { _enabled = value; }
64
65 Texture* cookie() const { return _cookie; }
66
67 bool visibleThisFrame() const { return _visibleThisFrame; }
68 void setVisibleThisFrame(const bool value) { _visibleThisFrame = value; }
69
70 LightType type() const { return _type; }
71 void setType(const LightType value) { _type = value; }
72
73 bool castShadows() const;
74 void setCastShadows(const bool value) { _castShadows = value; }
75
76 MaskType mask() const { return _mask; }
77 void setMask(const MaskType value) { _mask = value; }
78
79 ShadowUpdateType shadowUpdateMode() const { return _shadowUpdateMode; }
80 void setShadowUpdateMode(const ShadowUpdateType mode) { _shadowUpdateMode = mode; }
81
82 int numShadowFaces() const;
83
84 int numCascades() const;
85 void setNumCascades(int value);
86
87 float cascadeDistribution() const { return _cascadeDistribution; }
88 void setCascadeDistribution(const float value) { _cascadeDistribution = value; }
89
90 float cascadeBlend() const { return _cascadeBlend; }
91 void setCascadeBlend(const float value) { _cascadeBlend = value; }
92
93 const std::array<Vector4, 4>& cascadeViewports() const { return _cascadeViewports; }
94 const std::array<float, 64>& shadowMatrixPalette() const { return _shadowMatrixPalette; }
95 float* shadowMatrixPaletteData() { return _shadowMatrixPalette.data(); }
96 const std::array<float, 4>& shadowCascadeDistances() const { return _shadowCascadeDistances; }
97 float* shadowCascadeDistancesData() { return _shadowCascadeDistances.data(); }
98
99 ShadowMap* shadowMap() const { return _shadowMap; }
100 void setShadowMap(ShadowMap* value) { _shadowMap = value; }
101
102 LightRenderData* getRenderData(Camera* camera, int face);
103
104 ShadowType shadowType() const { return _shadowType; }
105 void setShadowType(const ShadowType value) { _shadowType = value; }
106
107 GraphNode* node() const { return _node; }
108 void setNode(GraphNode* value) { _node = value; }
109
110 float shadowDistance() const { return _shadowDistance; }
111 void setShadowDistance(const float value) { _shadowDistance = value; }
112
113 int shadowResolution() const { return _shadowResolution; }
114 void setShadowResolution(const int value) { _shadowResolution = value; }
115
116 //shadowBias.
117 float shadowBias() const { return _shadowBias; }
118 void setShadowBias(const float value) { _shadowBias = value; }
119
120 //_normalOffsetBias.
121 float normalBias() const { return _normalBias; }
122 void setNormalBias(const float value) { _normalBias = value; }
123
124 //shadowIntensity (1 = full shadow, 0 = no shadow effect).
125 float shadowIntensity() const { return _shadowIntensity; }
126 void setShadowIntensity(const float value) { _shadowIntensity = value; }
127
128 // Per-light shadow VP matrix for local lights. Set during shadow camera positioning.
129 const Matrix4& shadowViewProjection() const { return _shadowViewProjection; }
130 void setShadowViewProjection(const Matrix4& value) { _shadowViewProjection = value; }
131
132 // Range and cone angle — synced from LightComponent for shadow camera setup.
133 float range() const { return _range; }
134 void setRange(const float value) { _range = value; }
135
136 float outerConeAngle() const { return _outerConeAngle; }
137 void setOuterConeAngle(const float value) { _outerConeAngle = value; }
138
139 GraphicsDevice* device() const { return _device; }
140
141 private:
142 GraphicsDevice* _device;
143
144 bool _clusteredLighting;
145
146 bool _atlasViewportAllocated = false;
147
148 bool _atlasSlotUpdated = false;
149
150 bool _enabled = false;
151
152 Texture* _cookie = nullptr;
153
154 bool _visibleThisFrame = false;
155
157
158 bool _castShadows = false;
159
161
163
164 //numCascades, cascadeDistribution, _cascadeBlend.
165 int _numCascades = 4;
166 float _cascadeDistribution = 0.5f; // 0=linear splits, 1=logarithmic, 0.5=practical blend
167 float _cascadeBlend = 0.0f; // 0=no blend, >0=dither transition width at cascade edges
168
169 // Viewport rects per cascade (normalized 0..1 within shadow texture).
170 // Layout matches upstream directionalCascades:
171 // 1 cascade: full texture
172 // 2 cascades: 2×1 vertical strip
173 // 4 cascades: 2×2 grid
174 std::array<Vector4, 4> _cascadeViewports = {{ Vector4(0,0,0.5f,0.5f), Vector4(0,0.5f,0.5f,0.5f), Vector4(0.5f,0,0.5f,0.5f), Vector4(0.5f,0.5f,0.5f,0.5f) }};
175
176 // Per-cascade VP matrices (viewport-scaled). 4 matrices × 16 floats.
177 //_shadowMatrixPalette.
178 std::array<float, 64> _shadowMatrixPalette = {};
179
180 // Per-cascade split distances. distances[i] = far distance of cascade i.
181 //_shadowCascadeDistances.
182 std::array<float, 4> _shadowCascadeDistances = {};
183
184 ShadowMap* _shadowMap = nullptr;
185
186 std::vector<LightRenderData*> _renderData;
187
188 ShadowType _shadowType = SHADOW_PCF3_32F;
189
190 GraphNode* _node = nullptr;
191
192 float _shadowDistance = 40.0f;
193
194 int _shadowResolution = 2048;
195
196 //_shadowBias (-0.0005 default in the upstream engine).
197 float _shadowBias = -0.0005f;
198
199 //_normalOffsetBias.
200 float _normalBias = 0.0f;
201
202 //shadowIntensity.
203 float _shadowIntensity = 1.0f;
204
205 // Computed shadow VP matrix for local lights (set during shadow camera positioning).
206 Matrix4 _shadowViewProjection = Matrix4::identity();
207
208 // Range and cone angle — synced from LightComponent for shadow camera setup.
209 float _range = 10.0f;
210 float _outerConeAngle = 45.0f;
211 };
212}
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
Hierarchical scene graph node with local/world transforms and parent-child relationships.
Definition graphNode.h:28
Abstract GPU interface for resource creation, state management, and draw submission.
Directional, point, spot, or area light with shadow mapping and cookie projection.
Definition light.h:54
float cascadeBlend() const
Definition light.h:90
bool atlasViewportAllocated() const
Definition light.h:58
const Matrix4 & shadowViewProjection() const
Definition light.h:129
void setShadowBias(const float value)
Definition light.h:118
void setNumCascades(int value)
Definition light.cpp:48
float shadowIntensity() const
Definition light.h:125
int shadowResolution() const
Definition light.h:113
float * shadowCascadeDistancesData()
Definition light.h:97
float shadowDistance() const
Definition light.h:110
float * shadowMatrixPaletteData()
Definition light.h:95
void setCascadeDistribution(const float value)
Definition light.h:88
void setNode(GraphNode *value)
Definition light.h:108
const std::array< float, 4 > & shadowCascadeDistances() const
Definition light.h:96
float outerConeAngle() const
Definition light.h:136
void setShadowDistance(const float value)
Definition light.h:111
ShadowUpdateType shadowUpdateMode() const
Definition light.h:79
int numCascades() const
Definition light.cpp:42
GraphicsDevice * device() const
Definition light.h:139
float shadowBias() const
Definition light.h:117
bool castShadows() const
Definition light.cpp:26
bool atlasSlotUpdated() const
Definition light.h:60
ShadowType shadowType() const
Definition light.h:104
void setEnabled(const bool value)
Definition light.h:63
void setCastShadows(const bool value)
Definition light.h:74
void setShadowIntensity(const float value)
Definition light.h:126
float cascadeDistribution() const
Definition light.h:87
void setNormalBias(const float value)
Definition light.h:122
bool visibleThisFrame() const
Definition light.h:67
void setShadowResolution(const int value)
Definition light.h:114
float range() const
Definition light.h:133
bool enabled() const
Definition light.h:62
int numShadowFaces() const
Definition light.cpp:31
ShadowMap * shadowMap() const
Definition light.h:99
float normalBias() const
Definition light.h:121
void setShadowViewProjection(const Matrix4 &value)
Definition light.h:130
LightRenderData * getRenderData(Camera *camera, int face)
Definition light.cpp:77
void setRange(const float value)
Definition light.h:134
const std::array< Vector4, 4 > & cascadeViewports() const
Definition light.h:93
LightType type() const
Definition light.h:70
MaskType mask() const
Definition light.h:76
void setShadowMap(ShadowMap *value)
Definition light.h:100
void setCascadeBlend(const float value)
Definition light.h:91
void setVisibleThisFrame(const bool value)
Definition light.h:68
void setOuterConeAngle(const float value)
Definition light.h:137
void setMask(const MaskType value)
Definition light.h:77
void setShadowType(const ShadowType value)
Definition light.h:105
const std::array< float, 64 > & shadowMatrixPalette() const
Definition light.h:94
GraphNode * node() const
Definition light.h:107
void setShadowUpdateMode(const ShadowUpdateType mode)
Definition light.h:80
Texture * cookie() const
Definition light.h:65
Light(GraphicsDevice *graphicsDevice, bool clusteredLighting)
Definition light.cpp:20
void setType(const LightType value)
Definition light.h:71
Per-face shadow rendering data: shadow camera, viewport, and scissor.
Definition light.h:25
LightRenderData(Camera *camera, int face, Light *light)
Definition light.cpp:15
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
static Matrix4 identity()
Definition matrix4.h:108
4D vector for homogeneous coordinates, color values, and SIMD operations.
Definition vector4.h:20