OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParallelSplitShadowMap.h
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 
36 namespace osgShadow {
37 
39 {
40  public:
41  ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3);
42 
44 
46 
47 
49  virtual void init();
50 
52  virtual void update(osg::NodeVisitor& nv);
53 
55  virtual void cull(osgUtil::CullVisitor& cv);
56 
58  virtual void cleanSceneGraph();
59 
61  inline void setDebugColorOn() { _debug_color_in_GLSL = true; }
62 
64  inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;}
65 
67  inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;}
68 
70  inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; }
71 
73  inline unsigned int getTextureResolution() const { return _resolution; }
74 
76  inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; }
77 
79  inline double getMaxFarDistance() const { return _setMaxFarDistance; }
80 
82  inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; }
83 
85  inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; }
86 
88  inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }
89 
91  inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; }
92 
97  inline void setUserLight(osg::Light* light) { _userLight = light; }
98 
100  inline const osg::Light* getUserLight() const { return _userLight.get(); }
101 
103  void setAmbientBias(const osg::Vec2& ambientBias );
104 
106  const osg::Vec2& getAmbientBias() const { return _ambientBias; }
107 
112  public:
116  virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset);
117  };
118 
120  inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;}
121 
123  inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; }
124 
127  SPLIT_EXP
128  };
129 
131  inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; }
132 
134  inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; }
135 
136 
137  protected :
138 
140 
141 
143  // RTT
148  unsigned int _textureUnit;
149 
150 
151  double _split_far;
152 
156  unsigned int _debug_textureUnit;
157 
158  // Light (SUN)
163  double _lightNear;
164  double _lightFar;
165 
168 
169  unsigned int _splitID;
170  unsigned int _resolution;
171 
173  };
174 
175  typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap;
176  PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap;
177 
178 
179  private:
180  void calculateFrustumCorners(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
181  void calculateLightInitialPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
182  void calculateLightNearFarFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
183  void calculateLightViewProjectionFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
184 
185  osg::Geode** _displayTexturesGroupingNode;
186 
187  unsigned int _textureUnitOffset;
188 
189  unsigned int _number_of_splits;
190 
191  bool _debug_color_in_GLSL;
192 
193  osg::Vec2 _polgyonOffset;
194  bool _user_polgyonOffset_set;
195 
196  unsigned int _resolution;
197 
198  double _setMaxFarDistance;
199  bool _isSetMaxFarDistance;
200 
201  double _split_min_near_dist;
202 
203  double _move_vcam_behind_rcam_factor;
204 
205  osg::ref_ptr<osg::Light> _userLight;
206  osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator;
207 
208  bool _GLSL_shadow_filtered;
209  SplitCalcMode _SplitCalcMode;
210 
211  osg::Uniform* _ambientBiasUniform;
212  osg::Vec2 _ambientBias;
213 
214 };
215 }
216 #endif
void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP)
#define NULL
Definition: Export.h:59
void setMoveVCamBehindRCamFactor(double distFactor)
void setTextureResolution(unsigned int resolution)
const osg::Light * getUserLight() const
const osg::Vec2f & getPolygonOffset() const
#define OSGSHADOW_EXPORT
Definition: Export.h:39
#define META_Object(library, name)
Definition: Object.h:42
std::map< unsigned int, PSSMShadowSplitTexture > PSSMShadowSplitTextureMap
SplitCalcMode getSplitCalculationMode() const
void setPolygonOffset(const osg::Vec2f &p)
PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap
const osg::Vec2 & getAmbientBias() const
void enableShadowGLSLFiltering(bool filtering=true)
void setFragmentShaderGenerator(FragmentShaderGenerator *fsw)