Coverage Report

Created: 2025-07-11 06:36

/src/ogre/OgreMain/include/OgreShadowCameraSetupLiSPSM.h
Line
Count
Source (jump to first uncovered line)
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-2014 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 @cite WSP04
46
47
    Shadow mapping was introduced by Williams in 1978. First a depth image is rendered
48
    from the light's view and compared in a second pass with depth values of the normal 
49
    camera view. In case the depth camera's depth value is greater than the depth seen
50
    by the light the fragment lies in the shadow.
51
    The concept has a major draw back named perspective aliasing. The shadow map distri-
52
    butes the samples uniformly meaning the position of the viewer is ignored. For the 
53
    viewer however the perspective projection affects near objects to be displayed 
54
    bigger than further away objects. The same thing happens with the shadow map texels:
55
    Near shadows appear very coarse and far away shadows are perfectly sampled.
56
    PSM @cite stamminger2002perspective 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 @cite lloyd2007practical
84
    is the non plus ultra, however others reject this thought.
85
86
    More information can be found on the webpage of the TU Wien: 
87
    http://www.cg.tuwien.ac.at/research/vr/lispsm/
88
89
    Original implementation by Matthias Fink <matthias.fink@web.de>, 2006.
90
    */
91
    class _OgreExport LiSPSMShadowCameraSetup : public FocusedShadowCameraSetup
