From a40aa3a7346624c2833d8c3c7358d8a1f67372fc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 20 Jan 2009 17:41:45 +0000 Subject: [PATCH] Moved PropertyAdjustmentCallback from osgvolume.cpp into osgVolume. Updated lighting shaders. --- examples/osgvolume/osgvolume.cpp | 118 ++----------------- include/osgVolume/Property | 31 +++++ src/osgVolume/CMakeLists.txt | 1 + src/osgVolume/Layer.cpp | 1 + src/osgVolume/Property.cpp | 81 +++++++++++++ src/osgVolume/ShaderTechnique.cpp | 3 +- src/osgVolume/Shaders/volume_lit_frag.cpp | 2 + src/osgVolume/Shaders/volume_lit_tf_frag.cpp | 2 +- 8 files changed, 128 insertions(+), 111 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 926764cd8..efda6fe1f 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -742,110 +742,6 @@ osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename) } -namespace osgVolume -{ - -class PropertyAdjustmentCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback -{ - public: - - PropertyAdjustmentCallback() - { - _transparencyKey = 't'; - _alphaFuncKey = 'a'; - _sampleDensityKey = 'd'; - - _updateTransparency = false; - _updateAlphaCutOff = false; - _updateSampleDensity = false; - } - - PropertyAdjustmentCallback(const PropertyAdjustmentCallback&,const osg::CopyOp&) {} - - META_Object(osgVolume,PropertyAdjustmentCallback); - - void setKeyEventActivatesTransparenyAdjustment(int key) { _transparencyKey = key; } - int getKeyEventActivatesTransparenyAdjustment() const { return _transparencyKey; } - - void setKeyEventActivatesSampleDensityAdjustment(int key) { _sampleDensityKey = key; } - int getKeyEventActivatesSampleAdjustment() const { return _sampleDensityKey; } - - void setKeyEventActivatesAlphaFuncAdjustment(int key) { _alphaFuncKey = key; } - int getKeyEventActivatesAlphaFuncAdjustment() const { return _alphaFuncKey; } - - 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()); - float v2 = v*v; - float v4 = v2*v2; - - if (_updateAlphaCutOff && cpv._isoProperty.valid()) - { - osg::notify(osg::NOTICE)<<"Setting isoProperty to "<setValue(v); - } - - if (_updateAlphaCutOff && cpv._afProperty.valid()) - { - osg::notify(osg::NOTICE)<<"Setting afProperty to "<setValue(v2); - } - - if (_updateTransparency && cpv._transparencyProperty.valid()) - { - osg::notify(osg::NOTICE)<<"Setting transparency to "<setValue(v2); - } - - if (_updateSampleDensity && cpv._sampleDensityProperty.valid()) - { - osg::notify(osg::NOTICE)<<"Setting sample density to "<setValue(v4); - } - } - 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; - } - - int _transparencyKey; - int _alphaFuncKey; - int _sampleDensityKey; - - bool _updateTransparency; - bool _updateAlphaCutOff; - bool _updateSampleDensity; -}; - -} class TestSupportOperation: public osg::GraphicsOperation { public: @@ -971,10 +867,9 @@ int main( int argc, char **argv ) ShadingModel shadingModel = Standard; while(arguments.read("--mip")) shadingModel = MaximumIntensityProjection; - while (arguments.read("--isosurface")) - { - shadingModel = Isosurface; - } + while (arguments.read("--isosurface")) shadingModel = Isosurface; + + while (arguments.read("--light")) shadingModel = Light; float xSize=1.0f, ySize=1.0f, zSize=1.0f; while (arguments.read("--xSize",xSize)) {} @@ -1362,7 +1257,12 @@ int main( int argc, char **argv ) volume->addChild(tile); osg::ref_ptr layer = new osgVolume::ImageLayer(image_3d); - layer->setProperty(new osgVolume::TransferFunctionProperty(transferFunction.get())); + + if (transferFunction.valid()) + { + osg::notify(osg::NOTICE)<<"Attaching transferFunction"<addProperty(new osgVolume::TransferFunctionProperty(transferFunction.get())); + } if (matrix) { diff --git a/include/osgVolume/Property b/include/osgVolume/Property index d5490ade0..babad9e53 100644 --- a/include/osgVolume/Property +++ b/include/osgVolume/Property @@ -18,6 +18,8 @@ #include #include +#include + #include namespace osgVolume { @@ -316,6 +318,35 @@ class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisi }; +class PropertyAdjustmentCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback +{ + public: + + PropertyAdjustmentCallback(); + + PropertyAdjustmentCallback(const PropertyAdjustmentCallback&,const osg::CopyOp&) {} + + META_Object(osgVolume,PropertyAdjustmentCallback); + + void setKeyEventActivatesTransparenyAdjustment(int key) { _transparencyKey = key; } + int getKeyEventActivatesTransparenyAdjustment() const { return _transparencyKey; } + + void setKeyEventActivatesSampleDensityAdjustment(int key) { _sampleDensityKey = key; } + int getKeyEventActivatesSampleAdjustment() const { return _sampleDensityKey; } + + void setKeyEventActivatesAlphaFuncAdjustment(int key) { _alphaFuncKey = key; } + int getKeyEventActivatesAlphaFuncAdjustment() const { return _alphaFuncKey; } + + virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*); + + int _transparencyKey; + int _alphaFuncKey; + int _sampleDensityKey; + + bool _updateTransparency; + bool _updateAlphaCutOff; + bool _updateSampleDensity; +}; } #endif diff --git a/src/osgVolume/CMakeLists.txt b/src/osgVolume/CMakeLists.txt index d6159299a..f65d02eba 100644 --- a/src/osgVolume/CMakeLists.txt +++ b/src/osgVolume/CMakeLists.txt @@ -40,6 +40,7 @@ SET(TARGET_LIBRARIES_VARS FREETYPE_LIBRARY ) LINK_INTERNAL(${LIB_NAME} osgUtil osgDB + osgGA osg OpenThreads ) diff --git a/src/osgVolume/Layer.cpp b/src/osgVolume/Layer.cpp index 35825771f..87b7958e8 100644 --- a/src/osgVolume/Layer.cpp +++ b/src/osgVolume/Layer.cpp @@ -71,6 +71,7 @@ void Layer::addProperty(Property* property) { cp = new CompositeProperty; cp->addProperty(property); + cp->addProperty(_property.get()); _property = cp; } } diff --git a/src/osgVolume/Property.cpp b/src/osgVolume/Property.cpp index e2b34efcd..04e27b077 100644 --- a/src/osgVolume/Property.cpp +++ b/src/osgVolume/Property.cpp @@ -12,6 +12,7 @@ */ #include +#include using namespace osgVolume; @@ -201,3 +202,83 @@ void CollectPropertiesVisitor::apply(MaximumIntensityProjectionProperty& mip) { void CollectPropertiesVisitor::apply(LightingProperty& lp) { _lightingProperty = &lp; } void CollectPropertiesVisitor::apply(SampleDensityProperty& sdp) { _sampleDensityProperty = &sdp; } void CollectPropertiesVisitor::apply(TransparencyProperty& tp) { _transparencyProperty = &tp; } + + +///////////////////////////////////////////////////////////////////////////// +// +// PropertyAdjustmentCallback +// +PropertyAdjustmentCallback::PropertyAdjustmentCallback() +{ + _transparencyKey = 't'; + _alphaFuncKey = 'a'; + _sampleDensityKey = 'd'; + + _updateTransparency = false; + _updateAlphaCutOff = false; + _updateSampleDensity = false; +} + +bool PropertyAdjustmentCallback::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()); + float v2 = v*v; + float v4 = v2*v2; + + if (_updateAlphaCutOff && cpv._isoProperty.valid()) + { + osg::notify(osg::NOTICE)<<"Setting isoProperty to "<setValue(v); + } + + if (_updateAlphaCutOff && cpv._afProperty.valid()) + { + osg::notify(osg::NOTICE)<<"Setting afProperty to "<setValue(v2); + } + + if (_updateTransparency && cpv._transparencyProperty.valid()) + { + osg::notify(osg::NOTICE)<<"Setting transparency to "<setValue(v2); + } + + if (_updateSampleDensity && cpv._sampleDensityProperty.valid()) + { + osg::notify(osg::NOTICE)<<"Setting sample density to "<setValue(v4); + } + } + 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; +} + diff --git a/src/osgVolume/ShaderTechnique.cpp b/src/osgVolume/ShaderTechnique.cpp index 7a9ac4445..376e4e98b 100644 --- a/src/osgVolume/ShaderTechnique.cpp +++ b/src/osgVolume/ShaderTechnique.cpp @@ -104,7 +104,6 @@ void ShaderTechnique::init() if (_volumeTile->getLayer() && !masterLocator) { masterLocator = _volumeTile->getLayer()->getLocator(); - osg::notify(osg::NOTICE)<<"assigning locator = "<setImage(tf->getImage()); texture1D->setResizeNonPowerOfTwoHint(false); diff --git a/src/osgVolume/Shaders/volume_lit_frag.cpp b/src/osgVolume/Shaders/volume_lit_frag.cpp index 6af82cf63..647ab5217 100644 --- a/src/osgVolume/Shaders/volume_lit_frag.cpp +++ b/src/osgVolume/Shaders/volume_lit_frag.cpp @@ -11,6 +11,8 @@ char volume_lit_frag[] = "uniform sampler3D baseTexture;\n" "{ \n" " vec3 t0 = (texgen * vertexPos).xyz;\n" " vec3 te = (texgen * cameraPos).xyz;\n" + " \n" + " vec3 eyeDirection = normalize(te-t0);\n" "\n" " if (te.x>=0.0 && te.x<=1.0 &&\n" " te.y>=0.0 && te.y<=1.0 &&\n" diff --git a/src/osgVolume/Shaders/volume_lit_tf_frag.cpp b/src/osgVolume/Shaders/volume_lit_tf_frag.cpp index 4bf1c803a..9d7ab6265 100644 --- a/src/osgVolume/Shaders/volume_lit_tf_frag.cpp +++ b/src/osgVolume/Shaders/volume_lit_tf_frag.cpp @@ -83,7 +83,7 @@ char volume_lit_tf_frag[] = "uniform sampler3D baseTexture;\n" " float v = texture3D( baseTexture, texcoord).a;\n" " vec4 color = texture1D( tfTexture, v);\n" "\n" - " float a = color.a;\n" + " float a = v;\n" " float px = texture3D( baseTexture, texcoord + deltaX).a;\n" " float py = texture3D( baseTexture, texcoord + deltaY).a;\n" " float pz = texture3D( baseTexture, texcoord + deltaZ).a;\n"