openscenegraph
ParallelSplitShadowMap
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14/* ParallelSplitShadowMap written by Adrian Egli
15 *
16 * this version has still a bug in mutli-thread application (flickering problem)
17 * to avoid the flickering problem try osgShadow --pssm --SingleThreaded your_scene.ive
18 *
19 * The Parallel Split Shadow Map only supports directional light for simulating the shadow.
20 * It's one of the most robust algorithm for huge terrain sun light's shadow simulation, if
21 * you need to shadow a terrain, or another huge scene, you should use Parallel Split Shadow Map
22 * or at least test it against your scene. Have fun.
23 *
24 */
25
26#ifndef OSGSHADOW_ParallelSplitShadowMap
27#define OSGSHADOW_ParallelSplitShadowMap 1
28
29#include <osg/Camera>
30#include <osg/Material>
31#include <osg/Depth>
32#include <osg/ClipPlane>
33
34#include <osgShadow/ShadowTechnique>
35
36namespace osgShadow {
37
38class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
39{
40 public:
41 ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3);
42
43 ParallelSplitShadowMap(const ParallelSplitShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
44
45 META_Object(osgShadow, ParallelSplitShadowMap);
46
47
48 /** Initialize the ShadowedScene and local cached data structures.*/
49 virtual void init();
50
51 /** Run the update traversal of the ShadowedScene and update any loca chached data structures.*/
52 virtual void update(osg::NodeVisitor& nv);
53
54 /** Run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
55 virtual void cull(osgUtil::CullVisitor& cv);
56
57 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
58 virtual void cleanSceneGraph();
59
60 /** Switch on the debug coloring in GLSL (only the first 3 texture/splits showed for visualisation */
61 inline void setDebugColorOn() { _debug_color_in_GLSL = true; }
62
63 /** Set the polygon offset osg::Vec2f(factor,unit) */
64 inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;}
65
66 /** Get the polygon offset osg::Vec2f(factor,unit) */
67 inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;}
68
69 /** Set the texture resolution */
70 inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; }
71
72 /** Get the texture resolution */
73 inline unsigned int getTextureResolution() const { return _resolution; }
74
75 /** Set the max far distance */
76 inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; }
77
78 /** Get the max far distance */
79 inline double getMaxFarDistance() const { return _setMaxFarDistance; }
80
81 /** Set the factor for moving the virtual camera behind the real camera*/
82 inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; }
83
84 /** Get the factor for moving the virtual camera behind the real camera*/
85 inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; }
86
87 /** Set min near distance for splits */
88 inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }
89
90 /** Get min near distance for splits */
91 inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; }
92
93 /** set a user defined light for shadow simulation (sun light, ... )
94 * when this light get passed to pssm, the scene's light are no longer collected
95 * and simulated. just this user passed light, it needs to be a directional light.
96 */
97 inline void setUserLight(osg::Light* light) { _userLight = light; }
98
99 /** get the user defined light for shadow simulation */
100 inline const osg::Light* getUserLight() const { return _userLight.get(); }
101
102 /** Set the values for the ambient bias the shader will use.*/
103 void setAmbientBias(const osg::Vec2& ambientBias );
104
105 /** Get the values for the ambient bias the shader will use.*/
106 const osg::Vec2& getAmbientBias() const { return _ambientBias; }
107
108 /**
109 * you can overwrite the fragment shader if you like to modify it yourself, own fragment shader can be used
110 */
111 class OSGSHADOW_EXPORT FragmentShaderGenerator : public osg::Referenced {
112 public:
113 /**
114 * generate the GLSL fragment shader
115 */
116 virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset);
117 };
118
119 /** set fragment shader generator */
120 inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;}
121
122 /** enable / disable shadow filtering */
123 inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; }
124
125 enum SplitCalcMode {
126 SPLIT_LINEAR,
127 SPLIT_EXP
128 };
129
130 /** set split calculation mode */
131 inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; }
132
133 /** get split calculation mode */
134 inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; }
135
136
137 /** Resize any per context GLObject buffers to specified size. */
138 virtual void resizeGLObjectBuffers(unsigned int maxSize);
139
140 /** If State is non-zero, this function releases any associated OpenGL objects for
141 * the specified graphics context. Otherwise, releases OpenGL objects
142 * for all graphics contexts. */
143 virtual void releaseGLObjects(osg::State* = 0) const;
144
145 protected :
146
147 virtual ~ParallelSplitShadowMap() {}
148
149
150 struct PSSMShadowSplitTexture {
151 // RTT
152 osg::ref_ptr<osg::Camera> _camera;
153 osg::ref_ptr<osg::TexGen> _texgen;
154 osg::ref_ptr<osg::Texture2D> _texture;
155 osg::ref_ptr<osg::StateSet> _stateset;
156 unsigned int _textureUnit;
157
158
159 double _split_far;
160
161 osg::ref_ptr<osg::Camera> _debug_camera;
162 osg::ref_ptr<osg::Texture2D> _debug_texture;
163 osg::ref_ptr<osg::StateSet> _debug_stateset;
164 unsigned int _debug_textureUnit;
165
166 // Light (SUN)
167 osg::Vec3d _lightCameraSource;
168 osg::Vec3d _lightCameraTarget;
169 osg::Vec3d _frustumSplitCenter;
170 osg::Vec3d _lightDirection;
171 double _lightNear;
172 double _lightFar;
173
174 osg::Matrix _cameraView;
175 osg::Matrix _cameraProj;
176
177 unsigned int _splitID;
178 unsigned int _resolution;
179
180 osg::ref_ptr<osg::Uniform> _farDistanceSplit;
181
182 void resizeGLObjectBuffers(unsigned int maxSize);
183 void releaseGLObjects(osg::State* = 0) const;
184
185 };
186
187 typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap;
188 PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap;
189
190
191 private:
192 void calculateFrustumCorners(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
193 void calculateLightInitialPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
194 void calculateLightNearFarFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
195 void calculateLightViewProjectionFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
196
197 osg::Geode** _displayTexturesGroupingNode;
198
199 unsigned int _textureUnitOffset;
200
201 unsigned int _number_of_splits;
202
203 bool _debug_color_in_GLSL;
204
205 osg::Vec2 _polgyonOffset;
206 bool _user_polgyonOffset_set;
207
208 unsigned int _resolution;
209
210 double _setMaxFarDistance;
211 bool _isSetMaxFarDistance;
212
213 double _split_min_near_dist;
214
215 double _move_vcam_behind_rcam_factor;
216
217 osg::ref_ptr<osg::Light> _userLight;
218 osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator;
219
220 bool _GLSL_shadow_filtered;
221 SplitCalcMode _SplitCalcMode;
222
223 osg::Uniform* _ambientBiasUniform;
224 osg::Vec2 _ambientBias;
225
226};
227}
228#endif