2008-08-05 19:17:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2007-09-22 21:06:07 +08:00
*
2008-08-05 19:17:48 +08:00
* 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
2007-09-22 21:06:07 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2008-08-05 19:17:48 +08:00
*
2007-09-22 21:06:07 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2008-08-05 19:17:48 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2007-09-22 21:06:07 +08:00
* OpenSceneGraph Public License for more details.
2007-09-19 19:56:44 +08:00
*/
2008-08-05 19:17:48 +08:00
/* ParallelSplitShadowMap written by Adrian Egli
2008-05-28 22:11:22 +08:00
*
* this version has still a bug in mutli-thread application (flickering problem)
* to avoid the flickering problem try osgShadow --pssm --SingleThreaded your_scene.ive
*
2008-08-05 19:17:48 +08:00
* 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.
2008-05-28 22:11:22 +08:00
*
*/
2007-09-19 19:56:44 +08:00
2008-08-05 19:17:48 +08:00
#ifndef OSGSHADOW_ParallelSplitShadowMap
2007-09-19 19:56:44 +08:00
#define OSGSHADOW_ParallelSplitShadowMap 1
#include <osg/Camera>
#include <osg/Material>
2008-05-28 22:11:22 +08:00
#include <osg/Depth>
2008-06-19 22:45:54 +08:00
#include <osg/ClipPlane>
2007-09-19 19:56:44 +08:00
#include <osgShadow/ShadowTechnique>
2007-09-24 23:24:23 +08:00
2007-09-19 19:56:44 +08:00
namespace osgShadow {
2007-09-24 23:24:23 +08:00
2008-08-05 19:17:48 +08:00
class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
2007-09-19 19:56:44 +08:00
{
public:
ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3);
ParallelSplitShadowMap(const ParallelSplitShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2008-08-05 19:17:48 +08:00
2007-09-19 19:56:44 +08:00
META_Object(osgShadow, ParallelSplitShadowMap);
2008-08-05 19:17:48 +08:00
2007-09-23 00:46:38 +08:00
/** Initialize the ShadowedScene and local cached data structures.*/
2007-09-19 19:56:44 +08:00
virtual void init();
2007-09-23 00:46:38 +08:00
/** Run the update traversal of the ShadowedScene and update any loca chached data structures.*/
2007-09-19 19:56:44 +08:00
virtual void update(osg::NodeVisitor& nv);
2007-09-23 00:46:38 +08:00
/** Run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/
2007-09-19 19:56:44 +08:00
virtual void cull(osgUtil::CullVisitor& cv);
/** Clean scene graph from any shadow technique specific nodes, state and drawables.*/
virtual void cleanSceneGraph();
2007-09-23 00:46:38 +08:00
/** Switch on the debug coloring in GLSL (only the first 3 texture/splits showed for visualisation */
inline void setDebugColorOn() { _debug_color_in_GLSL = true; }
/** Set the polygon offset osg::Vec2f(factor,unit) */
inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;}
/** Get the polygon offset osg::Vec2f(factor,unit) */
2010-04-20 18:29:04 +08:00
inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;}
2007-09-23 00:46:38 +08:00
/** Set the texture resolution */
inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; }
2012-03-22 01:36:20 +08:00
2010-04-20 18:29:04 +08:00
/** Get the texture resolution */
inline unsigned int getTextureResolution() const { return _resolution; }
2007-09-23 00:46:38 +08:00
2008-08-05 19:17:48 +08:00
/** Set the max far distance */
2007-09-24 23:24:23 +08:00
inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; }
2010-04-20 18:29:04 +08:00
/** Get the max far distance */
inline double getMaxFarDistance() const { return _setMaxFarDistance; }
2007-09-27 20:47:34 +08:00
/** Set the factor for moving the virtual camera behind the real camera*/
inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; }
2010-04-20 18:29:04 +08:00
/** Get the factor for moving the virtual camera behind the real camera*/
inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; }
2008-08-05 19:17:48 +08:00
/** Set min near distance for splits */
2007-09-24 23:24:23 +08:00
inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }
2012-03-22 01:36:20 +08:00
2010-04-20 18:29:04 +08:00
/** Get min near distance for splits */
inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; }
2007-09-24 23:24:23 +08:00
2008-08-05 19:17:48 +08:00
/** set a user defined light for shadow simulation (sun light, ... )
* when this light get passed to pssm, the scene's light are no longer collected
* and simulated. just this user passed light, it needs to be a directional light.
2008-05-28 22:11:22 +08:00
*/
inline void setUserLight(osg::Light* light) { _userLight = light; }
2012-03-22 01:36:20 +08:00
2010-04-20 18:29:04 +08:00
/** get the user defined light for shadow simulation */
2010-05-12 19:37:27 +08:00
inline const osg::Light* getUserLight() const { return _userLight.get(); }
2008-08-05 19:17:48 +08:00
2008-05-28 22:11:22 +08:00
/** Set the values for the ambient bias the shader will use.*/
2010-04-20 18:35:33 +08:00
void setAmbientBias(const osg::Vec2& ambientBias );
2012-03-22 01:36:20 +08:00
2010-04-20 18:29:04 +08:00
/** Get the values for the ambient bias the shader will use.*/
2010-04-20 18:35:33 +08:00
const osg::Vec2& getAmbientBias() const { return _ambientBias; }
2008-06-19 22:45:54 +08:00
/**
* you can overwrite the fragment shader if you like to modify it yourself, own fragment shader can be used
*/
2008-06-27 18:17:38 +08:00
class OSGSHADOW_EXPORT FragmentShaderGenerator : public osg::Referenced {
2008-06-19 22:45:54 +08:00
public:
2008-08-05 19:17:48 +08:00
/**
2017-08-16 00:23:49 +08:00
* generate the GLSL fragment shader
2008-06-19 22:45:54 +08:00
*/
virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset);
};
2008-08-05 19:17:48 +08:00
2008-06-19 22:45:54 +08:00
/** set fragment shader generator */
inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;}
2008-08-05 19:17:48 +08:00
2008-06-19 22:45:54 +08:00
/** enable / disable shadow filtering */
inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; }
2008-08-05 19:17:48 +08:00
2008-06-19 22:45:54 +08:00
enum SplitCalcMode {
SPLIT_LINEAR,
SPLIT_EXP
};
2008-08-05 19:17:48 +08:00
2008-06-19 22:45:54 +08:00
/** set split calculation mode */
inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; }
2012-03-22 01:36:20 +08:00
2010-04-20 18:29:04 +08:00
/** get split calculation mode */
inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; }
2008-08-05 19:17:48 +08:00
2016-02-05 01:52:44 +08:00
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
/** If State is non-zero, this function releases any associated OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objects
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* = 0) const;
2007-09-19 19:56:44 +08:00
protected :
virtual ~ParallelSplitShadowMap() {}
2008-08-05 19:17:48 +08:00
2007-09-19 19:56:44 +08:00
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;
2008-06-19 22:45:54 +08:00
double _split_far;
2008-05-28 22:11:22 +08:00
2007-09-19 19:56:44 +08:00
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;
2008-05-28 22:11:22 +08:00
osg::Matrix _cameraView;
osg::Matrix _cameraProj;
2007-09-19 19:56:44 +08:00
2008-05-28 22:11:22 +08:00
unsigned int _splitID;
unsigned int _resolution;
2007-09-19 19:56:44 +08:00
2016-02-05 01:52:44 +08:00
osg::ref_ptr<osg::Uniform> _farDistanceSplit;
void resizeGLObjectBuffers(unsigned int maxSize);
void releaseGLObjects(osg::State* = 0) const;
2007-09-19 19:56:44 +08:00
};
2008-06-19 22:45:54 +08:00
2007-09-19 19:56:44 +08:00
typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap;
PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap;
private:
void calculateFrustumCorners(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
2008-08-05 19:17:48 +08:00
void calculateLightInitialPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
2007-09-19 19:56:44 +08:00
void calculateLightNearFarFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
void calculateLightViewProjectionFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners);
osg::Geode** _displayTexturesGroupingNode;
unsigned int _textureUnitOffset;
2007-09-23 00:46:38 +08:00
unsigned int _number_of_splits;
bool _debug_color_in_GLSL;
2010-04-20 18:35:33 +08:00
osg::Vec2 _polgyonOffset;
2007-09-23 00:46:38 +08:00
bool _user_polgyonOffset_set;
unsigned int _resolution;
2007-09-24 23:24:23 +08:00
double _setMaxFarDistance;
bool _isSetMaxFarDistance;
double _split_min_near_dist;
2007-09-27 20:47:34 +08:00
double _move_vcam_behind_rcam_factor;
2008-08-05 19:17:48 +08:00
2008-06-19 22:45:54 +08:00
osg::ref_ptr<osg::Light> _userLight;
osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator;
2007-09-27 20:47:34 +08:00
2008-10-27 21:09:43 +08:00
bool _GLSL_shadow_filtered;
SplitCalcMode _SplitCalcMode;
2008-08-05 19:17:48 +08:00
2008-10-27 21:09:43 +08:00
osg::Uniform* _ambientBiasUniform;
2010-04-20 18:35:33 +08:00
osg::Vec2 _ambientBias;
2008-05-28 22:11:22 +08:00
2007-09-19 19:56:44 +08:00
};
}
#endif