ParallelSplitShadowMap
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
/* ParallelSplitShadowMap written by Adrian Egli
*
* this version has still a bug in mutli-thread application (flickering problem)
* to avoid the flickering problem try osgShadow --pssm --SingleThreaded your_scene.ive
*
* The Parallel Split Shadow Map only supports directional light for simulating the shadow.
* It's one of the most robust algorithm for huge terrain sun light's shadow simulation, if
* you need to shadow a terrain, or another huge scene, you should use Parallel Split Shadow Map
* or at least test it against your scene. Have fun.
*
*/
#ifndef OSGSHADOW_ParallelSplitShadowMap
#define OSGSHADOW_ParallelSplitShadowMap 1
#include <osg/Camera>
#include <osg/Material>
#include <osg/Depth>
#include <osg/ClipPlane>
#include <osgShadow/ShadowTechnique>
namespace osgShadow {
class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
{
public:
ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3);
ParallelSplitShadowMap(const ParallelSplitShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgShadow, ParallelSplitShadowMap);
virtual void init();
virtual void update(osg::NodeVisitor& nv);
virtual void cull(osgUtil::CullVisitor& cv);
virtual void cleanSceneGraph();
inline void setDebugColorOn() { _debug_color_in_GLSL = true; }
inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;}
inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;}
inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; }
inline unsigned int getTextureResolution() const { return _resolution; }
inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; }
inline double getMaxFarDistance() const { return _setMaxFarDistance; }
inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; }
inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; }
inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }
inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; }
inline void setUserLight(osg::Light* light) { _userLight = light; }
inline const osg::Light* getUserLight() const { return _userLight.get(); }
void setAmbientBias(const osg::Vec2& ambientBias );
const osg::Vec2& getAmbientBias() const { return _ambientBias; }
class OSGSHADOW_EXPORT FragmentShaderGenerator : public osg::Referenced {
public:
virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset);
};
inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;}
inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; }
enum SplitCalcMode {
SPLIT_LINEAR,
SPLIT_EXP
};
inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; }
inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; }
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(osg::State* = 0) const;
protected :
virtual ~ParallelSplitShadowMap() {}
struct PSSMShadowSplitTexture {
// RTT
osg::ref_ptr<osg::Camera> _camera;
osg::ref_ptr<osg::TexGen> _texgen;
osg::ref_ptr<osg::Texture2D> _texture;
osg::ref_ptr<osg::StateSet> _stateset;
unsigned int _textureUnit;
double _split_far;
osg::ref_ptr<osg::Camera> _debug_camera;
osg::ref_ptr<osg::Texture2D> _debug_texture;
osg::ref_ptr<osg::StateSet> _debug_stateset;
unsigned int _debug_textureUnit;
// Light (SUN)
osg::Vec3d _lightCameraSource;
osg::Vec3d _lightCameraTarget;
osg::Vec3d _frustumSplitCenter;
osg::Vec3d _lightDirection;
double _lightNear;
double _lightFar;
osg::Matrix _cameraView;
osg::Matrix _cameraProj;
unsigned int _splitID;
unsigned int _resolution;
osg::ref_ptr<osg::Uniform> _farDistanceSplit;
void resizeGLObjectBuffers(unsigned int maxSize);
void releaseGLObjects(osg::State* = 0) const;
};
typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap;
PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap;
private:
void calculateFrustumCorners(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
void calculateLightInitialPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
void calculateLightNearFarFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
void calculateLightViewProjectionFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
osg::Geode** _displayTexturesGroupingNode;
unsigned int _textureUnitOffset;
unsigned int _number_of_splits;
bool _debug_color_in_GLSL;
osg::Vec2 _polgyonOffset;
bool _user_polgyonOffset_set;
unsigned int _resolution;
double _setMaxFarDistance;
bool _isSetMaxFarDistance;
double _split_min_near_dist;
double _move_vcam_behind_rcam_factor;
osg::ref_ptr<osg::Light> _userLight;
osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator;
bool _GLSL_shadow_filtered;
SplitCalcMode _SplitCalcMode;
osg::Uniform* _ambientBiasUniform;
osg::Vec2 _ambientBias;
};
}
#endif
Camera
ClipPlane
Depth
Material
ShadowTechnique
osgShadow::ParallelSplitShadowMap::FragmentShaderGenerator
Definition ParallelSplitShadowMap:111
osgShadow::ParallelSplitShadowMap::FragmentShaderGenerator::generateGLSL_FragmentShader_BaseTex
virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount, double textureRes, bool filtered, unsigned int nbrSplits, unsigned int textureOffset)
osgShadow::ParallelSplitShadowMap::setPolygonOffset
void setPolygonOffset(const osg::Vec2f &p)
Definition ParallelSplitShadowMap:64
osgShadow::ParallelSplitShadowMap::cull
virtual void cull(osgUtil::CullVisitor &cv)
osgShadow::ParallelSplitShadowMap::ParallelSplitShadowMap
ParallelSplitShadowMap(const ParallelSplitShadowMap &es, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgShadow::ParallelSplitShadowMap::init
virtual void init()
osgShadow::ParallelSplitShadowMap::setFragmentShaderGenerator
void setFragmentShaderGenerator(FragmentShaderGenerator *fsw)
Definition ParallelSplitShadowMap:120
osgShadow::ParallelSplitShadowMap::ParallelSplitShadowMap
ParallelSplitShadowMap(osg::Geode **debugGroup=NULL, int icountplanes=3)
osgShadow::ParallelSplitShadowMap::SplitCalcMode
SplitCalcMode
Definition ParallelSplitShadowMap:125
osgShadow::ParallelSplitShadowMap::SPLIT_EXP
@ SPLIT_EXP
Definition ParallelSplitShadowMap:127
osgShadow::ParallelSplitShadowMap::SPLIT_LINEAR
@ SPLIT_LINEAR
Definition ParallelSplitShadowMap:126
osgShadow::ParallelSplitShadowMap::_PSSMShadowSplitTextureMap
PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap
Definition ParallelSplitShadowMap:188
osgShadow::ParallelSplitShadowMap::setMoveVCamBehindRCamFactor
void setMoveVCamBehindRCamFactor(double distFactor)
Definition ParallelSplitShadowMap:82
osgShadow::ParallelSplitShadowMap::setSplitCalculationMode
void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP)
Definition ParallelSplitShadowMap:131
osgShadow::ParallelSplitShadowMap::setDebugColorOn
void setDebugColorOn()
Definition ParallelSplitShadowMap:61
osgShadow::ParallelSplitShadowMap::META_Object
META_Object(osgShadow, ParallelSplitShadowMap)
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTextureMap
std::map< unsigned int, PSSMShadowSplitTexture > PSSMShadowSplitTextureMap
Definition ParallelSplitShadowMap:187
osgShadow::ParallelSplitShadowMap::getMaxFarDistance
double getMaxFarDistance() const
Definition ParallelSplitShadowMap:79
osgShadow::ParallelSplitShadowMap::~ParallelSplitShadowMap
virtual ~ParallelSplitShadowMap()
Definition ParallelSplitShadowMap:147
osgShadow::ParallelSplitShadowMap::setMaxFarDistance
void setMaxFarDistance(double farDist)
Definition ParallelSplitShadowMap:76
osgShadow::ParallelSplitShadowMap::getMinNearDistanceForSplits
double getMinNearDistanceForSplits() const
Definition ParallelSplitShadowMap:91
osgShadow::ParallelSplitShadowMap::setTextureResolution
void setTextureResolution(unsigned int resolution)
Definition ParallelSplitShadowMap:70
osgShadow::ParallelSplitShadowMap::setMinNearDistanceForSplits
void setMinNearDistanceForSplits(double nd)
Definition ParallelSplitShadowMap:88
osgShadow::ParallelSplitShadowMap::getUserLight
const osg::Light * getUserLight() const
Definition ParallelSplitShadowMap:100
osgShadow::ParallelSplitShadowMap::enableShadowGLSLFiltering
void enableShadowGLSLFiltering(bool filtering=true)
Definition ParallelSplitShadowMap:123
osgShadow::ParallelSplitShadowMap::setAmbientBias
void setAmbientBias(const osg::Vec2 &ambientBias)
osgShadow::ParallelSplitShadowMap::getMoveVCamBehindRCamFactor
double getMoveVCamBehindRCamFactor() const
Definition ParallelSplitShadowMap:85
osgShadow::ParallelSplitShadowMap::getTextureResolution
unsigned int getTextureResolution() const
Definition ParallelSplitShadowMap:73
osgShadow::ParallelSplitShadowMap::update
virtual void update(osg::NodeVisitor &nv)
osgShadow::ParallelSplitShadowMap::getPolygonOffset
const osg::Vec2f & getPolygonOffset() const
Definition ParallelSplitShadowMap:67
osgShadow::ParallelSplitShadowMap::getAmbientBias
const osg::Vec2 & getAmbientBias() const
Definition ParallelSplitShadowMap:106
osgShadow::ParallelSplitShadowMap::cleanSceneGraph
virtual void cleanSceneGraph()
osgShadow::ParallelSplitShadowMap::resizeGLObjectBuffers
virtual void resizeGLObjectBuffers(unsigned int maxSize)
osgShadow::ParallelSplitShadowMap::releaseGLObjects
virtual void releaseGLObjects(osg::State *=0) const
osgShadow::ParallelSplitShadowMap::setUserLight
void setUserLight(osg::Light *light)
Definition ParallelSplitShadowMap:97
osgShadow::ParallelSplitShadowMap::getSplitCalculationMode
SplitCalcMode getSplitCalculationMode() const
Definition ParallelSplitShadowMap:134
osgShadow::ShadowTechnique::ShadowTechnique
ShadowTechnique()
osgUtil::CullVisitor
Definition CullVisitor:49
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::Geode
Definition Geode:29
osg::Light
Definition Light:40
osg::NodeVisitor
Definition NodeVisitor:82
osg::Referenced
Definition Referenced:44
osg::State
Definition State:80
osg::Uniform
Definition Uniform:414
osg::Vec2f
Definition Vec2f:29
osg::Vec3d
Definition Vec3d:30
osg::ref_ptr
Definition ref_ptr:32
osgShadow
Definition ConvexPolyhedron:33
osg::Vec2
Vec2f Vec2
Definition Vec2:21
osg::Matrix
Matrixd Matrix
Definition Matrix:27
OSGSHADOW_EXPORT
#define OSGSHADOW_EXPORT
Definition osgShadow/Export:39
NULL
#define NULL
Definition osg/Export:55
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture
Definition ParallelSplitShadowMap:150
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_lightFar
double _lightFar
Definition ParallelSplitShadowMap:172
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_lightNear
double _lightNear
Definition ParallelSplitShadowMap:171
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_frustumSplitCenter
osg::Vec3d _frustumSplitCenter
Definition ParallelSplitShadowMap:169
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::releaseGLObjects
void releaseGLObjects(osg::State *=0) const
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_splitID
unsigned int _splitID
Definition ParallelSplitShadowMap:177
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_texgen
osg::ref_ptr< osg::TexGen > _texgen
Definition ParallelSplitShadowMap:153
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_texture
osg::ref_ptr< osg::Texture2D > _texture
Definition ParallelSplitShadowMap:154
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_lightCameraTarget
osg::Vec3d _lightCameraTarget
Definition ParallelSplitShadowMap:168
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_debug_texture
osg::ref_ptr< osg::Texture2D > _debug_texture
Definition ParallelSplitShadowMap:162
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_split_far
double _split_far
Definition ParallelSplitShadowMap:159
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_lightDirection
osg::Vec3d _lightDirection
Definition ParallelSplitShadowMap:170
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_debug_stateset
osg::ref_ptr< osg::StateSet > _debug_stateset
Definition ParallelSplitShadowMap:163
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_resolution
unsigned int _resolution
Definition ParallelSplitShadowMap:178
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_cameraView
osg::Matrix _cameraView
Definition ParallelSplitShadowMap:174
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_camera
osg::ref_ptr< osg::Camera > _camera
Definition ParallelSplitShadowMap:152
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_stateset
osg::ref_ptr< osg::StateSet > _stateset
Definition ParallelSplitShadowMap:155
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_farDistanceSplit
osg::ref_ptr< osg::Uniform > _farDistanceSplit
Definition ParallelSplitShadowMap:180
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::resizeGLObjectBuffers
void resizeGLObjectBuffers(unsigned int maxSize)
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_cameraProj
osg::Matrix _cameraProj
Definition ParallelSplitShadowMap:175
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_textureUnit
unsigned int _textureUnit
Definition ParallelSplitShadowMap:156
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_debug_camera
osg::ref_ptr< osg::Camera > _debug_camera
Definition ParallelSplitShadowMap:161
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_debug_textureUnit
unsigned int _debug_textureUnit
Definition ParallelSplitShadowMap:164
osgShadow::ParallelSplitShadowMap::PSSMShadowSplitTexture::_lightCameraSource
osg::Vec3d _lightCameraSource
Definition ParallelSplitShadowMap:167