VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
light.cpp
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 01.10.2025.
5//
6
7#include "light.h"
8
9#include <algorithm>
10
12
13namespace visutwin::canvas
14{
19
20 Light::Light(GraphicsDevice* graphicsDevice, bool clusteredLighting)
21 : _device(graphicsDevice), _clusteredLighting(clusteredLighting)
22 {
23
24 }
25
26 bool Light::castShadows() const
27 {
28 return _castShadows && _mask != MaskType::MASK_BAKE && _mask != MaskType::MASK_NONE;
29 }
30
32 {
34 return numCascades();
35 }
36 if (_type == LightType::LIGHTTYPE_OMNI) {
37 return 6;
38 }
39 return 1;
40 }
41
43 {
44 return _numCascades;
45 }
46
47 //numCascades setter (lines 370-395).
48 void Light::setNumCascades(int value)
49 {
50 value = std::clamp(value, 1, 4);
51 if (_numCascades == value) {
52 return;
53 }
54 _numCascades = value;
55
56 // Directional cascades layout:
57 // 1 cascade: full texture [(0,0,1,1)]
58 // 2 cascades: 2×1 vertical strip [(0,0,0.5,0.5), (0,0.5,0.5,0.5)]
59 // 3 cascades: 3 of 4 quadrants
60 // 4 cascades: 2×2 grid
61 static const std::array<std::array<Vector4, 4>, 4> layouts = {{
62 {{ Vector4(0,0,1,1), Vector4(0,0,0,0), Vector4(0,0,0,0), Vector4(0,0,0,0) }},
63 {{ Vector4(0,0,0.5f,0.5f), Vector4(0,0.5f,0.5f,0.5f), Vector4(0,0,0,0), Vector4(0,0,0,0) }},
64 {{ 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,0,0,0) }},
65 {{ 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) }}
66 }};
67 _cascadeViewports = layouts[value - 1];
68
69 // Reset palette and distances
70 _shadowMatrixPalette.fill(0.0f);
71 _shadowCascadeDistances.fill(0.0f);
72
73 // Destroy existing shadow map to force re-allocation with correct cascade count
74 _shadowMap = nullptr;
75 }
76
78 {
79 // Return existing
80 for (auto* rd : _renderData) {
81 if (rd->camera == camera && rd->face == face) {
82 return rd;
83 }
84 }
85
86 // Create new one
87 LightRenderData* rd = new LightRenderData(camera, face, this);
88 _renderData.push_back(rd);
89 return rd;
90 }
91}
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
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
void setNumCascades(int value)
Definition light.cpp:48
int numCascades() const
Definition light.cpp:42
bool castShadows() const
Definition light.cpp:26
int numShadowFaces() const
Definition light.cpp:31
LightRenderData * getRenderData(Camera *camera, int face)
Definition light.cpp:77
Light(GraphicsDevice *graphicsDevice, bool clusteredLighting)
Definition light.cpp:20
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
static Camera * createShadowCamera(ShadowType shadowType, LightType type, int face)
4D vector for homogeneous coordinates, color values, and SIMD operations.
Definition vector4.h:20