Merge 'remotes/trunk'
[0ad.git] / source / renderer / ShadowMap.h
blobe6500070991c98ec022e6a760c7b223c1ce1f864
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_SHADOWMAP
19 #define INCLUDED_SHADOWMAP
21 #include "renderer/backend/IDeviceCommandContext.h"
22 #include "renderer/backend/IShaderProgram.h"
24 class CBoundingBoxAligned;
25 class CCamera;
26 class CFrustum;
27 class CVector3D;
29 struct ShadowMapInternals;
31 /**
32 * Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,
33 * including matrix calculations.
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.
38 class ShadowMap
40 public:
41 ShadowMap(Renderer::Backend::IDevice* device);
42 ~ShadowMap();
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.
49 void RecreateTexture();
51 /**
52 * GetDepthTextureBits: Return the number of bits to use for depth textures when
53 * enabled.
55 * @return depth texture bit depth
57 int GetDepthTextureBits() const;
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)
63 * @param bits number of bits
65 void SetDepthTextureBits(int bits);
67 /**
68 * SetupFrame: Configure light space for the given camera and light direction,
69 * create the shadow texture if necessary, etc.
71 * @param camera the camera that will be used for world rendering
72 * @param lightdir the direction of the (directional) sunlight
74 void SetupFrame(const CCamera& camera, const CVector3D& lightdir);
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.
80 * @param bounds world space bounding box
82 void AddShadowCasterBound(const int cascade, const CBoundingBoxAligned& bounds);
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.
88 * @param bounds world space bounding box
90 void AddShadowReceiverBound(const CBoundingBoxAligned& bounds);
92 /**
93 * Compute the frustum originating at the light source, that encompasses
94 * all the objects passed into AddShadowReceiverBound so far.
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.
100 CFrustum GetShadowCasterCullFrustum(const int cascade);
103 * Sets backend state for rendering into the shadow map texture.
105 void BeginRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
108 * Finishes rendering into the shadow map.
110 void EndRender(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
113 * Returns the current number of used cascades.
115 int GetCascadeCount() const;
118 * Sets the renderer camera for the cascade.
120 void PrepareCamera(
121 Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const int cascade);
124 * Binds all needed resources and uniforms to draw shadows using the shader.
126 void BindTo(
127 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
128 Renderer::Backend::IShaderProgram* shader) const;
131 * Visualize shadow mapping calculations to help in
132 * debugging and optimal shadow map usage.
134 void RenderDebugBounds();
136 private:
137 ShadowMapInternals* m;
140 #endif // INCLUDED_SHADOWMAP