Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ShadowMap.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SHADOWMAP
19#define INCLUDED_SHADOWMAP
20
22class CCamera;
23class CFrustum;
24class CVector3D;
25namespace Renderer::Backend { class IDevice; }
26namespace Renderer::Backend { class IDeviceCommandContext; }
27namespace Renderer::Backend { class IShaderProgram; }
29
30/**
31 * Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,
32 * including matrix calculations.
33 *
34 * The class will automatically generate a texture the first time the shadow map is rendered into.
35 * The texture will not be resized afterwards.
36 */
38{
39public:
41 ~ShadowMap();
42
43 /**
44 * RecreateTexture: Destroy the current shadow texture and force creation of
45 * a new one. Useful when shadows settings have changed and the texture
46 * should be resized/updated too.
47 */
48 void RecreateTexture();
49
50 void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height);
52
53 /**
54 * SetupFrame: Configure light space for the given camera and light direction,
55 * create the shadow texture if necessary, etc.
56 *
57 * @param camera the camera that will be used for world rendering
58 * @param lightdir the direction of the (directional) sunlight
59 */
60 void SetupFrame(const CCamera& camera, const CVector3D& lightdir);
61
62 /**
63 * Add the bounding box of an object that will cast a shadow.
64 * This is used to calculate the bounds for the shadow map.
65 *
66 * @param bounds world space bounding box
67 */
68 void AddShadowCasterBound(const int cascade, const CBoundingBoxAligned& bounds);
69
70 /**
71 * Add the bounding box of an object that will receive a shadow.
72 * This is used to calculate the bounds for the shadow map.
73 *
74 * @param bounds world space bounding box
75 */
77
78 /**
79 * Compute the frustum originating at the light source, that encompasses
80 * all the objects passed into AddShadowReceiverBound so far.
81 *
82 * This frustum can be used to determine which objects might cast a visible
83 * shadow. Those objects should be passed to AddShadowCasterBound and
84 * then should be rendered into the shadow map.
85 */
86 CFrustum GetShadowCasterCullFrustum(const int cascade);
87
88 /**
89 * Sets backend state for rendering into the shadow map texture.
90 */
91 void BeginRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
92
93 /**
94 * Finishes rendering into the shadow map.
95 */
96 void EndRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
97
98 /**
99 * Returns the current number of used cascades.
100 */
101 int GetCascadeCount() const;
102
103 /**
104 * Sets the renderer camera for the cascade.
105 */
106 void PrepareCamera(
107 Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const int cascade);
108
109 /**
110 * Binds all needed resources and uniforms to draw shadows using the shader.
111 */
112 void BindTo(
113 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
115
116 /**
117 * Visualize shadow mapping calculations to help in
118 * debugging and optimal shadow map usage.
119 */
121
122private:
124};
125
126#endif // INCLUDED_SHADOWMAP
Definition: BoundingBoxAligned.h:35
Definition: Camera.h:46
Definition: Frustum.h:40
Definition: Vector3D.h:32
Definition: IDeviceCommandContext.h:46
Definition: IDevice.h:64
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:85
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,...
Definition: ShadowMap.h:38
void EndRender(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Finishes rendering into the shadow map.
Definition: ShadowMap.cpp:656
void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height)
Definition: ShadowMap.cpp:230
ShadowMap(Renderer::Backend::IDevice *device)
Definition: ShadowMap.cpp:198
void SetupFrame(const CCamera &camera, const CVector3D &lightdir)
SetupFrame: Configure light space for the given camera and light direction, create the shadow texture...
Definition: ShadowMap.cpp:251
void AddShadowReceiverBound(const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will receive a shadow.
Definition: ShadowMap.cpp:353
void AddShadowCasterBound(const int cascade, const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will cast a shadow.
Definition: ShadowMap.cpp:345
void DestroyViewSizeDependentObjects()
Definition: ShadowMap.cpp:237
CFrustum GetShadowCasterCullFrustum(const int cascade)
Compute the frustum originating at the light source, that encompasses all the objects passed into Add...
Definition: ShadowMap.cpp:361
~ShadowMap()
Definition: ShadowMap.cpp:221
void BeginRender(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Sets backend state for rendering into the shadow map texture.
Definition: ShadowMap.cpp:622
void RenderDebugBounds(Renderer::Backend::IDeviceCommandContext &deviceCommandContext)
Visualize shadow mapping calculations to help in debugging and optimal shadow map usage.
Definition: ShadowMap.cpp:710
void BindTo(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IShaderProgram *shader) const
Binds all needed resources and uniforms to draw shadows using the shader.
Definition: ShadowMap.cpp:666
ShadowMapInternals * m
Definition: ShadowMap.h:123
void PrepareCamera(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const int cascade)
Sets the renderer camera for the cascade.
Definition: ShadowMap.cpp:631
int GetCascadeCount() const
Returns the current number of used cascades.
Definition: ShadowMap.cpp:745
void RecreateTexture()
RecreateTexture: Destroy the current shadow texture and force creation of a new one.
Definition: ShadowMap.cpp:244
Definition: Canvas2D.h:30
Struct ShadowMapInternals: Internal data for the ShadowMap implementation.
Definition: ShadowMap.cpp:73