Pyrogenesis  trunk
ShadowMap.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 
23 
25 class CCamera;
26 class CFrustum;
27 class CVector3D;
28 
29 struct ShadowMapInternals;
30 
31 /**
32  * Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,
33  * including matrix calculations.
34  *
35  * The class will automatically generate a texture the first time the shadow map is rendered into.
36  * The texture will not be resized afterwards.
37  */
38 class ShadowMap
39 {
40 public:
41  ShadowMap();
42  ~ShadowMap();
43 
44  /**
45  * RecreateTexture: Destroy the current shadow texture and force creation of
46  * a new one. Useful when the renderer's size has changed and the texture
47  * should be resized too.
48  */
49  void RecreateTexture();
50 
51  /**
52  * GetDepthTextureBits: Return the number of bits to use for depth textures when
53  * enabled.
54  *
55  * @return depth texture bit depth
56  */
57  int GetDepthTextureBits() const;
58 
59  /**
60  * SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
61  * Possible values are 16, 24, 32 and 0 (= use default)
62  *
63  * @param bits number of bits
64  */
65  void SetDepthTextureBits(int bits);
66 
67  /**
68  * SetupFrame: Configure light space for the given camera and light direction,
69  * create the shadow texture if necessary, etc.
70  *
71  * @param camera the camera that will be used for world rendering
72  * @param lightdir the direction of the (directional) sunlight
73  */
74  void SetupFrame(const CCamera& camera, const CVector3D& lightdir);
75 
76  /**
77  * Add the bounding box of an object that will cast a shadow.
78  * This is used to calculate the bounds for the shadow map.
79  *
80  * @param bounds world space bounding box
81  */
82  void AddShadowCasterBound(const int cascade, const CBoundingBoxAligned& bounds);
83 
84  /**
85  * Add the bounding box of an object that will receive a shadow.
86  * This is used to calculate the bounds for the shadow map.
87  *
88  * @param bounds world space bounding box
89  */
90  void AddShadowReceiverBound(const CBoundingBoxAligned& bounds);
91 
92  /**
93  * Compute the frustum originating at the light source, that encompasses
94  * all the objects passed into AddShadowReceiverBound so far.
95  *
96  * This frustum can be used to determine which objects might cast a visible
97  * shadow. Those objects should be passed to AddShadowCasterBound and
98  * then should be rendered into the shadow map.
99  */
100  CFrustum GetShadowCasterCullFrustum(const int cascade);
101 
102  /**
103  * Sets backend state for rendering into the shadow map texture.
104  */
105  void BeginRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
106 
107  /**
108  * Finishes rendering into the shadow map.
109  */
110  void EndRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
111 
112  /**
113  * Returns the current number of used cascades.
114  */
115  int GetCascadeCount() const;
116 
117  /**
118  * Sets the renderer camera for the cascade.
119  */
120  void PrepareCamera(
121  Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const int cascade);
122 
123  /**
124  * Binds all needed resources and uniforms to draw shadows using the shader.
125  */
126  void BindTo(
127  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
128  Renderer::Backend::IShaderProgram* shader) const;
129 
130  /**
131  * Visualize shadow mapping calculations to help in
132  * debugging and optimal shadow map usage.
133  */
134  void RenderDebugBounds();
135 
136 private:
138 };
139 
140 #endif // INCLUDED_SHADOWMAP
int GetDepthTextureBits() const
GetDepthTextureBits: Return the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:689
void SetDepthTextureBits(int bits)
SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:694
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:231
Struct ShadowMapInternals: Internal data for the ShadowMap implementation.
Definition: ShadowMap.cpp:58
Definition: Vector3D.h:30
Definition: Frustum.h:36
void AddShadowCasterBound(const int cascade, const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will cast a shadow.
Definition: ShadowMap.cpp:328
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup, including matrix calculations.
Definition: ShadowMap.h:38
void AddShadowReceiverBound(const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will receive a shadow.
Definition: ShadowMap.cpp:336
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:644
int GetCascadeCount() const
Returns the current number of used cascades.
Definition: ShadowMap.cpp:737
Definition: Camera.h:41
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:80
T bits(T num, size_t lo_idx, size_t hi_idx)
extract the value of bits hi_idx:lo_idx within num
Definition: bits.h:97
void RenderDebugBounds()
Visualize shadow mapping calculations to help in debugging and optimal shadow map usage...
Definition: ShadowMap.cpp:705
Definition: BoundingBoxAligned.h:33
~ShadowMap()
Definition: ShadowMap.cpp:208
void RecreateTexture()
RecreateTexture: Destroy the current shadow texture and force creation of a new one.
Definition: ShadowMap.cpp:219
void PrepareCamera(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const int cascade)
Sets the renderer camera for the cascade.
Definition: ShadowMap.cpp:610
void EndRender(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Finishes rendering into the shadow map.
Definition: ShadowMap.cpp:634
CFrustum GetShadowCasterCullFrustum(const int cascade)
Compute the frustum originating at the light source, that encompasses all the objects passed into Add...
Definition: ShadowMap.cpp:344
ShadowMapInternals * m
Definition: ShadowMap.h:137
Definition: IDeviceCommandContext.h:40
ShadowMap()
Definition: ShadowMap.cpp:186
void BeginRender(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Sets backend state for rendering into the shadow map texture.
Definition: ShadowMap.cpp:601