[148] | 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) 2000-2013 Torus Knot Software Ltd |
---|
| 8 | Copyright (c) 2006 Matthias Fink, netAllied GmbH <matthias.fink@web.de> |
---|
| 9 | |
---|
| 10 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 11 | of this software and associated documentation files (the "Software"), to deal |
---|
| 12 | in the Software without restriction, including without limitation the rights |
---|
| 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 14 | copies of the Software, and to permit persons to whom the Software is |
---|
| 15 | furnished to do so, subject to the following conditions: |
---|
| 16 | |
---|
| 17 | The above copyright notice and this permission notice shall be included in |
---|
| 18 | all copies or substantial portions of the Software. |
---|
| 19 | |
---|
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 26 | THE SOFTWARE. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __ShadowCameraSetupLiSPSM_H__ |
---|
| 30 | #define __ShadowCameraSetupLiSPSM_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | #include "OgreShadowCameraSetupFocused.h" |
---|
| 34 | #include "OgreHeaderPrefix.h" |
---|
| 35 | |
---|
| 36 | namespace Ogre |
---|
| 37 | { |
---|
| 38 | |
---|
| 39 | /** \addtogroup Core |
---|
| 40 | * @{ |
---|
| 41 | */ |
---|
| 42 | /** \addtogroup Scene |
---|
| 43 | * @{ |
---|
| 44 | */ |
---|
| 45 | /** Implements the Light Space Perspective Shadow Mapping Algorithm. |
---|
| 46 | @remarks |
---|
| 47 | Implements the LiSPSM algorithm for an advanced shadow map generation. LiSPSM was |
---|
| 48 | developed by Michael Wimmer, Daniel Scherzer and Werner Purgathofer of the TU Wien. |
---|
| 49 | The algorithm was presented on the Eurographics Symposium on Rendering 2004. |
---|
| 50 | @note |
---|
| 51 | Shadow mapping was introduced by Williams in 1978. First a depth image is rendered |
---|
| 52 | from the light's view and compared in a second pass with depth values of the normal |
---|
| 53 | camera view. In case the depth camera's depth value is greater than the depth seen |
---|
| 54 | by the light the fragment lies in the shadow. |
---|
| 55 | The concept has a major draw back named perspective aliasing. The shadow map distri- |
---|
| 56 | butes the samples uniformly meaning the position of the viewer is ignored. For the |
---|
| 57 | viewer however the perspective projection affects near objects to be displayed |
---|
| 58 | bigger than further away objects. The same thing happens with the shadow map texels: |
---|
| 59 | Near shadows appear very coarse and far away shadows are perfectly sampled. |
---|
| 60 | In 2002 Stamminger et al. presented an algorithm called Perspective Shadow Maps |
---|
| 61 | (PSM). PSM battles the perspective aliasing by distributing 50% of the shadow map |
---|
| 62 | texels for objects in the range of <near clipping plane> to <near clipping plane * 2> |
---|
| 63 | which inverts the problem: The shadows near the viewer are perfectly sampled, |
---|
| 64 | however far away shadow may contain aliasing artefacts. A near clipping plane may be |
---|
| 65 | a problem. But this is not the only one. In the post-perspective space the light |
---|
| 66 | sources are non-intuitively mapped: Directional lights may become point light and |
---|
| 67 | point lights may become directional lights. Also light sinks (opposite of a light |
---|
| 68 | source) may appear. Another problem are shadow casters located behind the viewer. |
---|
| 69 | In post-projective space objects behind the viewer are mapped in front of him with |
---|
| 70 | a flipped up-vector. |
---|
| 71 | LiSPSM battles the light source problem of the post-projective space by rearranging |
---|
| 72 | the light space before transformation in such a way that no special cases appear. |
---|
| 73 | This is done by converting point/spot lights into directional lights. The light |
---|
| 74 | space is arranged in such a way that the light direction equals the inverse UNIT_Y. |
---|
| 75 | In this combination the directional light will neither change its type nor its |
---|
| 76 | direction. Furthermore all visible objects and shadow casters affecting the user's |
---|
| 77 | visible area lie in front of the shadow camera: After building the intersection body |
---|
| 78 | that contains all these objects (body intersection building was introduced with PSM; |
---|
| 79 | have a look at the description for the method "calculateB" for further info) a |
---|
| 80 | frustum around the body's light space bounding box is created. A parameter (called |
---|
| 81 | 'n') automatically adjusts the shadow map sample distribution by specifying the |
---|
| 82 | frustum's view point - near plane which affects the perspective warp. In case the |
---|
| 83 | distance is small the perspecive warp will be strong. As a consequence near objects |
---|
| 84 | will gain quality. |
---|
| 85 | However there are still problems. PSM as well as LiSPSM only devote to minimize |
---|
| 86 | perspective aliasing. Projection aliasing is still a problem, also 'swimming |
---|
| 87 | artefacts' still occur. The LiSPSM quality distribution is very good but not the |
---|
| 88 | best available: Some sources say logarithmic shadow mapping is the non plus ultra, |
---|
| 89 | however others reject this thought. There is a research project on logarithmic shadow |
---|
| 90 | maps. The web page url is http://gamma.cs.unc.edu/logsm/. However there is no techical |
---|
| 91 | report available yet (Oct 23rd, 2006). |
---|
| 92 | @note |
---|
| 93 | More information can be found on the webpage of the TU Wien: |
---|
| 94 | http://www.cg.tuwien.ac.at/research/vr/lispsm/ |
---|
| 95 | @note |
---|
| 96 | Original implementation by Matthias Fink <matthias.fink@web.de>, 2006. |
---|
| 97 | */ |
---|
| 98 | class _OgreExport LiSPSMShadowCameraSetup : public FocusedShadowCameraSetup |
---|
| 99 | { |
---|
| 100 | protected: |
---|
| 101 | /// Warp factor adjustment |
---|
| 102 | Real mOptAdjustFactor; |
---|
| 103 | /// Use simple nopt derivation? |
---|
| 104 | bool mUseSimpleNOpt; |
---|
| 105 | /// Extra calculated warp factor |
---|
| 106 | mutable Real mOptAdjustFactorTweak; |
---|
| 107 | /// Threshold (cos angle) within which to start increasing the opt adjust as camera direction approaches light direction |
---|
| 108 | Real mCosCamLightDirThreshold; |
---|
| 109 | |
---|
| 110 | /** Calculates the LiSPSM projection matrix P. |
---|
| 111 | @remarks |
---|
| 112 | The LiSPSM projection matrix will be built around the axis aligned bounding box |
---|
| 113 | of the intersection body B in light space. The distance between the near plane |
---|
| 114 | and the projection center is chosen in such a way (distance is set by the para- |
---|
| 115 | meter n) that the perspective error is the same on the near and far plane. In |
---|
| 116 | case P equals the identity matrix the algorithm falls back to a uniform shadow |
---|
| 117 | mapping matrix. |
---|
| 118 | @param lightSpace Matrix of the light space transformation |
---|
| 119 | @param bodyB Intersection body B |
---|
| 120 | @param bodyLVS Intersection body LVS (relevant space in front of the camera) |
---|
| 121 | @param sm Scene manager |
---|
| 122 | @param cam Currently active camera |
---|
| 123 | @param light Currently active light |
---|
| 124 | */ |
---|
| 125 | Matrix4 calculateLiSPSM(const Matrix4& lightSpace, const PointListBody& bodyB, |
---|
| 126 | const PointListBody& bodyLVS, const SceneManager& sm, |
---|
| 127 | const Camera& cam, const Light& light) const; |
---|
| 128 | |
---|
| 129 | /** Calculates the distance between camera position and near clipping plane. |
---|
| 130 | @remarks |
---|
| 131 | n_opt determines the distance between light space origin (shadow camera position) |
---|
| 132 | and the near clipping plane to achieve an optimal perspective foreshortening effect. |
---|
| 133 | In this way the texel distribution over the shadow map is controlled. |
---|
| 134 | |
---|
| 135 | Formula: |
---|
| 136 | d |
---|
| 137 | n_opt = --------------- |
---|
| 138 | sqrt(z1/z0) - 1 |
---|
| 139 | |
---|
| 140 | Parameters: |
---|
| 141 | d: distance between the near and the far clipping plane |
---|
| 142 | z0: located on the near clipping plane of the intersection body b |
---|
| 143 | z1: located on the far clipping plane with the same x/y values as z0 |
---|
| 144 | @note |
---|
| 145 | A positive value is applied as the distance between viewer and near clipping plane. |
---|
| 146 | In case null is returned uniform shadow mapping will be applied. |
---|
| 147 | @param lightSpace Matrix of the light space transformation |
---|
| 148 | @param bodyBABB_ls Bounding box of the transformed (light space) bodyB |
---|
| 149 | @param bodyLVS Point list of the bodyLVS which describes the scene space which is in |
---|
| 150 | front of the light and the camera |
---|
| 151 | @param cam Currently active camera |
---|
| 152 | */ |
---|
| 153 | Real calculateNOpt(const Matrix4& lightSpace, const AxisAlignedBox& bodyBABB_ls, |
---|
| 154 | const PointListBody& bodyLVS, const Camera& cam) const; |
---|
| 155 | |
---|
| 156 | /** Calculates a simpler version than the one above. |
---|
| 157 | */ |
---|
| 158 | Real calculateNOptSimple(const PointListBody& bodyLVS, |
---|
| 159 | const Camera& cam) const; |
---|
| 160 | |
---|
| 161 | /** Calculates the visible point on the near plane for the n_opt calculation |
---|
| 162 | @remarks |
---|
| 163 | z0 lies on the parallel plane to the near plane through e and on the near plane of |
---|
| 164 | the frustum C (plane z = bodyB_zMax_ls) and on the line x = e.x. |
---|
| 165 | @param lightSpace Matrix of the light space transformation |
---|
| 166 | @param e The LiSPSM parameter e is located near or on the near clipping plane of the |
---|
| 167 | LiSPSM frustum C |
---|
| 168 | @param bodyB_zMax_ls Maximum z-value of the light space bodyB bounding box |
---|
| 169 | @param cam Currently active camera |
---|
| 170 | */ |
---|
| 171 | Vector3 calculateZ0_ls(const Matrix4& lightSpace, const Vector3& e, Real bodyB_zMax_ls, |
---|
| 172 | const Camera& cam) const; |
---|
| 173 | |
---|
| 174 | /** Builds a frustum matrix. |
---|
| 175 | @remarks |
---|
| 176 | Builds a standard frustum matrix out of the distance info of the six frustum |
---|
| 177 | clipping planes. |
---|
| 178 | */ |
---|
| 179 | Matrix4 buildFrustumProjection(Real left, Real right, Real bottom, |
---|
| 180 | Real top, Real near, Real far) const; |
---|
| 181 | |
---|
| 182 | public: |
---|
| 183 | /** Default constructor. |
---|
| 184 | @remarks |
---|
| 185 | Nothing done here. |
---|
| 186 | */ |
---|
| 187 | LiSPSMShadowCameraSetup(void); |
---|
| 188 | |
---|
| 189 | /** Default destructor. |
---|
| 190 | @remarks |
---|
| 191 | Nothing done here. |
---|
| 192 | */ |
---|
| 193 | virtual ~LiSPSMShadowCameraSetup(void); |
---|
| 194 | |
---|
| 195 | /** Returns a LiSPSM shadow camera. |
---|
| 196 | @remarks |
---|
| 197 | Builds and returns a LiSPSM shadow camera. |
---|
| 198 | More information can be found on the webpage of the TU Wien: |
---|
| 199 | http://www.cg.tuwien.ac.at/research/vr/lispsm/ |
---|
| 200 | */ |
---|
| 201 | virtual void getShadowCamera(const SceneManager *sm, const Camera *cam, |
---|
| 202 | const Viewport *vp, const Light *light, Camera *texCam, size_t iteration) const; |
---|
| 203 | |
---|
| 204 | /** Adjusts the parameter n to produce optimal shadows. |
---|
| 205 | @remarks |
---|
| 206 | The smaller the parameter n, the stronger the perspective warping effect. |
---|
| 207 | The consequence of a stronger warping is that the near shadows will gain |
---|
| 208 | quality while the far ones will lose it. Depending on your scene and light |
---|
| 209 | types you may want to tweak this value - for example directional lights |
---|
| 210 | tend to benefit from higher values of n than other types of light, |
---|
| 211 | especially if you expect to see more distant shadows (say if the viewpoint is |
---|
| 212 | higher above the ground plane). Remember that you can supply separate |
---|
| 213 | ShadowCameraSetup instances configured differently per light if you wish. |
---|
| 214 | @param n The adjustment factor - default is 0.1f. |
---|
| 215 | */ |
---|
| 216 | virtual void setOptimalAdjustFactor(Real n) { mOptAdjustFactor = n; } |
---|
| 217 | /** Get the parameter n used to produce optimal shadows. |
---|
| 218 | @see setOptimalAdjustFactor |
---|
| 219 | */ |
---|
| 220 | virtual Real getOptimalAdjustFactor() const { return mOptAdjustFactor; } |
---|
| 221 | /** Sets whether or not to use a slightly simpler version of the |
---|
| 222 | camera near point derivation (default is true) |
---|
| 223 | */ |
---|
| 224 | virtual void setUseSimpleOptimalAdjust(bool s) { mUseSimpleNOpt = s; } |
---|
| 225 | /** Gets whether or not to use a slightly simpler version of the |
---|
| 226 | camera near point derivation (default is true) |
---|
| 227 | */ |
---|
| 228 | virtual bool getUseSimpleOptimalAdjust() const { return mUseSimpleNOpt; } |
---|
| 229 | |
---|
| 230 | /** Sets the threshold between the camera and the light direction below |
---|
| 231 | which the LiSPSM projection is 'flattened', since coincident light |
---|
| 232 | and camera projections cause problems with the perspective skew. |
---|
| 233 | @remarks |
---|
| 234 | For example, setting this to 20 degrees will mean that as the difference |
---|
| 235 | between the light and camera direction reduces from 20 degrees to 0 |
---|
| 236 | degrees, the perspective skew will be proportionately removed. |
---|
| 237 | */ |
---|
| 238 | virtual void setCameraLightDirectionThreshold(Degree angle); |
---|
| 239 | |
---|
| 240 | /** Sets the threshold between the camera and the light direction below |
---|
| 241 | which the LiSPSM projection is 'flattened', since coincident light |
---|
| 242 | and camera projections cause problems with the perspective skew. |
---|
| 243 | */ |
---|
| 244 | virtual Degree getCameraLightDirectionThreshold() const; |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | }; |
---|
| 248 | /** @} */ |
---|
| 249 | /** @} */ |
---|
| 250 | |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | #include "OgreHeaderSuffix.h" |
---|
| 254 | |
---|
| 255 | #endif |
---|
| 256 | |
---|