Pyrogenesis  trunk
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  * Add the bounding box of an object that will cast a shadow.
93  * This is used to calculate the bounds for the shadow map.
94  *
95  * @param bounds world space bounding box
96  */
97  void AddShadowCasterBound(const CBoundingBoxAligned& bounds);
98 
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.
102  *
103  * @param bounds world space bounding box
104  */
105  void AddShadowReceiverBound(const CBoundingBoxAligned& bounds);
106 
107  /**
108  * Compute the frustum originating at the light source, that encompasses
109  * all the objects passed into AddShadowReceiverBound so far.
110  *
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.
114  */
116 
117  /**
118  * BeginRender: Set OpenGL state for rendering into the shadow map texture.
119  *
120  * @todo this depends in non-obvious ways on the behaviour of the call-site
121  */
122  void BeginRender();
123 
124  /**
125  * EndRender: Finish rendering into the shadow map.
126  *
127  * @todo this depends in non-obvious ways on the behaviour of the call-site
128  */
129  void EndRender();
130 
131  /**
132  * GetTexture: Retrieve the OpenGL texture object name that contains the shadow map.
133  *
134  * @return the texture name of the shadow map texture
135  */
136  GLuint GetTexture() const;
137 
138  /**
139  * GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates
140  * transformation matrix.
141  *
142  * @return the matrix that transforms world-space coordinates into homogenous
143  * shadow map texture coordinates
144  */
145  const CMatrix3D& GetTextureMatrix() const;
146 
147  /**
148  * Visualize shadow mapping calculations to help in
149  * debugging and optimal shadow map usage.
150  */
151  void RenderDebugBounds();
152 
153  /**
154  * Visualize shadow map texture to help in debugging.
155  */
156  void RenderDebugTexture();
157 
158 private:
160 };
161 
162 #endif // INCLUDED_SHADOWMAP
void SetDepthTextureBits(int bits)
SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:596
int GetDepthTextureBits() const
GetDepthTextureBits: Return the number of bits to use for depth textures when enabled.
Definition: ShadowMap.cpp:591
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:157
Struct ShadowMapInternals: Internal data for the ShadowMap implementation.
Definition: ShadowMap.cpp:48
Definition: Vector3D.h:28
Definition: Frustum.h:39
GLuint GetTexture() const
GetTexture: Retrieve the OpenGL texture object name that contains the shadow map. ...
Definition: ShadowMap.cpp:578
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup, including matrix calculations.
Definition: ShadowMap.h:39
void AddShadowCasterBound(const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will cast a shadow.
Definition: ShadowMap.cpp:218
Definition: Matrix3D.h:33
void AddShadowReceiverBound(const CBoundingBoxAligned &bounds)
Add the bounding box of an object that will receive a shadow.
Definition: ShadowMap.cpp:226
int GetWidth() const
GetWidth: Return the width of the depth texture.
Definition: ShadowMap.cpp:613
void RenderDebugTexture()
Visualize shadow map texture to help in debugging.
Definition: ShadowMap.cpp:717
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:507
const CMatrix3D & GetTextureMatrix() const
GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates transformation matrix...
Definition: ShadowMap.cpp:583
void RenderDebugBounds()
Visualize shadow mapping calculations to help in debugging and optimal shadow map usage...
Definition: ShadowMap.cpp:625
Definition: BoundingBoxAligned.h:35
~ShadowMap()
Definition: ShadowMap.cpp:124
void RecreateTexture()
RecreateTexture: Destroy the current shadow texture and force creation of a new one.
Definition: ShadowMap.cpp:139
ShadowMapInternals * m
Definition: ShadowMap.h:159
void EndRender()
EndRender: Finish rendering into the shadow map.
Definition: ShadowMap.cpp:558
CFrustum GetShadowCasterCullFrustum()
Compute the frustum originating at the light source, that encompasses all the objects passed into Add...
Definition: ShadowMap.cpp:234
int GetHeight() const
GetHeight: Return the height of the depth texture.
Definition: ShadowMap.cpp:618
ShadowMap()
Definition: ShadowMap.cpp:102