From 894b161878a6b9cde6712e675dab1a10612c8900 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 17 Jan 2009 17:23:47 +0000 Subject: [PATCH] Updated event callback to be able to update iso surface value. --- examples/osgvolume/osgvolume.cpp | 179 ++++++++++++------------------- 1 file changed, 71 insertions(+), 108 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 08046042b..bcba2bec5 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -457,114 +457,6 @@ osg::Image* createTexture3D(ImageList& imageList, ProcessRow& processRow, } -class FollowMouseCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback -{ - public: - - FollowMouseCallback(bool shader = false): - _shader(shader) - { - _updateTransparency = false; - _updateAlphaCutOff = false; - _updateSampleDensity = false; - } - - FollowMouseCallback(const FollowMouseCallback&,const osg::CopyOp&) {} - - META_Object(osg,FollowMouseCallback); - - virtual void operator() (osg::StateSet* stateset, osg::NodeVisitor* nv) - { - if (nv->getVisitorType()==osg::NodeVisitor::EVENT_VISITOR) - { - osgGA::EventVisitor* ev = dynamic_cast(nv); - if (ev) - { - osgGA::GUIActionAdapter* aa = ev->getActionAdapter(); - osgGA::EventQueue::Events& events = ev->getEvents(); - for(osgGA::EventQueue::Events::iterator itr=events.begin(); - itr!=events.end(); - ++itr) - { - handle(*(*itr), *aa, stateset, ev); - } - } - } - } - - virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*) - { - osg::StateSet* stateset = dynamic_cast(object); - if (!stateset) return false; - - switch(ea.getEventType()) - { - case(osgGA::GUIEventAdapter::MOVE): - case(osgGA::GUIEventAdapter::DRAG): - { - float v = (ea.getY()-ea.getYmin())/(ea.getYmax()-ea.getYmin()); - if (_shader) - { - osg::Uniform* uniform = 0; - if (_updateTransparency && (uniform = stateset->getUniform("transparency"))) uniform->set(v); - if (_updateAlphaCutOff && (uniform = stateset->getUniform("alphaCutOff"))) uniform->set(v); - if (_updateSampleDensity && (uniform = stateset->getUniform("sampleDensity"))) - { - float value = powf(v,5); - osg::notify(osg::INFO)<<"sampleDensity = "<set(value); - } - } - else - { - if (_updateAlphaCutOff) - { - osg::AlphaFunc* alphaFunc = dynamic_cast(stateset->getAttribute(osg::StateAttribute::ALPHAFUNC)); - if (alphaFunc) - { - alphaFunc->setReferenceValue(v); - } - } - - if (_updateTransparency) - { - osg::Material* material = dynamic_cast(stateset->getAttribute(osg::StateAttribute::MATERIAL)); - if (material) - { - material->setAlpha(osg::Material::FRONT_AND_BACK,v); - } - } - } - - break; - } - case(osgGA::GUIEventAdapter::KEYDOWN): - { - if (ea.getKey()=='t') _updateTransparency = true; - if (ea.getKey()=='a') _updateAlphaCutOff = true; - if (ea.getKey()=='d') _updateSampleDensity = true; - break; - } - case(osgGA::GUIEventAdapter::KEYUP): - { - if (ea.getKey()=='t') _updateTransparency = false; - if (ea.getKey()=='a') _updateAlphaCutOff = false; - if (ea.getKey()=='d') _updateSampleDensity = false; - break; - } - default: - break; - } - return false; - } - - bool _shader; - bool _updateTransparency; - bool _updateAlphaCutOff; - bool _updateSampleDensity; - -}; - struct ScaleOperator { ScaleOperator():_scale(1.0f) {} @@ -850,6 +742,75 @@ osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename) } +class FollowMouseCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback +{ + public: + + FollowMouseCallback() + { + _updateTransparency = false; + _updateAlphaCutOff = false; + _updateSampleDensity = false; + } + + FollowMouseCallback(const FollowMouseCallback&,const osg::CopyOp&) {} + + META_Object(osg,FollowMouseCallback); + + virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*) + { + osgVolume::VolumeTile* tile = dynamic_cast(object); + osgVolume::Layer* layer = tile ? tile->getLayer() : 0; + osgVolume::Property* property = layer ? layer->getProperty() : 0; + if (!property) return false; + + osgVolume::CollectPropertiesVisitor cpv; + property->accept(cpv); + + switch(ea.getEventType()) + { + case(osgGA::GUIEventAdapter::MOVE): + case(osgGA::GUIEventAdapter::DRAG): + { + float v = (ea.getY()-ea.getYmin())/(ea.getYmax()-ea.getYmin()); + + if (_updateAlphaCutOff && cpv._isoProperty.valid()) + { + osg::notify(osg::NOTICE)<<"Setting isoProperty to "<setValue(v); + } + + if (_updateAlphaCutOff && cpv._afProperty.valid()) + { + cpv._afProperty->setValue(v); + } + } + case(osgGA::GUIEventAdapter::KEYDOWN): + { + if (ea.getKey()=='t') _updateTransparency = true; + if (ea.getKey()=='a') _updateAlphaCutOff = true; + if (ea.getKey()=='d') _updateSampleDensity = true; + break; + } + case(osgGA::GUIEventAdapter::KEYUP): + { + if (ea.getKey()=='t') _updateTransparency = false; + if (ea.getKey()=='a') _updateAlphaCutOff = false; + if (ea.getKey()=='d') _updateSampleDensity = false; + break; + } + default: + break; + } + return false; + } + + bool _updateTransparency; + bool _updateAlphaCutOff; + bool _updateSampleDensity; +}; + + class TestSupportOperation: public osg::GraphicsOperation { public: @@ -1377,6 +1338,8 @@ int main( int argc, char **argv ) tile->setLayer(layer.get()); + tile->setEventCallback(new FollowMouseCallback()); + if (useShader) { switch(shadingModel)