| [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 __ShadowCameraSetupFocused_H__ |
|---|
| 30 | #define __ShadowCameraSetupFocused_H__ |
|---|
| 31 | |
|---|
| 32 | #include "OgrePrerequisites.h" |
|---|
| 33 | #include "OgreShadowCameraSetup.h" |
|---|
| 34 | #include "OgrePolygon.h" |
|---|
| 35 | #include "OgreConvexBody.h" |
|---|
| 36 | #include "OgreHeaderPrefix.h" |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | namespace Ogre { |
|---|
| 40 | |
|---|
| 41 | class ConvexBody; |
|---|
| 42 | |
|---|
| 43 | /** \addtogroup Core |
|---|
| 44 | * @{ |
|---|
| 45 | */ |
|---|
| 46 | /** \addtogroup Scene |
|---|
| 47 | * @{ |
|---|
| 48 | */ |
|---|
| 49 | /** Implements the uniform shadow mapping algorithm in focused mode. |
|---|
| 50 | @remarks |
|---|
| 51 | Differs from the default shadow mapping projection in that it focuses the |
|---|
| 52 | shadow map on the visible areas of the scene. This results in better |
|---|
| 53 | shadow map texel usage, at the expense of some 'swimming' of the shadow |
|---|
| 54 | texture on receivers as the basis is constantly being reevaluated. |
|---|
| 55 | @note |
|---|
| 56 | Original implementation by Matthias Fink <matthias.fink@web.de>, 2006. |
|---|
| 57 | */ |
|---|
| 58 | class _OgreExport FocusedShadowCameraSetup : public ShadowCameraSetup |
|---|
| 59 | { |
|---|
| 60 | protected: |
|---|
| 61 | /** Transform to or from light space as defined by Wimmer et al. |
|---|
| 62 | @remarks |
|---|
| 63 | Point and spot lights need to be converted to directional lights to enable a 1:1 |
|---|
| 64 | light mapping. Otherwise a directional light may become a point light or a point |
|---|
| 65 | sink (opposite of a light source) or point/spot lights may become directional lights |
|---|
| 66 | or light sinks. The light direction is always -y. |
|---|
| 67 | */ |
|---|
| 68 | static const Matrix4 msNormalToLightSpace; |
|---|
| 69 | static const Matrix4 msLightSpaceToNormal; |
|---|
| 70 | |
|---|
| 71 | /** Temporary preallocated frustum to set up a projection matrix in |
|---|
| 72 | calculateShadowMappingMatrix(). |
|---|
| 73 | */ |
|---|
| 74 | Frustum* mTempFrustum; |
|---|
| 75 | |
|---|
| 76 | /** Temporary preallocated camera to set up a light frustum for clipping in FocusedShadowCameraSetup::calculateB. |
|---|
| 77 | */ |
|---|
| 78 | Camera* mLightFrustumCamera; |
|---|
| 79 | mutable bool mLightFrustumCameraCalculated; |
|---|
| 80 | |
|---|
| 81 | /// Use tighter focus region? |
|---|
| 82 | bool mUseAggressiveRegion; |
|---|
| 83 | |
|---|
| 84 | /** Internal class holding a point list representation of a convex body. |
|---|
| 85 | */ |
|---|
| 86 | class _OgreExport PointListBody |
|---|
| 87 | { |
|---|
| 88 | Polygon::VertexList mBodyPoints; |
|---|
| 89 | AxisAlignedBox mAAB; |
|---|
| 90 | |
|---|
| 91 | public: |
|---|
| 92 | PointListBody(); |
|---|
| 93 | PointListBody(const ConvexBody& body); |
|---|
| 94 | ~PointListBody(); |
|---|
| 95 | |
|---|
| 96 | /** Merges a second PointListBody into this one. |
|---|
| 97 | */ |
|---|
| 98 | void merge(const PointListBody& plb); |
|---|
| 99 | |
|---|
| 100 | /** Builds a point list body from a 'real' body. |
|---|
| 101 | @remarks |
|---|
| 102 | Inserts all vertices from a body into the point list with or without adding duplicate vertices. |
|---|
| 103 | */ |
|---|
| 104 | void build(const ConvexBody& body, bool filterDuplicates = true); |
|---|
| 105 | |
|---|
| 106 | /** Builds a PointListBody from a Body and includes all the space in a given direction. |
|---|
| 107 | @remarks |
|---|
| 108 | Intersects the bounding box with a ray from each available point of the body with the given |
|---|
| 109 | direction. Base and intersection points are stored in a PointListBody structure. |
|---|
| 110 | @note |
|---|
| 111 | Duplicate vertices are not filtered. |
|---|
| 112 | @note |
|---|
| 113 | Body is not checked for correctness. |
|---|
| 114 | */ |
|---|
| 115 | void buildAndIncludeDirection(const ConvexBody& body, |
|---|
| 116 | Real extrudeDist, const Vector3& dir); |
|---|
| 117 | |
|---|
| 118 | /** Returns the bounding box representation. |
|---|
| 119 | */ |
|---|
| 120 | const AxisAlignedBox& getAAB(void) const; |
|---|
| 121 | |
|---|
| 122 | /** Adds a specific point to the body list. |
|---|
| 123 | */ |
|---|
| 124 | void addPoint(const Vector3& point); |
|---|
| 125 | |
|---|
| 126 | /** Adds all points of an AAB. |
|---|
| 127 | */ |
|---|
| 128 | void addAAB(const AxisAlignedBox& aab); |
|---|
| 129 | |
|---|
| 130 | /** Returns a point. |
|---|
| 131 | */ |
|---|
| 132 | const Vector3& getPoint(size_t cnt) const; |
|---|
| 133 | |
|---|
| 134 | /** Returns the point count. |
|---|
| 135 | */ |
|---|
| 136 | size_t getPointCount(void) const; |
|---|
| 137 | |
|---|
| 138 | /** Resets the body. |
|---|
| 139 | */ |
|---|
| 140 | void reset(void); |
|---|
| 141 | |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | // Persistent calculations to prevent reallocation |
|---|
| 145 | mutable ConvexBody mBodyB; |
|---|
| 146 | mutable PointListBody mPointListBodyB; |
|---|
| 147 | mutable PointListBody mPointListBodyLVS; |
|---|
| 148 | |
|---|
| 149 | protected: |
|---|
| 150 | /** Calculates the standard shadow mapping matrix. |
|---|
| 151 | @remarks |
|---|
| 152 | Provides the view and projection matrix for standard shadow mapping. |
|---|
| 153 | @note |
|---|
| 154 | You can choose which things you want to have: view matrix and/or projection |
|---|
| 155 | matrix and/or shadow camera. Passing a NULL value as parameter ignores the |
|---|
| 156 | generation of this specific value. |
|---|
| 157 | @param sm |
|---|
| 158 | Scene manager. |
|---|
| 159 | @param cam |
|---|
| 160 | Currently active camera. |
|---|
| 161 | @param light |
|---|
| 162 | Currently active light. |
|---|
| 163 | @param out_view |
|---|
| 164 | Calculated uniform view shadow mapping matrix (may be @c NULL). |
|---|
| 165 | @param out_proj |
|---|
| 166 | Calculated uniform projection shadow mapping matrix (may be @c NULL). |
|---|
| 167 | @param out_cam |
|---|
| 168 | Calculated uniform shadow camera (may be @c NULL). |
|---|
| 169 | */ |
|---|
| 170 | void calculateShadowMappingMatrix(const SceneManager& sm, const Camera& cam, |
|---|
| 171 | const Light& light, Matrix4 *out_view, |
|---|
| 172 | Matrix4 *out_proj, Camera *out_cam) const; |
|---|
| 173 | |
|---|
| 174 | /** Calculates the intersection bodyB. |
|---|
| 175 | @remarks |
|---|
| 176 | The intersection bodyB consists of the concatenation the cam frustum clipped |
|---|
| 177 | by the scene bounding box followed by a convex hullification with the light's |
|---|
| 178 | position and the clipping with the scene bounding box and the light frustum: |
|---|
| 179 | ((V \cap S) + l) \cap S \cap L (\cap: convex intersection, +: convex hull |
|---|
| 180 | operation). |
|---|
| 181 | For directional lights the bodyB is assembled out of the camera frustum |
|---|
| 182 | clipped by the scene bounding box followed by the extrusion of all available |
|---|
| 183 | bodyB points towards the negative light direction. The rays are intersected |
|---|
| 184 | by a maximum bounding box and added to the bodyB points to form the final |
|---|
| 185 | intersection bodyB point list. |
|---|
| 186 | @param sm |
|---|
| 187 | Scene manager. |
|---|
| 188 | @param cam |
|---|
| 189 | Currently active camera. |
|---|
| 190 | @param light |
|---|
| 191 | Currently active light. |
|---|
| 192 | @param sceneBB |
|---|
| 193 | Scene bounding box for clipping operations. |
|---|
| 194 | @param receiverBB |
|---|
| 195 | Bounding information for just the receivers. |
|---|
| 196 | @param out_bodyB |
|---|
| 197 | Final intersection bodyB point list. |
|---|
| 198 | */ |
|---|
| 199 | void calculateB(const SceneManager& sm, const Camera& cam, const Light& light, |
|---|
| 200 | const AxisAlignedBox& sceneBB, const AxisAlignedBox& receiverBB, PointListBody *out_bodyB) const; |
|---|
| 201 | |
|---|
| 202 | /** Calculates the bodyLVS. |
|---|
| 203 | @remarks |
|---|
| 204 | Calculates the bodyLVS which consists of the convex intersection operation |
|---|
| 205 | affecting the light frustum, the view frustum, and the current scene bounding |
|---|
| 206 | box is used to find suitable positions in the viewer's frustum to build the |
|---|
| 207 | rotation matrix L_r. This matrix is applied after the projection matrix L_p to |
|---|
| 208 | avoid an accidental flip of the frustum orientation for views tilted with |
|---|
| 209 | respect to the shadow map. |
|---|
| 210 | @param cam |
|---|
| 211 | Current viewer camera. |
|---|
| 212 | @param light |
|---|
| 213 | Current light. |
|---|
| 214 | @param sceneBB |
|---|
| 215 | Holds all potential occluders / receivers as one single bounding box |
|---|
| 216 | of the currently active scene node. |
|---|
| 217 | @param out_LVS |
|---|
| 218 | Intersection body LVS (world coordinates). |
|---|
| 219 | */ |
|---|
| 220 | void calculateLVS(const SceneManager& sm, const Camera& cam, const Light& light, |
|---|
| 221 | const AxisAlignedBox& sceneBB, PointListBody *out_LVS) const; |
|---|
| 222 | |
|---|
| 223 | /** Returns the projection view direction. |
|---|
| 224 | @remarks |
|---|
| 225 | After the matrix L_p is applied the orientation of the light space may tilt for |
|---|
| 226 | non-identity projections. To prevent a false shadow cast the real view direction |
|---|
| 227 | is evaluated and applied to the light matrix L. |
|---|
| 228 | @param lightSpace |
|---|
| 229 | Matrix of the light space transformation. |
|---|
| 230 | @param cam |
|---|
| 231 | Current viewer camera. |
|---|
| 232 | @param bodyLVS |
|---|
| 233 | Intersection body LVS (relevant space in front of the camera). |
|---|
| 234 | */ |
|---|
| 235 | Vector3 getLSProjViewDir(const Matrix4& lightSpace, const Camera& cam, |
|---|
| 236 | const PointListBody& bodyLVS) const; |
|---|
| 237 | |
|---|
| 238 | /** Returns a valid near-point seen by the camera. |
|---|
| 239 | @remarks |
|---|
| 240 | Returns a point that is situated near the camera by analyzing the bodyLVS that |
|---|
| 241 | contains all the relevant scene space in front of the light and the camera in |
|---|
| 242 | a point list array. The view matrix is relevant because the nearest point in |
|---|
| 243 | front of the camera should be determined. |
|---|
| 244 | @param viewMatrix |
|---|
| 245 | View matrix of the current camera. |
|---|
| 246 | @param bodyLVS |
|---|
| 247 | Intersection body LVS (relevant space in front of the camera). |
|---|
| 248 | */ |
|---|
| 249 | Vector3 getNearCameraPoint_ws(const Matrix4& viewMatrix, |
|---|
| 250 | const PointListBody& bodyLVS) const; |
|---|
| 251 | |
|---|
| 252 | /** Transforms a given body to the unit cube (-1,-1,-1) / (+1,+1,+1) with a specific |
|---|
| 253 | shadow matrix enabled. |
|---|
| 254 | @remarks |
|---|
| 255 | Transforms a given point list body object with the matrix m and then maps its |
|---|
| 256 | extends to a (-1,-1,-1) / (+1,+1,+1) unit cube. |
|---|
| 257 | @param m |
|---|
| 258 | Transformation matrix applied on the point list body. |
|---|
| 259 | @param body |
|---|
| 260 | Contains the points of the extends of all valid scene elements which |
|---|
| 261 | are mapped to the unit cube. |
|---|
| 262 | */ |
|---|
| 263 | Matrix4 transformToUnitCube(const Matrix4& m, const PointListBody& body) const; |
|---|
| 264 | |
|---|
| 265 | /** Builds a view matrix. |
|---|
| 266 | @remarks |
|---|
| 267 | Builds a standard view matrix out of a given position, direction and up vector. |
|---|
| 268 | */ |
|---|
| 269 | Matrix4 buildViewMatrix(const Vector3& pos, const Vector3& dir, const Vector3& up) const; |
|---|
| 270 | |
|---|
| 271 | public: |
|---|
| 272 | /** Default constructor. |
|---|
| 273 | @remarks |
|---|
| 274 | Temporary frustum and camera set up here. |
|---|
| 275 | */ |
|---|
| 276 | FocusedShadowCameraSetup(void); |
|---|
| 277 | |
|---|
| 278 | /** Default destructor. |
|---|
| 279 | @remarks |
|---|
| 280 | Temporary frustum and camera destroyed here. |
|---|
| 281 | */ |
|---|
| 282 | virtual ~FocusedShadowCameraSetup(void); |
|---|
| 283 | |
|---|
| 284 | /** Returns a uniform shadow camera with a focused view. |
|---|
| 285 | */ |
|---|
| 286 | virtual void getShadowCamera(const SceneManager *sm, const Camera *cam, |
|---|
| 287 | const Viewport *vp, const Light *light, Camera *texCam, size_t iteration) const; |
|---|
| 288 | |
|---|
| 289 | /** Sets whether or not to use the more aggressive approach to deciding on |
|---|
| 290 | the focus region or not. |
|---|
| 291 | @note |
|---|
| 292 | There are 2 approaches that can be used to define the focus region, |
|---|
| 293 | the more aggressive way introduced by Wimmer et al, or the original |
|---|
| 294 | way as described in Stamminger et al. Wimmer et al's way tends to |
|---|
| 295 | come up with a tighter focus region but in rare cases (mostly highly |
|---|
| 296 | glancing angles) can cause some shadow casters to be clipped |
|---|
| 297 | incorrectly. By default the more aggressive approach is used since it |
|---|
| 298 | leads to significantly better results in most cases, but if you experience |
|---|
| 299 | clipping issues, you can use the less aggressive version. |
|---|
| 300 | @param aggressive |
|---|
| 301 | True to use the more aggressive approach, false otherwise. |
|---|
| 302 | */ |
|---|
| 303 | void setUseAggressiveFocusRegion(bool aggressive) { mUseAggressiveRegion = aggressive; } |
|---|
| 304 | |
|---|
| 305 | bool getUseAggressiveFocusRegion() const { return mUseAggressiveRegion; } |
|---|
| 306 | |
|---|
| 307 | }; |
|---|
| 308 | |
|---|
| 309 | /** @} */ |
|---|
| 310 | /** @} */ |
|---|
| 311 | |
|---|
| 312 | } // namespace Ogre |
|---|
| 313 | |
|---|
| 314 | #include "OgreHeaderSuffix.h" |
|---|
| 315 | |
|---|
| 316 | #endif // __ShadowCameraSetupFocused_H__ |
|---|