92
    {
93
        /// Warp factor adjustment
94
        Real mOptAdjustFactor;
95
        /// Use simple nopt derivation?
96
        bool mUseSimpleNOpt;
97
        /// Extra calculated warp factor
98
        mutable Real mOptAdjustFactorTweak;
99
        /// Threshold (cos angle) within which to start increasing the opt adjust as camera direction approaches light direction
100
        Real mCosCamLightDirThreshold;
101
102
        /** Calculates the LiSPSM projection matrix P.
103
104
        The LiSPSM projection matrix will be built around the axis aligned bounding box 
105
        of the intersection body B in light space. The distance between the near plane 
106
        and the projection center is chosen in such a way (distance is set by the para-
107
        meter n) that the perspective error is the same on the near and far plane. In 
108
        case P equals the identity matrix the algorithm falls back to a uniform shadow
109
        mapping matrix.
110
        @param lightSpace Matrix of the light space transformation
111
        @param bodyB Intersection body B
112
        @param bodyLVS Intersection body LVS (relevant space in front of the camera)
113
        @param sm Scene manager
114
        @param cam Currently active camera
115
        @param light Currently active light
116
        */
117
        Matrix4 calculateLiSPSM(const Matrix4& lightSpace, const PointListBody& bodyB, 
118
            const PointListBody& bodyLVS, const SceneManager& sm, 
119
            const Camera& cam, const Light& light) const;
120
121
        /** Calculates the distance between camera position and near clipping plane.
122
123
        n_opt determines the distance between light space origin (shadow camera position)
124
        and the near clipping plane to achieve an optimal perspective foreshortening effect.
125
        In this way the texel distribution over the shadow map is controlled.
126
127
        Formula:
128
                       d
129
        n_opt = ---------------
130
                sqrt(z1/z0) - 1
131
132
        Parameters:
133
        d: distance between the near and the far clipping plane
134
        z0: located on the near clipping plane of the intersection body b
135
        z1: located on the far clipping plane with the same x/y values as z0        
136
        @note
137
        A positive value is applied as the distance between viewer and near clipping plane.
138
        In case null is returned uniform shadow mapping will be applied.
139
        @param lightSpace Matrix of the light space transformation
140
        @param bodyBABB_ls Bounding box of the transformed (light space) bodyB
141
        @param bodyLVS Point list of the bodyLVS which describes the scene space which is in
142
        front of the light and the camera
143
        @param cam Currently active camera
144
        */
145
        Real calculateNOpt(const Matrix4& lightSpace, const AxisAlignedBox& bodyBABB_ls, 
146
            const PointListBody& bodyLVS, const Camera& cam) const;
147
148
        /** Calculates a simpler version than the one above.
149
        */
150
        Real calculateNOptSimple(const PointListBody& bodyLVS, 
151
            const Camera& cam) const;
152
153
        /** Calculates the visible point on the near plane for the n_opt calculation
154
155
        z0 lies on the parallel plane to the near plane through e and on the near plane of 
156
        the frustum C (plane z = bodyB_zMax_ls) and on the line x = e.x.
157
        @param lightSpace Matrix of the light space transformation
158
        @param e The LiSPSM parameter e is located near or on the near clipping plane of the
159
        LiSPSM frustum C
160
        @param bodyB_zMax_ls Maximum z-value of the light space bodyB bounding box
161
        @param cam Currently active camera
162
        */
163
        Vector3 calculateZ0_ls(const Matrix4& lightSpace, const Vector3& e, Real bodyB_zMax_ls, 
164
            const Camera& cam) const;
165
    public:
166
        /// @deprecated use create()
167
        LiSPSMShadowCameraSetup(Real n = 0.1f, bool useSimpleNOpt = true, Degree angle = Radian(0.451f));
168
169
        virtual ~LiSPSMShadowCameraSetup();
170
171
        /**
172
         * @param n The adjustment factor
173
         * @param useSimpleNOpt
174
         * @param angle camera Light Direction Threshold
175
         */
176
        static ShadowCameraSetupPtr create(Real n = 0.1f, bool useSimpleNOpt = true, Degree angle = Radian(0.451f))
177
0
        {
178
0
            return std::make_shared<LiSPSMShadowCameraSetup>(n, useSimpleNOpt, angle);
179
0
        }
180
181
        /** Returns a LiSPSM shadow camera.
182
183
        Builds and returns a LiSPSM shadow camera. 
184
        More information can be found on the webpage of the TU Wien: 
185
        http://www.cg.tuwien.ac.at/research/vr/lispsm/
186
        */
187
        void getShadowCamera(const SceneManager *sm, const Camera *cam,
188
            const Viewport *vp, const Light *light, Camera *texCam, size_t iteration) const override;
189
190
        /** Adjusts the parameter n to produce optimal shadows.
191
192
        The smaller the parameter n, the stronger the perspective warping effect.
193
        The consequence of a stronger warping is that the near shadows will gain 
194
        quality while the far ones will lose it. Depending on your scene and light
195
        types you may want to tweak this value - for example directional lights
196
        tend to benefit from higher values of n than other types of light, 
197
        especially if you expect to see more distant shadows (say if the viewpoint is
198
        higher above the ground plane). Remember that you can supply separate
199
        ShadowCameraSetup instances configured differently per light if you wish.
200
        @param n The adjustment factor - default is 0.1f. 
201
        */
202
0
        virtual void setOptimalAdjustFactor(Real n) { mOptAdjustFactor = n; }
203
        /** Get the parameter n used to produce optimal shadows. 
204
        @see setOptimalAdjustFactor
205
        */
206
0
        virtual Real getOptimalAdjustFactor() const { return mOptAdjustFactor; }
207
        /** Sets whether or not to use a slightly simpler version of the 
208
            camera near point derivation (default is true)
209
        */
210
0
        virtual void setUseSimpleOptimalAdjust(bool s) { mUseSimpleNOpt = s; }
211
        /** Gets whether or not to use a slightly simpler version of the 
212
        camera near point derivation (default is true)
213
        */
214
0
        virtual bool getUseSimpleOptimalAdjust() const { return mUseSimpleNOpt; }
215
216
        /** Sets the threshold between the camera and the light direction below
217
            which the LiSPSM projection is 'flattened', since coincident light
218
            and camera projections cause problems with the perspective skew.
219
220
            For example, setting this to 20 degrees will mean that as the difference 
221
            between the light and camera direction reduces from 20 degrees to 0
222
            degrees, the perspective skew will be proportionately removed.
223
        */
224
        void setCameraLightDirectionThreshold(Degree angle);
225
226
        /** Sets the threshold between the camera and the light direction below
227
        which the LiSPSM projection is 'flattened', since coincident light
228
        and camera projections cause problems with the perspective skew.
229
        */
230
        virtual Degree getCameraLightDirectionThreshold() const;
231
232
233
    };
234
    /** @} */
235
    /** @} */
236
237
}
238
239
#include "OgreHeaderSuffix.h"
240
241
#endif
242