1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14/* ParallelSplitShadowMap written by Adrian Egli
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
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.
26#ifndef OSGSHADOW_ParallelSplitShadowMap
27#define OSGSHADOW_ParallelSplitShadowMap 1
30#include <osg/Material>
32#include <osg/ClipPlane>
34#include <osgShadow/ShadowTechnique>
38class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
41 ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3);
43 ParallelSplitShadowMap(const ParallelSplitShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
45 META_Object(osgShadow, ParallelSplitShadowMap);
48 /** Initialize the ShadowedScene and local cached data structures.*/
51 /** Run the update traversal of the ShadowedScene and update any loca chached data structures.*/
52 virtual void update(osg::NodeVisitor& nv);
54 /** Run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
55 virtual void cull(osgUtil::CullVisitor& cv);
57 /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
58 virtual void cleanSceneGraph();
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; }
63 /** Set the polygon offset osg::Vec2f(factor,unit) */
64 inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;}
66 /** Get the polygon offset osg::Vec2f(factor,unit) */
67 inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;}
69 /** Set the texture resolution */
70 inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; }
72 /** Get the texture resolution */
73 inline unsigned int getTextureResolution() const { return _resolution; }
75 /** Set the max far distance */
76 inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; }
78 /** Get the max far distance */
79 inline double getMaxFarDistance() const { return _setMaxFarDistance; }
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; }
84 /** Get the factor for moving the virtual camera behind the real camera*/
85 inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; }
87 /** Set min near distance for splits */
88 inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }
90 /** Get min near distance for splits */
91 inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; }
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.
97 inline void setUserLight(osg::Light* light) { _userLight = light; }
99 /** get the user defined light for shadow simulation */
100 inline const osg::Light* getUserLight() const { return _userLight.get(); }
102 /** Set the values for the ambient bias the shader will use.*/
103 void setAmbientBias(const osg::Vec2& ambientBias );
105 /** Get the values for the ambient bias the shader will use.*/
106 const osg::Vec2& getAmbientBias() const { return _ambientBias; }
109 * you can overwrite the fragment shader if you like to modify it yourself, own fragment shader can be used
111 class OSGSHADOW_EXPORT FragmentShaderGenerator : public osg::Referenced {
114 * generate the GLSL fragment shader
116 virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset);
119 /** set fragment shader generator */
120 inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;}
122 /** enable / disable shadow filtering */
123 inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; }
130 /** set split calculation mode */
131 inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; }
133 /** get split calculation mode */
134 inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; }
137 /** Resize any per context GLObject buffers to specified size. */
138 virtual void resizeGLObjectBuffers(unsigned int maxSize);
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;
147 virtual ~ParallelSplitShadowMap() {}
150 struct PSSMShadowSplitTexture {
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;
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;
167 osg::Vec3d _lightCameraSource;
168 osg::Vec3d _lightCameraTarget;
169 osg::Vec3d _frustumSplitCenter;
170 osg::Vec3d _lightDirection;
174 osg::Matrix _cameraView;
175 osg::Matrix _cameraProj;
177 unsigned int _splitID;
178 unsigned int _resolution;
180 osg::ref_ptr<osg::Uniform> _farDistanceSplit;
182 void resizeGLObjectBuffers(unsigned int maxSize);
183 void releaseGLObjects(osg::State* = 0) const;
187 typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap;
188 PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap;
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);
197 osg::Geode** _displayTexturesGroupingNode;
199 unsigned int _textureUnitOffset;
201 unsigned int _number_of_splits;
203 bool _debug_color_in_GLSL;
205 osg::Vec2 _polgyonOffset;
206 bool _user_polgyonOffset_set;
208 unsigned int _resolution;
210 double _setMaxFarDistance;
211 bool _isSetMaxFarDistance;
213 double _split_min_near_dist;
215 double _move_vcam_behind_rcam_factor;
217 osg::ref_ptr<osg::Light> _userLight;
218 osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator;
220 bool _GLSL_shadow_filtered;
221 SplitCalcMode _SplitCalcMode;
223 osg::Uniform* _ambientBiasUniform;
224 osg::Vec2 _ambientBias;