VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
shadowRenderer.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 Lektayers on 11.09.2025.
5//
6#include "shadowRenderer.h"
7
8#include "lightCamera.h"
9#include "renderer.h"
10
11namespace visutwin::canvas
12{
14 bool needs = light->enabled() && light->castShadows()
16
19 }
20
21 if (needs) {
22 _renderer->_shadowMapUpdates += light->numShadowFaces();
23 }
24
25 return needs;
26 }
27
28 Camera* ShadowRenderer::prepareFace(Light* light, Camera* camera, int face) {
29 LightType type = light->type();
30 LightRenderData* lightRenderData = getLightRenderData(light, camera, face);
31 Camera* shadowCam = lightRenderData->shadowCamera;
32
33 // Assign a render target for the face
34 int renderTargetIndex = type == LightType::LIGHTTYPE_DIRECTIONAL ? 0 : face;
35 shadowCam->setRenderTarget(light->shadowMap()->renderTargets()[renderTargetIndex]);
36
37 return shadowCam;
38 }
39
40 void ShadowRenderer::setupRenderPass(RenderPass* renderPass, Camera* shadowCamera, bool clearRenderTarget)
41 {
42 std::shared_ptr<RenderTarget> rt = shadowCamera->renderTarget();
43 renderPass->init(rt);
44
45 renderPass->depthStencilOps()->clearDepthValue = 1.0f;
46 renderPass->depthStencilOps()->clearDepth = clearRenderTarget;
47
48 // If rendering to depth buffer
49 if (rt->depthBuffer()) {
50 renderPass->depthStencilOps()->storeDepth = true;
51 } else {
52 // Rendering to color buffer
53 renderPass->colorOps()->clearValue = shadowCamera->clearColor();
54 renderPass->colorOps()->clear = clearRenderTarget;
55 renderPass->depthStencilOps()->storeDepth = false;
56 }
57
58 // Not sampling dynamically generated cubemaps
59 renderPass->setRequiresCubemaps(false);
60 }
61
63 {
64 return light->getRenderData(light->type() == LightType::LIGHTTYPE_DIRECTIONAL ? camera : nullptr, face);
65 }
66
68 {
69 Camera* shadowCam = LightCamera::create("ShadowCamera", type, face);
70
71 const ShadowTypeInfo* shadowInfo = &shadowTypeInfo.at(shadowType);
72 assert(shadowInfo != nullptr);
73 const bool isVsm = shadowInfo ? shadowInfo->vsm : false;
74 const bool isPcf = shadowInfo ? shadowInfo->pcf : false;
75
76 // Don't clear the color buffer if rendering a depth map
77 if (isVsm) {
78 shadowCam->setClearColor(Color(0, 0, 0, 0));
79 } else {
80 shadowCam->setClearColor(Color(1, 1, 1, 1));
81 }
82
83 shadowCam->setClearDepthBuffer(true);
84 shadowCam->setClearStencilBuffer(false);
85
86 // Clear color buffer only when using it
87 shadowCam->setClearColorBuffer(!isPcf);
88
89 return shadowCam;
90 }
91}
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
void setRenderTarget(const std::shared_ptr< RenderTarget > &value)
Definition camera.h:87
void setClearColorBuffer(bool value)
Definition camera.h:111
void setClearStencilBuffer(bool value)
Definition camera.h:109
const std::shared_ptr< RenderTarget > & renderTarget() const
Definition camera.h:86
void setClearColor(const Color &value)
Definition camera.h:90
const Color & clearColor() const
Definition camera.h:89
void setClearDepthBuffer(bool value)
Definition camera.h:107
static Camera * create(const std::string &name, LightType lightType, int face=0)
Directional, point, spot, or area light with shadow mapping and cookie projection.
Definition light.h:54
ShadowUpdateType shadowUpdateMode() const
Definition light.h:79
bool castShadows() const
Definition light.cpp:26
bool visibleThisFrame() const
Definition light.h:67
bool enabled() const
Definition light.h:62
int numShadowFaces() const
Definition light.cpp:31
ShadowMap * shadowMap() const
Definition light.h:99
LightRenderData * getRenderData(Camera *camera, int face)
Definition light.cpp:77
LightType type() const
Definition light.h:70
void setShadowUpdateMode(const ShadowUpdateType mode)
Definition light.h:80
Per-face shadow rendering data: shadow camera, viewport, and scissor.
Definition light.h:25
virtual void init(const std::shared_ptr< RenderTarget > &renderTarget=nullptr, const std::shared_ptr< RenderPassOptions > &options=nullptr)
void setRequiresCubemaps(bool value)
Definition renderPass.h:108
std::shared_ptr< DepthStencilAttachmentOps > depthStencilOps() const
Definition renderPass.h:102
std::shared_ptr< ColorAttachmentOps > colorOps() const
const std::vector< std::shared_ptr< RenderTarget > > & renderTargets() const
Definition shadowMap.h:20
void setupRenderPass(RenderPass *renderPass, Camera *shadowCamera, bool clearRenderTarget)
static Camera * createShadowCamera(ShadowType shadowType, LightType type, int face)
LightRenderData * getLightRenderData(Light *light, Camera *camera, int face)
bool needsShadowRendering(Light *light)
Camera * prepareFace(Light *light, Camera *camera, int face)
const std::unordered_map< ShadowType, ShadowTypeInfo > shadowTypeInfo
Definition constants.cpp:10
RGBA color with floating-point components in [0, 1].
Definition color.h:18