1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2006 Torus Knot Software Ltd |
---|
8 | Copyright (c) 2006 Matthias Fink, netAllied GmbH <matthias.fink@web.de> |
---|
9 | Also see acknowledgements in Readme.html |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under |
---|
12 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
14 | version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU Lesser General Public License along with |
---|
21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
23 | http://www.gnu.org/copyleft/lesser.txt. |
---|
24 | |
---|
25 | You may alternatively use this source under the terms of a specific version of |
---|
26 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
27 | Torus Knot Software Ltd. |
---|
28 | ----------------------------------------------------------------------------- |
---|
29 | */ |
---|
30 | #ifndef __ShadowCameraSetupLiSPSM_H__ |
---|
31 | #define __ShadowCameraSetupLiSPSM_H__ |
---|
32 | |
---|
33 | #include "OgrePrerequisites.h" |
---|
34 | #include "OgreShadowCameraSetupFocused.h" |
---|
35 | |
---|
36 | |
---|
37 | namespace Ogre |
---|
38 | { |
---|
39 | |
---|
40 | /** Implements the Light Space Perspective Shadow Mapping Algorithm. |
---|
41 | @remarks |
---|
42 | Implements the LiSPSM algorithm for an advanced shadow map generation. LiSPSM was |
---|
43 | developed by Michael Wimmer, Daniel Scherzer and Werner Purgathofer of the TU Wien. |
---|
44 | The algorithm was presented on the Eurographics Symposium on Rendering 2004. |
---|
45 | @note |
---|
46 | Shadow mapping was introduced by Williams in 1978. First a depth image is rendered |
---|
47 | from the light's view and compared in a second pass with depth values of the normal |
---|
48 | camera view. In case the depth camera's depth value is greater than the depth seen |
---|
49 | by the light the fragment lies in the shadow. |
---|
50 | The concept has a major draw back named perspective aliasing. The shadow map distri- |
---|
51 | butes the samples uniformly meaning the position of the viewer is ignored. For the |
---|
52 | viewer however the perspective projection affects near objects to be displayed |
---|
53 | bigger than further away objects. The same thing happens with the shadow map texels: |
---|
54 | Near shadows appear very coarse and far away shadows are perfectly sampled. |
---|
55 | In 2002 Stamminger et al. presented an algorithm called Perspective Shadow Maps |
---|
56 | (PSM). PSM battles the perspective aliasing by distributing 50% of the shadow map |
---|
57 | texels for objects in the range of <near clipping plane> to <near clipping plane * 2> |
---|
58 | which inverts the problem: The shadows near the viewer are perfectly sampled, |
---|
59 | however far away shadow may contain aliasing artefacts. A near clipping plane may be |
---|
60 | a problem. But this is not the only one. In the post-perspective space the light |
---|
61 | sources are non-intuitively mapped: Directional lights may become point light and |
---|
62 | point lights may become directional lights. Also light sinks (opposite of a light |
---|
63 | source) may appear. Another problem are shadow casters located behind the viewer. |
---|
64 | In post-projective space objects behind the viewer are mapped in front of him with |
---|
65 | a flipped up-vector. |
---|
66 | LiSPSM battles the light source problem of the post-projective space by rearranging |
---|
67 | the light space before transformation in such a way that no special cases appear. |
---|
68 | This is done by converting point/spot lights into directional lights. The light |
---|
69 | space is arranged in such a way that the light direction equals the inverse UNIT_Y. |
---|
70 | In this combination the directional light will neither change its type nor its |
---|
71 | direction. Furthermore all visible objects and shadow casters affecting the user's |
---|
72 | visible area lie in front of the shadow camera: After building the intersection body |
---|
73 | that contains all these objects (body intersection building was introduced with PSM; |
---|
74 | have a look at the description for the method "calculateB" for further info) a |
---|
75 | frustum around the body's light space bounding box is created. A parameter (called |
---|
76 | 'n') automatically adjusts the shadow map sample distribution by specifying the |
---|
77 | frustum's view point - near plane which affects the perspective warp. In case the |
---|
78 | distance is small the perspecive warp will be strong. As a consequence near objects |
---|
79 | will gain quality. |
---|
80 | However there are still problems. PSM as well as LiSPSM only devote to minimize |
---|
81 | perspective aliasing. Projection aliasing is still a problem, also 'swimming |
---|
82 | artefacts' still occur. The LiSPSM quality distribution is very good but not the |
---|
83 | best available: Some sources say logarithmic shadow mapping is the non plus ultra, |
---|
84 | however others reject this thought. There is a research project on logarithmic shadow |
---|
85 | maps. The web page url is http://gamma.cs.unc.edu/logsm/. However there is no techical |
---|
86 | report available yet (Oct 23rd, 2006). |
---|
87 | @note |
---|
88 | More information can be found on the webpage of the TU Wien: |
---|
89 | http://www.cg.tuwien.ac.at/research/vr/lispsm/ |
---|
90 | @note |
---|
91 | Original implementation by Matthias Fink <matthias.fink@web.de>, 2006. |
---|
92 | */ |
---|
93 | class _OgreExport LiSPSMShadowCameraSetup : public FocusedShadowCameraSetup |
---|
94 | { |
---|
95 | protected: |
---|
96 | /// Warp factor adjustment |
---|
97 | Real mOptAdjustFactor; |
---|
98 | /// Use simple nopt derivation? |
---|
99 | bool mUseSimpleNOpt; |
---|
100 | |
---|
101 | /** Calculates the LiSPSM projection matrix P. |
---|
102 | @remarks |
---|
103 | The LiSPSM projection matrix will be built around the axis aligned bounding box |
---|
104 | of the intersection body B in light space. The distance between the near plane |
---|
105 | and the projection center is chosen in such a way (distance is set by the para- |
---|
106 | meter n) that the perspective error is the same on the near and far plane. In |
---|
107 | case P equals the identity matrix the algorithm falls back to a uniform shadow |
---|
108 | mapping matrix. |
---|
109 | @param lightSpace: matrix of the light space transformation |
---|
110 | @param bodyB: intersection body B |
---|
111 | @param bodyLVS: intersection body LVS (relevant space in front of the camera) |
---|
112 | @param sm: scene manager |
---|
113 | @param cam: currently active camera |
---|
114 | @param light: currently active light |
---|
115 | */ |
---|
116 | Matrix4 calculateLiSPSM(const Matrix4& lightSpace, const PointListBody& bodyB, |
---|
117 | const PointListBody& bodyLVS, const SceneManager& sm, |
---|
118 | const Camera& cam, const Light& light) const; |
---|
119 | |
---|
120 | /** Calculates the distance between camera position and near clipping plane. |
---|
121 | @remarks |
---|
122 | n_opt determines the distance between light space origin (shadow camera position) |
---|
123 | and the near clipping plane to achieve an optimal perspective forshortening effect. |
---|
124 | In this way the texel distibution over the shadow map is controlled. |
---|
125 | |
---|
126 | Formula: |
---|
127 | d |
---|
128 | n_opt = --------------- |
---|
129 | sqrt(z1/z0) - 1 |
---|
130 | |
---|
131 | Parameters: |
---|
132 | d: distance between the near and the far clipping plane |
---|
133 | z0: located on the near clipping plane of the intersection body b |
---|
134 | z1: located on the far clipping plane with the same x/y values as z0 |
---|
135 | @note |
---|
136 | A positive value is applied as the distance between viewer and near clipping plane. |
---|
137 | In case null is returned uniform shadow mapping will be applied. |
---|
138 | @param lightSpace: matrix of the light space transformation |
---|
139 | @param bodyBABB_ls: bounding box of the tranformed (light space) bodyB |
---|
140 | @param bodyLVS: point list of the bodyLVS which describes the scene space which is in |
---|
141 | front of the light and the camera |
---|
142 | @param cam: currently active camera |
---|
143 | */ |
---|
144 | Real calculateNOpt(const Matrix4& lightSpace, const AxisAlignedBox& bodyBABB_ls, |
---|
145 | const PointListBody& bodyLVS, const Camera& cam) const; |
---|
146 | |
---|
147 | /** Calculates a simpler version than the one above. |
---|
148 | */ |
---|
149 | Real calculateNOptSimple(const PointListBody& bodyLVS, |
---|
150 | const Camera& cam) const; |
---|
151 | |
---|
152 | /** Calculates the visible point on the near plane for the n_opt calculation |
---|
153 | @remarks |
---|
154 | z0 lies on the parallel plane to the near plane through e and on the near plane of |
---|
155 | the frustum C (plane z = bodyB_zMax_ls) and on the line x = e.x. |
---|
156 | @param lightSpace: matrix of the light space transformation |
---|
157 | @param e: the LiSPSM parameter e is located near or on the near clipping plane of the |
---|
158 | LiSPSM frustum C |
---|
159 | @param bodyB_zMax_ls: maximum z-value of the light space bodyB bounding box |
---|
160 | @param cam: currently active camera |
---|
161 | */ |
---|
162 | Vector3 calculateZ0_ls(const Matrix4& lightSpace, const Vector3& e, Real bodyB_zMax_ls, |
---|
163 | const Camera& cam) const; |
---|
164 | |
---|
165 | /** Builds a frustum matrix. |
---|
166 | @remarks |
---|
167 | Builds a standard frustum matrix out of the distance infos of the six frustum |
---|
168 | clipping planes. |
---|
169 | */ |
---|
170 | Matrix4 buildFrustumProjection(Real left, Real right, Real bottom, |
---|
171 | Real top, Real near, Real far) const; |
---|
172 | |
---|
173 | public: |
---|
174 | /** Default constructor. |
---|
175 | @remarks |
---|
176 | Nothing done here. |
---|
177 | */ |
---|
178 | LiSPSMShadowCameraSetup(void); |
---|
179 | |
---|
180 | /** Default destructor. |
---|
181 | @remarks |
---|
182 | Nothing done here. |
---|
183 | */ |
---|
184 | virtual ~LiSPSMShadowCameraSetup(void); |
---|
185 | |
---|
186 | /** Returns a LiSPSM shadow camera. |
---|
187 | @remarks |
---|
188 | Builds and returns a LiSPSM shadow camera. |
---|
189 | More information can be found on the webpage of the TU Wien: |
---|
190 | http://www.cg.tuwien.ac.at/research/vr/lispsm/ |
---|
191 | */ |
---|
192 | virtual void getShadowCamera(const SceneManager *sm, const Camera *cam, |
---|
193 | const Viewport *vp, const Light *light, Camera *texCam) const; |
---|
194 | |
---|
195 | /** Adjusts the parameter n to produce optimal shadows. |
---|
196 | @remarks |
---|
197 | The smaller the parameter n, the stronger the perspective warping effect. |
---|
198 | The consequence of a stronger warping is that the near shadows will gain |
---|
199 | quality while the far ones will lose it. Depending on your scene and light |
---|
200 | types you may want to tweak this value - for example directional lights |
---|
201 | tend to benefit from higher values of n than other types of light, |
---|
202 | especially if you expect to see more distant shadows (say if the viewpoint is |
---|
203 | higher above the ground plane). Remember that you can supply separate |
---|
204 | ShadowCameraSetup instances configured differently per light if you wish. |
---|
205 | @param n The adjustment factor - default is 0.1f. |
---|
206 | */ |
---|
207 | virtual void setOptimalAdjustFactor(Real n) { mOptAdjustFactor = n; } |
---|
208 | /** Get the parameter n used to produce optimal shadows. |
---|
209 | @see setOptimalAdjustFactor |
---|
210 | */ |
---|
211 | virtual Real getOptimalAdjustFactor() const { return mOptAdjustFactor; } |
---|
212 | /** Sets whether or not to use a slightly simpler version of the |
---|
213 | camera near point derivation (default is true) |
---|
214 | */ |
---|
215 | virtual void setUseSimpleOptimalAdjust(bool s) { mUseSimpleNOpt = s; } |
---|
216 | /** Gets whether or not to use a slightly simpler version of the |
---|
217 | camera near point derivation (default is true) |
---|
218 | */ |
---|
219 | virtual bool getUseSimpleOptimalAdjust() const { return mUseSimpleNOpt; } |
---|
220 | |
---|
221 | }; |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | #endif |
---|
226 | |
---|