Implemented ViewDependentShadowMap::setDebugDraw(bool) to allow osgshadow to enable/disable the debug display via the

standard --debugHUD option.
This commit is contained in:
Robert Osfield 2011-08-09 15:57:37 +00:00
parent c14516d5f6
commit 675a61ea87
3 changed files with 54 additions and 32 deletions

View File

@ -814,6 +814,7 @@ int main(int argc, char** argv)
else if( arguments.read("--vdsm") ) else if( arguments.read("--vdsm") )
{ {
osg::ref_ptr<osgShadow::ViewDependentShadowMap> vdsm = new osgShadow::ViewDependentShadowMap; osg::ref_ptr<osgShadow::ViewDependentShadowMap> vdsm = new osgShadow::ViewDependentShadowMap;
while( arguments.read("--debugHUD") ) vdsm->setDebugDraw( true );
shadowedScene->setShadowTechnique(vdsm.get()); shadowedScene->setShadowTechnique(vdsm.get());
} }
else /* if (arguments.read("--sm")) */ else /* if (arguments.read("--sm")) */
@ -829,8 +830,7 @@ int main(int argc, char** argv)
if( msm )// Set common MSM & LISPSM arguments if( msm )// Set common MSM & LISPSM arguments
{ {
shadowedScene->setShadowTechnique( msm.get() ); shadowedScene->setShadowTechnique( msm.get() );
while( arguments.read("--debugHUD") ) while( arguments.read("--debugHUD") ) msm->setDebugDraw( true );
msm->setDebugDraw( true );
float minLightMargin = 10.f; float minLightMargin = 10.f;
float maxFarPlane = 0; float maxFarPlane = 0;

View File

@ -69,13 +69,17 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
osg::Vec3d frustumCenterLine; osg::Vec3d frustumCenterLine;
}; };
// forward declare
class ViewDependentData;
struct OSGSHADOW_EXPORT LightData : public osg::Referenced struct OSGSHADOW_EXPORT LightData : public osg::Referenced
{ {
LightData(); LightData(ViewDependentData* vdd);
virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix); virtual void setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix);
bool active; ViewDependentData* _viewDependentData;
osg::ref_ptr<osg::RefMatrix> lightMatrix; osg::ref_ptr<osg::RefMatrix> lightMatrix;
osg::ref_ptr<const osg::Light> light; osg::ref_ptr<const osg::Light> light;
@ -92,9 +96,10 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced struct OSGSHADOW_EXPORT ShadowData : public osg::Referenced
{ {
ShadowData(); ShadowData(ViewDependentData* vdd);
bool _active; ViewDependentData* _viewDependentData;
unsigned int _textureUnit; unsigned int _textureUnit;
osg::ref_ptr<osg::Texture2D> _texture; osg::ref_ptr<osg::Texture2D> _texture;
osg::ref_ptr<osg::TexGen> _texgen; osg::ref_ptr<osg::TexGen> _texgen;
@ -107,7 +112,9 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced class OSGSHADOW_EXPORT ViewDependentData : public osg::Referenced
{ {
public: public:
ViewDependentData(); ViewDependentData(ViewDependentShadowMap* vdsm);
const ViewDependentShadowMap* getViewDependentShadowMap() const { return _viewDependentShadowMap; }
LightDataList& getLightDataList() { return _lightDataList; } LightDataList& getLightDataList() { return _lightDataList; }
@ -118,10 +125,12 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
protected: protected:
virtual ~ViewDependentData() {} virtual ~ViewDependentData() {}
ViewDependentShadowMap* _viewDependentShadowMap;
osg::ref_ptr<osg::StateSet> _stateset; osg::ref_ptr<osg::StateSet> _stateset;
LightDataList _lightDataList; LightDataList _lightDataList;
ShadowDataList _shadowDataList; ShadowDataList _shadowDataList;
}; };
virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv); virtual ViewDependentData* createViewDependentData(osgUtil::CullVisitor* cv);
@ -131,7 +140,7 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
virtual void createShaders(); virtual void createShaders();
virtual bool selectActiveLights(osgUtil::CullVisitor* cv, LightDataList& pll) const; virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight); virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight);
@ -145,6 +154,11 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique
virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const; virtual osg::StateSet* selectStateSetForRenderingShadow(ViewDependentData& vdd) const;
void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; }
bool getDebugDraw() const { return _debugDraw; }
protected: protected:
virtual ~ViewDependentShadowMap(); virtual ~ViewDependentShadowMap();
@ -153,6 +167,8 @@ protected:
ViewDependentDataMap _viewDependentDataMap; ViewDependentDataMap _viewDependentDataMap;
osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet; osg::ref_ptr<osg::StateSet> _shadowRecievingPlaceholderStateSet;
bool _debugDraw;
}; };

