From 4c31b38f773b8252262f29961d51c1d856eadad7 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 26 Feb 2018 14:58:12 +0000 Subject: [PATCH] Move CLSB changes to new shadow technique --- components/sceneutil/mwshadowtechnique.cpp | 219 +++++++++++---------- components/sceneutil/mwshadowtechnique.hpp | 27 +++ 2 files changed, 137 insertions(+), 109 deletions(-) diff --git a/components/sceneutil/mwshadowtechnique.cpp b/components/sceneutil/mwshadowtechnique.cpp index e8543c8b4..522172f42 100644 --- a/components/sceneutil/mwshadowtechnique.cpp +++ b/components/sceneutil/mwshadowtechnique.cpp @@ -293,122 +293,123 @@ void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) } } - -class ComputeLightSpaceBounds : public osg::NodeVisitor, public osg::CullStack +MWShadowTechnique::ComputeLightSpaceBounds::ComputeLightSpaceBounds(osg::Viewport* viewport, const osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix) : + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN) { -public: - ComputeLightSpaceBounds(osg::Viewport* viewport, const osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix): - osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN) - { - setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING); + setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING); - pushViewport(viewport); - pushProjectionMatrix(new osg::RefMatrix(projectionMatrix)); - pushModelViewMatrix(new osg::RefMatrix(viewMatrix),osg::Transform::ABSOLUTE_RF); + pushViewport(viewport); + pushProjectionMatrix(new osg::RefMatrix(projectionMatrix)); + pushModelViewMatrix(new osg::RefMatrix(viewMatrix), osg::Transform::ABSOLUTE_RF); +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Node& node) +{ + if (isCulled(node)) return; + + // push the culling mode. + pushCurrentMask(); + + traverse(node); + + // pop the culling mode. + popCurrentMask(); +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Drawable& drawable) +{ + if (isCulled(drawable)) return; + + // push the culling mode. + pushCurrentMask(); + + updateBound(drawable.getBoundingBox()); + + // pop the culling mode. + popCurrentMask(); +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(Terrain::QuadTreeWorld & quadTreeWorld) +{ + // For now, just expand the bounds fully as terrain will fill them up and possible ways to detect which terrain definitely won't cast shadows aren't implemented. + + update(osg::Vec3(-1.0, -1.0, 0.0)); + update(osg::Vec3(1.0, 1.0, 0.0)); +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Billboard&) +{ + OSG_INFO << "Warning Billboards not yet supported" << std::endl; + return; +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Projection&) +{ + // projection nodes won't affect a shadow map so their subgraphs should be ignored + return; +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Transform& transform) +{ + if (isCulled(transform)) return; + + // push the culling mode. + pushCurrentMask(); + + // absolute transforms won't affect a shadow map so their subgraphs should be ignored. + if (transform.getReferenceFrame() == osg::Transform::RELATIVE_RF) + { + osg::ref_ptr matrix = new osg::RefMatrix(*getModelViewMatrix()); + transform.computeLocalToWorldMatrix(*matrix, this); + pushModelViewMatrix(matrix.get(), transform.getReferenceFrame()); + + traverse(transform); + + popModelViewMatrix(); } - void apply(osg::Node& node) + // pop the culling mode. + popCurrentMask(); + +} + +void MWShadowTechnique::ComputeLightSpaceBounds::apply(osg::Camera&) +{ + // camera nodes won't affect a shadow map so their subgraphs should be ignored + return; +} + +void MWShadowTechnique::ComputeLightSpaceBounds::updateBound(const osg::BoundingBox& bb) +{ + if (!bb.valid()) return; + + const osg::Matrix& matrix = *getModelViewMatrix() * *getProjectionMatrix(); + + update(bb.corner(0) * matrix); + update(bb.corner(1) * matrix); + update(bb.corner(2) * matrix); + update(bb.corner(3) * matrix); + update(bb.corner(4) * matrix); + update(bb.corner(5) * matrix); + update(bb.corner(6) * matrix); + update(bb.corner(7) * matrix); +} + +void MWShadowTechnique::ComputeLightSpaceBounds::update(const osg::Vec3& v) +{ + if (v.z()<-1.0f) { - if (isCulled(node)) return; - - // push the culling mode. - pushCurrentMask(); - - traverse(node); - - // pop the culling mode. - popCurrentMask(); - } - - void apply(osg::Drawable& drawable) - { - if (isCulled(drawable)) return; - - // push the culling mode. - pushCurrentMask(); - - updateBound(drawable.getBoundingBox()); - - // pop the culling mode. - popCurrentMask(); - } - - void apply(osg::Billboard&) - { - OSG_INFO<<"Warning Billboards not yet supported"< matrix = new osg::RefMatrix(*getModelViewMatrix()); - transform.computeLocalToWorldMatrix(*matrix,this); - pushModelViewMatrix(matrix.get(), transform.getReferenceFrame()); - - traverse(transform); - - popModelViewMatrix(); - } - - // pop the culling mode. - popCurrentMask(); - - } - - void apply(osg::Camera&) - { - // camera nodes won't affect a shadow map so their subgraphs should be ignored - return; - } - - void updateBound(const osg::BoundingBox& bb) - { - if (!bb.valid()) return; - - const osg::Matrix& matrix = *getModelViewMatrix() * *getProjectionMatrix(); - - update(bb.corner(0) * matrix); - update(bb.corner(1) * matrix); - update(bb.corner(2) * matrix); - update(bb.corner(3) * matrix); - update(bb.corner(4) * matrix); - update(bb.corner(5) * matrix); - update(bb.corner(6) * matrix); - update(bb.corner(7) * matrix); - } - - void update(const osg::Vec3& v) - { - if (v.z()<-1.0f) - { - //OSG_NOTICE<<"discarding("<1.0f) x=1.0f; - float y = v.y(); - if (y<-1.0f) y=-1.0f; - if (y>1.0f) y=1.0f; - _bb.expandBy(osg::Vec3(x,y,v.z())); - } - - osg::BoundingBox _bb; -}; + float x = v.x(); + if (x<-1.0f) x = -1.0f; + if (x>1.0f) x = 1.0f; + float y = v.y(); + if (y<-1.0f) y = -1.0f; + if (y>1.0f) y = 1.0f; + _bb.expandBy(osg::Vec3(x, y, v.z())); +} /////////////////////////////////////////////////////////////////////////////////////////////// // diff --git a/components/sceneutil/mwshadowtechnique.hpp b/components/sceneutil/mwshadowtechnique.hpp index e7465ff39..7cf7db155 100644 --- a/components/sceneutil/mwshadowtechnique.hpp +++ b/components/sceneutil/mwshadowtechnique.hpp @@ -27,6 +27,8 @@ #include +#include + namespace SceneUtil { /** ViewDependentShadowMap provides an base implementation of view dependent shadow mapping techniques.*/ @@ -59,6 +61,31 @@ namespace SceneUtil { /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/ virtual void cleanSceneGraph(); + class ComputeLightSpaceBounds : public osg::NodeVisitor, public osg::CullStack + { + public: + ComputeLightSpaceBounds(osg::Viewport* viewport, const osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix); + + void apply(osg::Node& node); + + void apply(osg::Drawable& drawable); + + void apply(Terrain::QuadTreeWorld& quadTreeWorld); + + void apply(osg::Billboard&); + + void apply(osg::Projection&); + + void apply(osg::Transform& transform); + + void apply(osg::Camera&); + + void updateBound(const osg::BoundingBox& bb); + + void update(const osg::Vec3& v); + + osg::BoundingBox _bb; + }; struct OSGSHADOW_EXPORT Frustum {