Skip to content

Commit

Permalink
From Adrian Egli, improvements to ParallelSplitShadowMap implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed May 28, 2008
1 parent 8e91926 commit 1047f97
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 304 deletions.
20 changes: 5 additions & 15 deletions examples/osgshadow/osgshadow.cpp
Expand Up @@ -502,10 +502,8 @@ int main(int argc, char** argv)
arguments.getApplicationUsage()->addCommandLineOption("--minNearSplit", "ParallelSplitShadowMap shadow map near offset.");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "ParallelSplitShadowMap max far distance to shadow.");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--moveVCamFactor", "ParallelSplitShadowMap move the virtual frustum behind the real camera, (also back ground object can cast shadow).");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--NVidia", "ParallelSplitShadowMap set default PolygonOffset for NVidia.");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Factor", "ParallelSplitShadowMap set PolygonOffset factor.");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit.");//ADEGLI
arguments.getApplicationUsage()->addCommandLineOption("--CullFaceFront", "ParallelSplitShadowMap add a cull face: front.");//ADEGLI


arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one.");
Expand Down Expand Up @@ -626,20 +624,12 @@ int main(int argc, char** argv)
}


double polyoffsetfactor = -0.02;
double polyoffsetunit = 1.0;

double polyoffsetfactor = pssm->getPolygonOffset().x();
double polyoffsetunit = pssm->getPolygonOffset().y();
while (arguments.read("--PolyOffset-Factor", polyoffsetfactor));
while (arguments.read("--PolyOffset-Unit", polyoffsetunit));
pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit)); //ATI Radeon

if (arguments.read("--NVidia")){
//pssm->setPolygonOffset(osg::Vec2(-0.02,1.0)); //ATI Radeon
pssm->setPolygonOffset(osg::Vec2(10.0f,20.0f)); //NVidia
}

if ( arguments.read("--CullFaceFront") ) {
pssm->forceFrontCullFace();
}
pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit));

shadowedScene->setShadowTechnique(pssm.get());
}
Expand Down Expand Up @@ -761,7 +751,7 @@ int main(int argc, char** argv)


// osgDB::writeNodeFile(*group,"test.osg");

while (!viewer.done())
{
if (updateLightPosition)
Expand Down
73 changes: 37 additions & 36 deletions include/osgShadow/ParallelSplitShadowMap
Expand Up @@ -11,13 +11,24 @@
* OpenSceneGraph Public License for more details.
*/

/* ParallelSplitShadowMap written by Adrian Egli */
/* 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 <osgShadow/ShadowTechnique>

Expand All @@ -32,7 +43,7 @@ class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique

META_Object(osgShadow, ParallelSplitShadowMap);


/** Initialize the ShadowedScene and local cached data structures.*/
virtual void init();

Expand Down Expand Up @@ -63,58 +74,44 @@ class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
/** Set the factor for moving the virtual camera behind the real camera*/
inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; }

/** Force to add a cull face front */
inline void forceFrontCullFace() { _useFrontCullFace = true; }

/** Set min near distance for splits */
inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; }

/** use linear split (default: linear) */
inline void useLinearSplit(bool flag) { _linearSplit = flag;}


/**
light / |
\ / |
min near dist / |
for splits / |
\ / ¦ |
./ \ ¦ |
| \ ¦ |
| x ¦ |
| ¦ |
. | |
\ ¦ |
\ ¦ |
\ |
\ |
\ |
.<- max far dist. ->.
*/

/** 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.
*/
inline void setUserLight(osg::Light* light) { _userLight = light; }


/** Set the values for the ambient bias the shader will use.*/
void setAmbientBias(const osg::Vec2& ambientBias );

protected :

virtual ~ParallelSplitShadowMap() {}

std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount);

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;
osg::Vec2d _ambientBias;


osg::ref_ptr<osg::Depth> _depth;

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;
Expand All @@ -123,11 +120,11 @@ class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
double _lightNear;
double _lightFar;

osg::Matrix _cameraView;
osg::Matrix _cameraProj;
osg::Matrix _cameraView;
osg::Matrix _cameraProj;

unsigned int _splitID;
unsigned int _resolution;
unsigned int _splitID;
unsigned int _resolution;

osg::Uniform* _farDistanceSplit;

Expand Down Expand Up @@ -158,14 +155,18 @@ class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique
double _setMaxFarDistance;
bool _isSetMaxFarDistance;

bool _useFrontCullFace;

double _split_min_near_dist;

double _move_vcam_behind_rcam_factor;

bool _linearSplit;

osg::Light* _userLight;


osg::Uniform* _ambientBiasUniform;
osg::Vec2d _ambientBias;

};
}
#endif

0 comments on commit 1047f97

Please sign in to comment.