View File

@ -116,8 +116,8 @@ void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
// //
// LightData // LightData
// //
ViewDependentShadowMap::LightData::LightData(): ViewDependentShadowMap::LightData::LightData(ViewDependentShadowMap::ViewDependentData* vdd):
active(false), _viewDependentData(vdd),
directionalLight(false) directionalLight(false)
{ {
} }
@ -167,11 +167,11 @@ void ViewDependentShadowMap::LightData::setLightData(osg::RefMatrix* lm, const o
// //
// ShadowData // ShadowData
// //
ViewDependentShadowMap::ShadowData::ShadowData(): ViewDependentShadowMap::ShadowData::ShadowData(ViewDependentShadowMap::ViewDependentData* vdd):
_active(false), _viewDependentData(vdd),
_textureUnit(0) _textureUnit(0)
{ {
bool debug = false; bool debug = vdd->getViewDependentShadowMap()->getDebugDraw();
// set up texgen // set up texgen
_texgen = new osg::TexGen; _texgen = new osg::TexGen;
@ -199,14 +199,17 @@ ViewDependentShadowMap::ShadowData::ShadowData():
// the shadow comparison should fail if object is outside the texture // the shadow comparison should fail if object is outside the texture
_texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER); _texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
_texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER); _texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
_texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); //_texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
_texture->setBorderColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f));
// set up the camera // set up the camera
_camera = new osg::Camera; _camera = new osg::Camera;
_camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT); _camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT);
_camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); //_camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
_camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f));
//camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); //camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
// set viewport // set viewport
@ -216,7 +219,7 @@ ViewDependentShadowMap::ShadowData::ShadowData():
if (debug) if (debug)
{ {
// clear just the depth buffer // clear just the depth buffer
_camera->setClearMask(GL_DEPTH_BUFFER_BIT); _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// render after the main camera // render after the main camera
_camera->setRenderOrder(osg::Camera::POST_RENDER); _camera->setRenderOrder(osg::Camera::POST_RENDER);
@ -369,7 +372,8 @@ ViewDependentShadowMap::Frustum::Frustum(osgUtil::CullVisitor* cv):
// //
// ViewDependentData // ViewDependentData
// //
ViewDependentShadowMap::ViewDependentData::ViewDependentData() ViewDependentShadowMap::ViewDependentData::ViewDependentData(ViewDependentShadowMap* vdsm):
_viewDependentShadowMap(vdsm)
{ {
OSG_NOTICE<<"ViewDependentData::ViewDependentData()"<<this<<std::endl; OSG_NOTICE<<"ViewDependentData::ViewDependentData()"<<this<<std::endl;
_stateset = new osg::StateSet; _stateset = new osg::StateSet;
@ -381,13 +385,15 @@ ViewDependentShadowMap::ViewDependentData::ViewDependentData()
// ViewDependentShadowMap // ViewDependentShadowMap
// //
ViewDependentShadowMap::ViewDependentShadowMap(): ViewDependentShadowMap::ViewDependentShadowMap():
ShadowTechnique() ShadowTechnique(),
_debugDraw(false)
{ {
_shadowRecievingPlaceholderStateSet = new osg::StateSet; _shadowRecievingPlaceholderStateSet = new osg::StateSet;
} }
ViewDependentShadowMap::ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop): ViewDependentShadowMap::ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop):
ShadowTechnique(vdsm,copyop) ShadowTechnique(vdsm,copyop),
_debugDraw(vdsm._debugDraw)
{ {
_shadowRecievingPlaceholderStateSet = new osg::StateSet; _shadowRecievingPlaceholderStateSet = new osg::StateSet;
} }
@ -415,7 +421,7 @@ void ViewDependentShadowMap::cleanSceneGraph()
ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::createViewDependentData(osgUtil::CullVisitor* cv) ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::createViewDependentData(osgUtil::CullVisitor* cv)
{ {
return new ViewDependentData(); return new ViewDependentData(this);
} }
ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::getViewDependentData(osgUtil::CullVisitor* cv) ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::getViewDependentData(osgUtil::CullVisitor* cv)
@ -461,22 +467,20 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv)
// 2. select active light sources // 2. select active light sources
// create a list of light sources + their matrices to place them // create a list of light sources + their matrices to place them
LightDataList& pll = vdd->getLightDataList(); selectActiveLights(&cv, vdd);
selectActiveLights(&cv, pll);
unsigned int pos_x = 0; unsigned int pos_x = 0;
unsigned int baseTextureUnit = 0; unsigned int baseTextureUnit = 0;
unsigned int textureUnit = baseTextureUnit; unsigned int textureUnit = baseTextureUnit;
unsigned int numValidShadows = 0; unsigned int numValidShadows = 0;
bool debug = false;
Frustum frustum(&cv); Frustum frustum(&cv);
ShadowDataList& sdl = vdd->getShadowDataList(); ShadowDataList& sdl = vdd->getShadowDataList();
ShadowDataList previous_sdl; ShadowDataList previous_sdl;
previous_sdl.swap(sdl); previous_sdl.swap(sdl);
LightDataList& pll = vdd->getLightDataList();
for(LightDataList::iterator itr = pll.begin(); for(LightDataList::iterator itr = pll.begin();
itr != pll.end(); itr != pll.end();
++itr) ++itr)
@ -493,7 +497,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv)
if (previous_sdl.empty()) if (previous_sdl.empty())
{ {
OSG_NOTICE<<"Create new ShadowData"<<std::endl; OSG_NOTICE<<"Create new ShadowData"<<std::endl;
sd = new ShadowData; sd = new ShadowData(vdd);
} }
else else
{ {
@ -504,10 +508,10 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv)
osg::ref_ptr<osg::Camera> camera = sd->_camera; osg::ref_ptr<osg::Camera> camera = sd->_camera;
if (debug) if (_debugDraw)
{ {
camera->setViewport(pos_x,0,400,400); camera->getViewport()->x() = pos_x;
pos_x += 440; pos_x += camera->getViewport()->width() + 40;
} }
osg::ref_ptr<osg::TexGen> texgen = sd->_texgen; osg::ref_ptr<osg::TexGen> texgen = sd->_texgen;
@ -575,10 +579,12 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv)
} }
bool ViewDependentShadowMap::selectActiveLights(osgUtil::CullVisitor* cv, LightDataList& pll) const bool ViewDependentShadowMap::selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const
{ {
OSG_NOTICE<<"selectActiveLights"<<std::endl; OSG_NOTICE<<"selectActiveLights"<<std::endl;
LightDataList& pll = vdd->getLightDataList();
LightDataList previous_ldl; LightDataList previous_ldl;
previous_ldl.swap(pll); previous_ldl.swap(pll);
@ -606,7 +612,7 @@ bool ViewDependentShadowMap::selectActiveLights(osgUtil::CullVisitor* cv, LightD
if (pll_itr==pll.end()) if (pll_itr==pll.end())
{ {
OSG_NOTICE<<"Light num "<<light->getLightNum()<<std::endl; OSG_NOTICE<<"Light num "<<light->getLightNum()<<std::endl;
LightData* ld = new LightData(); LightData* ld = new LightData(vdd);
ld->setLightData(itr->second, light, modelViewMatrix); ld->setLightData(itr->second, light, modelViewMatrix);
pll.push_back(ld); pll.push_back(ld);
} }