Merge 'remotes/trunk'
[0ad.git] / source / renderer / ShadowMap.h
blob77fe33dcbdc923099d18b26c817c20baf9967f92
1 /* Copyright (C) 2012 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/>.
19 * Shadow mapping related texture and matrix management
22 #ifndef INCLUDED_SHADOWMAP
23 #define INCLUDED_SHADOWMAP
25 #include "lib/ogl.h"
27 class CBoundingBoxAligned;
28 class CMatrix3D;
30 struct ShadowMapInternals;
32 /**
33 * Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,
34 * including matrix calculations.
36 * The class will automatically generate a texture the first time the shadow map is rendered into.
37 * The texture will not be resized afterwards.
39 class ShadowMap
41 public:
42 ShadowMap();
43 ~ShadowMap();
45 /**
46 * RecreateTexture: Destroy the current shadow texture and force creation of
47 * a new one. Useful when the renderer's size has changed and the texture
48 * should be resized too.
50 void RecreateTexture();
52 /**
53 * GetDepthTextureBits: Return the number of bits to use for depth textures when
54 * enabled.
56 * @return depth texture bit depth
58 int GetDepthTextureBits() const;
60 /**
61 * SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
62 * Possible values are 16, 24, 32 and 0 (= use default)
64 * @param bits number of bits
66 void SetDepthTextureBits(int bits);
68 /**
69 * GetWidth: Return the width of the depth texture.
71 * @return depth texture width
73 int GetWidth() const;
75 /**
76 * GetHeight: Return the height of the depth texture
78 * @return depth texture height
80 int GetHeight() const;
82 /**
83 * SetupFrame: Configure light space for the given camera and light direction,
84 * create the shadow texture if necessary, etc.
86 * @param camera the camera that will be used for world rendering
87 * @param lightdir the direction of the (directional) sunlight
89 void SetupFrame(const CCamera& camera, const CVector3D& lightdir);
91 /**
92 * Add the bounding box of an object that will cast a shadow.
93 * This is used to calculate the bounds for the shadow map.
95 * @param bounds world space bounding box
97 void AddShadowCasterBound(const CBoundingBoxAligned& bounds);
99 /**
100 * Add the bounding box of an object that will receive a shadow.
101 * This is used to calculate the bounds for the shadow map.
103 * @param bounds world space bounding box
105 void AddShadowReceiverBound(const CBoundingBoxAligned& bounds);
108 * Compute the frustum originating at the light source, that encompasses
109 * all the objects passed into AddShadowReceiverBound so far.
111 * This frustum can be used to determine which objects might cast a visible
112 * shadow. Those objects should be passed to AddShadowCasterBound and
113 * then should be rendered into the shadow map.
115 CFrustum GetShadowCasterCullFrustum();
118 * BeginRender: Set OpenGL state for rendering into the shadow map texture.
120 * @todo this depends in non-obvious ways on the behaviour of the call-site
122 void BeginRender();
125 * EndRender: Finish rendering into the shadow map.
127 * @todo this depends in non-obvious ways on the behaviour of the call-site
129 void EndRender();
132 * GetTexture: Retrieve the OpenGL texture object name that contains the shadow map.
134 * @return the texture name of the shadow map texture
136 GLuint GetTexture() const;
139 * GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates
140 * transformation matrix.
142 * @return the matrix that transforms world-space coordinates into homogenous
143 * shadow map texture coordinates
145 const CMatrix3D& GetTextureMatrix() const;
148 * Visualize shadow mapping calculations to help in
149 * debugging and optimal shadow map usage.
151 void RenderDebugBounds();
154 * Visualize shadow map texture to help in debugging.
156 void RenderDebugTexture();
158 private:
159 ShadowMapInternals* m;
162 #endif // INCLUDED_SHADOWMAP