diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index c5fcd8a31..be9ef0c1f 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -664,6 +664,9 @@ int main( int argc, char **argv ) double sampleDensityWhenMoving = 0.0; while(arguments.read("--sdwm", sampleDensityWhenMoving)) {} + double sampleRatioWhenMoving = 0.0; + while(arguments.read("--srwm", sampleRatioWhenMoving)) {} + while(arguments.read("--lod")) { sampleDensityWhenMoving = 0.02; } double sequenceLength = 10.0; @@ -1075,6 +1078,7 @@ int main( int argc, char **argv ) // use SampleRatio in place of SampleDensity osgVolume::SampleRatioProperty* sr = new osgVolume::SampleRatioProperty(1.0f); + osgVolume::SampleRatioWhenMovingProperty* srwm = sampleRatioWhenMoving!=0.0 ? new osgVolume::SampleRatioWhenMovingProperty(sampleRatioWhenMoving) : 0; osgVolume::TransparencyProperty* tp = new osgVolume::TransparencyProperty(1.0); osgVolume::TransferFunctionProperty* tfp = transferFunction.valid() ? new osgVolume::TransferFunctionProperty(transferFunction.get()) : 0; @@ -1083,11 +1087,18 @@ int main( int argc, char **argv ) // Standard osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; cp->addProperty(ap); - if (useMultipass) cp->addProperty(sr); - else cp->addProperty(sd); + if (useMultipass) + { + cp->addProperty(sr); + if (srwm) cp->addProperty(srwm); + } + else + { + cp->addProperty(sd); + if (sdwm) cp->addProperty(sdwm); + } cp->addProperty(tp); - if (sdwm) cp->addProperty(sdwm); if (tfp) { OSG_NOTICE<<"Adding TransferFunction"< _tfProperty; @@ -392,6 +413,7 @@ class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisi osg::ref_ptr _sampleDensityProperty; osg::ref_ptr _sampleDensityWhenMovingProperty; osg::ref_ptr _sampleRatioProperty; + osg::ref_ptr _sampleRatioWhenMovingProperty; osg::ref_ptr _transparencyProperty; }; diff --git a/include/osgVolume/RayTracedTechnique b/include/osgVolume/RayTracedTechnique index 5491aa03d..c90cd56db 100644 --- a/include/osgVolume/RayTracedTechnique +++ b/include/osgVolume/RayTracedTechnique @@ -47,11 +47,6 @@ class OSGVOLUME_EXPORT RayTracedTechnique : public VolumeTechnique osg::ref_ptr _transform; - typedef std::map ModelViewMatrixMap; - - OpenThreads::Mutex _mutex; - ModelViewMatrixMap _modelViewMatrixMap; - osg::ref_ptr _whenMovingStateSet; }; diff --git a/include/osgVolume/VolumeTechnique b/include/osgVolume/VolumeTechnique index 814ae7abe..dec084ad3 100644 --- a/include/osgVolume/VolumeTechnique +++ b/include/osgVolume/VolumeTechnique @@ -43,6 +43,8 @@ class OSGVOLUME_EXPORT VolumeTechnique : public osg::Object virtual void update(osgUtil::UpdateVisitor* nv); + virtual bool isMoving(osgUtil::CullVisitor* nv); + virtual void cull(osgUtil::CullVisitor* nv); /** Clean scene graph from any terrain technique specific nodes.*/ @@ -61,6 +63,9 @@ class OSGVOLUME_EXPORT VolumeTechnique : public osg::Object VolumeTile* _volumeTile; + typedef std::map ModelViewMatrixMap; + OpenThreads::Mutex _mutex; + ModelViewMatrixMap _modelViewMatrixMap; }; } diff --git a/src/osgVolume/MultipassTechnique.cpp b/src/osgVolume/MultipassTechnique.cpp index 2ae1f0097..4304c2163 100644 --- a/src/osgVolume/MultipassTechnique.cpp +++ b/src/osgVolume/MultipassTechnique.cpp @@ -419,6 +419,12 @@ void MultipassTechnique::init() } + if (cpv._sampleRatioWhenMovingProperty.valid()) + { + _whenMovingStateSet = new osg::StateSet; + _whenMovingStateSet->addUniform(cpv._sampleRatioWhenMovingProperty->getUniform(), osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); + } + } void MultipassTechnique::update(osgUtil::UpdateVisitor* /*uv*/) @@ -435,8 +441,18 @@ void MultipassTechnique::cull(osgUtil::CullVisitor* cv) if (postTraversal) { - // OSG_NOTICE<<" OK need to handle postTraversal"<accept(*cv); + if (_whenMovingStateSet.valid() && isMoving(cv)) + { + OSG_NOTICE<<"Using MovingStateSet"<pushStateSet(_whenMovingStateSet.get()); + _transform->accept(*cv); + cv->popStateSet(); + } + else + { + OSG_NOTICE<<"NOT using MovingStateSet"<accept(*cv); + } } else { diff --git a/src/osgVolume/Property.cpp b/src/osgVolume/Property.cpp index 920bac246..6639ec9c6 100644 --- a/src/osgVolume/Property.cpp +++ b/src/osgVolume/Property.cpp @@ -207,6 +207,20 @@ SampleRatioProperty::SampleRatioProperty(const SampleRatioProperty& srp,const os { } +///////////////////////////////////////////////////////////////////////////// +// +// SampleRatioWhenMovingProperty +// +SampleRatioWhenMovingProperty::SampleRatioWhenMovingProperty(float value): + ScalarProperty("SampleRatioValue",value) +{ +} + +SampleRatioWhenMovingProperty::SampleRatioWhenMovingProperty(const SampleRatioWhenMovingProperty& isp,const osg::CopyOp& copyop): + ScalarProperty(isp, copyop) +{ +} + ///////////////////////////////////////////////////////////////////////////// @@ -278,6 +292,7 @@ void CollectPropertiesVisitor::apply(LightingProperty& lp) { _lightingProperty = void CollectPropertiesVisitor::apply(SampleDensityProperty& sdp) { _sampleDensityProperty = &sdp; } void CollectPropertiesVisitor::apply(SampleDensityWhenMovingProperty& sdp) { _sampleDensityWhenMovingProperty = &sdp; } void CollectPropertiesVisitor::apply(SampleRatioProperty& srp) { _sampleRatioProperty = &srp; } +void CollectPropertiesVisitor::apply(SampleRatioWhenMovingProperty& srp) { _sampleRatioWhenMovingProperty = &srp; } void CollectPropertiesVisitor::apply(TransparencyProperty& tp) { _transparencyProperty = &tp; } @@ -449,6 +464,13 @@ bool PropertyAdjustmentCallback::handle(const osgGA::GUIEventAdapter& ea,osgGA:: OSG_NOTICE<<"Setting sample ratio to "<setValue(sampleRatio); } + + if (_updateSampleDensity && cpv._sampleRatioWhenMovingProperty.valid()) + { + float sampleRatio = v2*4; + OSG_NOTICE<<"Setting sample ratio to "<setValue(sampleRatio); + } } diff --git a/src/osgVolume/RayTracedTechnique.cpp b/src/osgVolume/RayTracedTechnique.cpp index 167015ab6..31f8ea1a5 100644 --- a/src/osgVolume/RayTracedTechnique.cpp +++ b/src/osgVolume/RayTracedTechnique.cpp @@ -498,36 +498,11 @@ void RayTracedTechnique::cull(osgUtil::CullVisitor* cv) { if (!_transform.valid()) return; - if (_whenMovingStateSet.valid()) + if (_whenMovingStateSet.valid() && isMoving(cv)) { - bool moving = false; - { - OpenThreads::ScopedLock lock(_mutex); - ModelViewMatrixMap::iterator itr = _modelViewMatrixMap.find(cv->getIdentifier()); - if (itr!=_modelViewMatrixMap.end()) - { - osg::Matrix newModelViewMatrix = *(cv->getModelViewMatrix()); - osg::Matrix& previousModelViewMatrix = itr->second; - moving = (newModelViewMatrix != previousModelViewMatrix); - - previousModelViewMatrix = newModelViewMatrix; - } - else - { - _modelViewMatrixMap[cv->getIdentifier()] = *(cv->getModelViewMatrix()); - } - } - - if (moving) - { - cv->pushStateSet(_whenMovingStateSet.get()); - _transform->accept(*cv); - cv->popStateSet(); - } - else - { - _transform->accept(*cv); - } + cv->pushStateSet(_whenMovingStateSet.get()); + _transform->accept(*cv); + cv->popStateSet(); } else { diff --git a/src/osgVolume/VolumeTechnique.cpp b/src/osgVolume/VolumeTechnique.cpp index 6960c1d29..b4a6860b1 100644 --- a/src/osgVolume/VolumeTechnique.cpp +++ b/src/osgVolume/VolumeTechnique.cpp @@ -86,3 +86,27 @@ void VolumeTechnique::traverse(osg::NodeVisitor& nv) // otherwise fallback to the Group::traverse() _volumeTile->osg::Group::traverse(nv); } + +bool VolumeTechnique::isMoving(osgUtil::CullVisitor* cv) +{ + bool moving = false; + + OpenThreads::ScopedLock lock(_mutex); + + OSG_NOTICE<<"cv="<getIdentifier()="<getIdentifier()<getIdentifier()); + if (itr!=_modelViewMatrixMap.end()) + { + osg::Matrix newModelViewMatrix = *(cv->getModelViewMatrix()); + osg::Matrix& previousModelViewMatrix = itr->second; + moving = (newModelViewMatrix != previousModelViewMatrix); + + previousModelViewMatrix = newModelViewMatrix; + } + else + { + _modelViewMatrixMap[cv->getIdentifier()] = *(cv->getModelViewMatrix()); + } + return moving; +}