Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ShadowMap.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /*
19  * Shadow mapping related texture and matrix management
20  */
21 
22 #ifndef INCLUDED_SHADOWMAP
23 #define INCLUDED_SHADOWMAP
24 
25 #include "lib/ogl.h"
26 
28 class CMatrix3D;
29 
30 struct ShadowMapInternals;
31 
32 /**
33  * Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,
34  * including matrix calculations.
35  *
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.
38  */
39 class ShadowMap
40 {
41 public:
42  ShadowMap();
43  ~ShadowMap();
44 
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.
49  */
50  void RecreateTexture();
51 
52  /**
53  * GetDepthTextureBits: Return the number of bits to use for depth textures when
54  * enabled.
55  *
56  * @return depth texture bit depth
57  */
58  int GetDepthTextureBits() const;
59 
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)
63  *
64  * @param bits number of bits
65  */
66  void SetDepthTextureBits(int bits);
67 
68  /**
69  * GetWidth: Return the width of the depth texture.
70  *
71  * @return depth texture width
72  */
73  int GetWidth() const;
74 
75  /**
76  * GetHeight: Return the height of the depth texture
77  *
78  * @return depth texture height
79  */
80  int GetHeight() const;
81 
82  /**
83  * SetupFrame: Configure light space for the given camera and light direction,
84  * create the shadow texture if necessary, etc.
85  *
86  * @param camera the camera that will be used for world rendering
87  * @param lightdir the direction of the (directional) sunlight
88  */
89  void SetupFrame(const CCamera& camera, const CVector3D& lightdir);
90 
91  /**
92  * AddShadowedBound: Add the bounding box of an object that has to be shadowed.
93  * This is used to calculate the bounds for the shadow map.
94  *
95  * @param bounds world space bounding box
96  */
97  void AddShadowedBound(const CBoundingBoxAligned& bounds);
98 
99  /**
100  * BeginRender: Set OpenGL state for rendering into the shadow map texture.
101  *
102  * @todo this depends in non-obvious ways on the behaviour of the call-site
103  */
104  void BeginRender();
105 
106  /**
107  * EndRender: Finish rendering into the shadow map.
108  *
109  * @todo this depends in non-obvious ways on the behaviour of the call-site
110  */
111  void EndRender();
112 
113  /**
114  * GetTexture: Retrieve the OpenGL texture object name that contains the shadow map.
115  *
116  * @return the texture name of the shadow map texture
117  */
118  GLuint GetTexture() const;
119 
120  /**
121  * GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates
122  * transformation matrix.
123  *
124  * @return the matrix that transforms world-space coordinates into homogenous
125  * shadow map texture coordinates
126  */
127  const CMatrix3D& GetTextureMatrix() const;
128 
129  /**
130  * Visualize shadow mapping calculations to help in
131  * debugging and optimal shadow map usage.
132  */
133  void RenderDebugBounds();
134 
135  /**
136  * Visualize shadow map texture to help in debugging.
137  */
138  void RenderDebugTexture();
139 
140 private:
142 };
143 
144 #endif // INCLUDED_SHADOWMAP
void SetDepthTextureBits(int bits)
SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:542
int GetDepthTextureBits() const
GetDepthTextureBits: Return the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:537
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:153
void AddShadowedBound(const CBoundingBoxAligned &bounds)
AddShadowedBound: Add the bounding box of an object that has to be shadowed.
Definition: ShadowMap.cpp:213
Struct ShadowMapInternals: Internal data for the ShadowMap implementation.
Definition: ShadowMap.cpp:47
GLuint GetTexture() const
GetTexture: Retrieve the OpenGL texture object name that contains the shadow map. ...
Definition: ShadowMap.cpp:524
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup, including matrix calculations.
Definition: ShadowMap.h:39
int GetWidth() const
GetWidth: Return the width of the depth texture.
Definition: ShadowMap.cpp:559
void RenderDebugTexture()
Visualize shadow map texture to help in debugging.
Definition: ShadowMap.cpp:648
Definition: Camera.h:39
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 BeginRender()
BeginRender: Set OpenGL state for rendering into the shadow map texture.
Definition: ShadowMap.cpp:455
const CMatrix3D & GetTextureMatrix() const
GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates transformation matrix...
Definition: ShadowMap.cpp:529
void RenderDebugBounds()
Visualize shadow mapping calculations to help in debugging and optimal shadow map usage...
Definition: ShadowMap.cpp:571
void RecreateTexture()
RecreateTexture: Destroy the current shadow texture and force creation of a new one.
Definition: ShadowMap.cpp:135
ShadowMapInternals * m
Definition: ShadowMap.h:141
void EndRender()
EndRender: Finish rendering into the shadow map.
Definition: ShadowMap.cpp:505
int GetHeight() const
GetHeight: Return the height of the depth texture.
Definition: ShadowMap.cpp:564