From 1a00a2ce3c95ac238f17939114aed6f8e3d62bd8 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 14 Jan 2009 20:32:06 +0000 Subject: [PATCH] Added osgVolume .osg plugin --- src/osgPlugins/CMakeLists.txt | 1 + src/osgPlugins/osgVolume/CMakeLists.txt | 16 ++ .../osgVolume/FixedFunctionTechnique.cpp | 40 ++++ src/osgPlugins/osgVolume/ImageLayer.cpp | 73 ++++++++ src/osgPlugins/osgVolume/Layer.cpp | 56 ++++++ src/osgPlugins/osgVolume/Locator.cpp | 85 +++++++++ src/osgPlugins/osgVolume/ShaderTechnique.cpp | 40 ++++ src/osgPlugins/osgVolume/Volume.cpp | 56 ++++++ src/osgPlugins/osgVolume/VolumeTile.cpp | 177 ++++++++++++++++++ 9 files changed, 544 insertions(+) create mode 100644 src/osgPlugins/osgVolume/CMakeLists.txt create mode 100644 src/osgPlugins/osgVolume/FixedFunctionTechnique.cpp create mode 100644 src/osgPlugins/osgVolume/ImageLayer.cpp create mode 100644 src/osgPlugins/osgVolume/Layer.cpp create mode 100644 src/osgPlugins/osgVolume/Locator.cpp create mode 100644 src/osgPlugins/osgVolume/ShaderTechnique.cpp create mode 100644 src/osgPlugins/osgVolume/Volume.cpp create mode 100644 src/osgPlugins/osgVolume/VolumeTile.cpp diff --git a/src/osgPlugins/CMakeLists.txt b/src/osgPlugins/CMakeLists.txt index e8c6b9a3d..ea6c03385 100644 --- a/src/osgPlugins/CMakeLists.txt +++ b/src/osgPlugins/CMakeLists.txt @@ -44,6 +44,7 @@ ADD_SUBDIRECTORY(osgText) ADD_SUBDIRECTORY(osgViewer) ADD_SUBDIRECTORY(osgShadow) ADD_SUBDIRECTORY(osgTerrain) +ADD_SUBDIRECTORY(osgVolume) ADD_SUBDIRECTORY(osgWidget) ADD_SUBDIRECTORY(osga) ADD_SUBDIRECTORY(rot) diff --git a/src/osgPlugins/osgVolume/CMakeLists.txt b/src/osgPlugins/osgVolume/CMakeLists.txt new file mode 100644 index 000000000..6a37b5b62 --- /dev/null +++ b/src/osgPlugins/osgVolume/CMakeLists.txt @@ -0,0 +1,16 @@ +SET(TARGET_SRC + Locator.cpp + ImageLayer.cpp + Layer.cpp + Volume.cpp + VolumeTile.cpp + ShaderTechnique.cpp + FixedFunctionTechnique.cpp +) + +SET(TARGET_ADDED_LIBRARIES osgVolume ) + +#### end var setup ### +SETUP_PLUGIN(osgvolume) + + diff --git a/src/osgPlugins/osgVolume/FixedFunctionTechnique.cpp b/src/osgPlugins/osgVolume/FixedFunctionTechnique.cpp new file mode 100644 index 000000000..83a4f4c04 --- /dev/null +++ b/src/osgPlugins/osgVolume/FixedFunctionTechnique.cpp @@ -0,0 +1,40 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +bool FixedFunctionTechnique_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool FixedFunctionTechnique_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy FixedFunctionTechnique_Proxy +( + new osgVolume::FixedFunctionTechnique, + "FixedFunctionTechnique", + "FixedFunctionTechnique Object", + FixedFunctionTechnique_readLocalData, + FixedFunctionTechnique_writeLocalData +); + + +bool FixedFunctionTechnique_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + //osgVolume::FixedFunctionTechnique& fft = static_cast(obj); + bool itrAdvanced = false; + return itrAdvanced; +} + +bool FixedFunctionTechnique_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + //const osgVolume::FixedFunctionTechnique& fft = static_cast(obj); + return true; +} diff --git a/src/osgPlugins/osgVolume/ImageLayer.cpp b/src/osgPlugins/osgVolume/ImageLayer.cpp new file mode 100644 index 000000000..c7c71867f --- /dev/null +++ b/src/osgPlugins/osgVolume/ImageLayer.cpp @@ -0,0 +1,73 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +bool ImageLayer_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool ImageLayer_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy ImageLayer_Proxy +( + new osgVolume::ImageLayer, + "ImageLayer", + "Object Layer ImageLayer", + ImageLayer_readLocalData, + ImageLayer_writeLocalData +); + +bool ImageLayer_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + osgVolume::ImageLayer& layer = static_cast(obj); + + bool itrAdvanced = false; + + if (fr.matchSequence("file %w") || fr.matchSequence("file %s")) + { + std::string filename = fr[1].getStr(); + if (!filename.empty()) + { + bool deferExternalLayerLoading = false; + + layer.setFileName(filename); + + if (!deferExternalLayerLoading) + { + osg::ref_ptr image = fr.readImage(filename.c_str()); + if (image.valid()) + { + layer.setImage(image.get()); + } + } + } + + fr += 2; + itrAdvanced = true; + } + + + return itrAdvanced; +} + +bool ImageLayer_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + const osgVolume::ImageLayer& layer = static_cast(obj); + + if (!layer.getFileName().empty()) + { + fw.indent()<<"file "<< layer.getFileName() << std::endl; + } + + return true; +} diff --git a/src/osgPlugins/osgVolume/Layer.cpp b/src/osgPlugins/osgVolume/Layer.cpp new file mode 100644 index 000000000..a3d019928 --- /dev/null +++ b/src/osgPlugins/osgVolume/Layer.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +bool Layer_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool Layer_writeLocalData(const osg::Object &obj, osgDB::Output &fw); +bool Layer_matchFilterStr(const char* str, osg::Texture::FilterMode& filter); +const char* Layer_getFilterStr(osg::Texture::FilterMode filter); + +osgDB::RegisterDotOsgWrapperProxy Layer_Proxy +( + new osgVolume::Layer, + "Layer", + "Object Layer", + Layer_readLocalData, + Layer_writeLocalData +); + +bool Layer_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + osgVolume::Layer& layer = static_cast(obj); + + bool itrAdvanced = false; + + osg::ref_ptr readObject = fr.readObjectOfType(osgDB::type_wrapper()); + osgVolume::Locator* locator = dynamic_cast(readObject.get()); + if (locator) layer.setLocator(locator); + + return itrAdvanced; +} + +bool Layer_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + const osgVolume::Layer& layer = static_cast(obj); + + if (layer.getLocator()) + { + fw.writeObject(*layer.getLocator()); + } + + return true; +} + diff --git a/src/osgPlugins/osgVolume/Locator.cpp b/src/osgPlugins/osgVolume/Locator.cpp new file mode 100644 index 000000000..59cdaa196 --- /dev/null +++ b/src/osgPlugins/osgVolume/Locator.cpp @@ -0,0 +1,85 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +bool Locator_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool Locator_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy Locator_Proxy +( + new osgVolume::Locator, + "Locator", + "Object Locator", + Locator_readLocalData, + Locator_writeLocalData +); + + +bool Locator_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + osgVolume::Locator& locator = static_cast(obj); + + bool itrAdvanced = false; + + if (fr.matchSequence("Transform {")) + { + int tansform_entry = fr[0].getNoNestedBrackets(); + + fr += 2; + + int row=0; + int col=0; + double v; + osg::Matrixd matrix; + while (!fr.eof() && fr[0].getNoNestedBrackets()>tansform_entry) + { + if (fr[0].getFloat(v)) + { + matrix(row,col)=v; + ++col; + if (col>=4) + { + col = 0; + ++row; + } + ++fr; + } + else fr.advanceOverCurrentFieldOrBlock(); + } + + locator.setTransform(matrix); + + ++fr; + itrAdvanced = true; + } + + return itrAdvanced; +} + +bool Locator_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + const osgVolume::Locator& locator = static_cast(obj); + + const osg::Matrixd& matrix = locator.getTransform(); + fw.indent() << "Transform {" << std::endl; + fw.moveIn(); + fw.indent() << matrix(0,0) << " " << matrix(0,1) << " " << matrix(0,2) << " " << matrix(0,3) << std::endl; + fw.indent() << matrix(1,0) << " " << matrix(1,1) << " " << matrix(1,2) << " " << matrix(1,3) << std::endl; + fw.indent() << matrix(2,0) << " " << matrix(2,1) << " " << matrix(2,2) << " " << matrix(2,3) << std::endl; + fw.indent() << matrix(3,0) << " " << matrix(3,1) << " " << matrix(3,2) << " " << matrix(3,3) << std::endl; + fw.moveOut(); + fw.indent() << "}"<< std::endl; + + return true; +} diff --git a/src/osgPlugins/osgVolume/ShaderTechnique.cpp b/src/osgPlugins/osgVolume/ShaderTechnique.cpp new file mode 100644 index 000000000..9e8aba253 --- /dev/null +++ b/src/osgPlugins/osgVolume/ShaderTechnique.cpp @@ -0,0 +1,40 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +bool ShaderTechnique_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool ShaderTechnique_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy ShaderTechnique_Proxy +( + new osgVolume::ShaderTechnique, + "ShaderTechnique", + "ShaderTechnique Object", + ShaderTechnique_readLocalData, + ShaderTechnique_writeLocalData +); + + +bool ShaderTechnique_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + //osgVolume::ShaderTechnique& st = static_cast(obj); + bool itrAdvanced = false; + return itrAdvanced; +} + +bool ShaderTechnique_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + //const osgVolume::ShaderTechnique& st = static_cast(obj); + return true; +} diff --git a/src/osgPlugins/osgVolume/Volume.cpp b/src/osgPlugins/osgVolume/Volume.cpp new file mode 100644 index 000000000..148a4de02 --- /dev/null +++ b/src/osgPlugins/osgVolume/Volume.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +bool Volume_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool Volume_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy Volume_Proxy +( + new osgVolume::Volume, + "Volume", + "Object Node Volume Group", + Volume_readLocalData, + Volume_writeLocalData +); + +bool Volume_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + osgVolume::Volume& volume = static_cast(obj); + + bool itrAdvanced = false; + + osg::ref_ptr readObject = fr.readObjectOfType(osgDB::type_wrapper()); + if (readObject.valid()) + { + volume.setVolumeTechnique(dynamic_cast(readObject.get())); + itrAdvanced = true; + } + + return itrAdvanced; +} + +bool Volume_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + const osgVolume::Volume& volume = static_cast(obj); + + osg::notify(osg::NOTICE)<<"Volume write"< + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +bool VolumeTile_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool VolumeTile_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy VolumeTile_Proxy +( + new osgVolume::VolumeTile, + "VolumeTile", + "Object Node VolumeTile Group", + VolumeTile_readLocalData, + VolumeTile_writeLocalData +); + +osg::TransferFunction* readTransferFunction(osgDB::Input& fr) +{ + osg::ref_ptr tf = new osg::TransferFunction1D; + + int entry = fr[0].getNoNestedBrackets(); + + fr += 2; + + std::vector colours; + + while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) + { + bool itrAdvanced = false; + if (fr.matchSequence("range %f %f")) + { + float minValue,maxValue; + fr[1].getFloat(minValue); + fr[2].getFloat(maxValue); + + tf->setInputRange(minValue,maxValue); + + fr += 3; + itrAdvanced = true; + } + + if (fr.matchSequence("color %f %f %f %f")) + { + float r,g,b,a; + fr[1].getFloat(r); + fr[2].getFloat(g); + fr[3].getFloat(b); + fr[4].getFloat(a); + + colours.push_back(osg::Vec4(r,g,b,a)); + + fr += 5; + itrAdvanced = true; + } + + if (fr.matchSequence("color %f %f %f")) + { + float r,g,b; + fr[1].getFloat(r); + fr[2].getFloat(g); + fr[3].getFloat(b); + + colours.push_back(osg::Vec4(r,g,b,1.0f)); + + fr += 5; + itrAdvanced = true; + } + + if (!itrAdvanced) + { + if (fr[0].getStr()) osg::notify(osg::NOTICE)<<"TransferFunction - unreconised token : "<allocate(colours.size()); + for(unsigned int i=0; isetValue(i, colours[i]); + } + } + + if (tf->getNumberCellsX()==0) + { + tf->allocate(6); + tf->setValue(0, osg::Vec4(1.0,1.0,1.0,1.0)); + tf->setValue(1, osg::Vec4(1.0,0.0,1.0,1.0)); + tf->setValue(2, osg::Vec4(1.0,0.0,0.0,1.0)); + tf->setValue(3, osg::Vec4(1.0,1.0,0.0,1.0)); + tf->setValue(4, osg::Vec4(0.0,1.0,1.0,1.0)); + tf->setValue(5, osg::Vec4(0.0,1.0,0.0,1.0)); + } + + return tf.release(); +} + + +bool VolumeTile_readLocalData(osg::Object& obj, osgDB::Input &fr) +{ + osgVolume::VolumeTile& volumeTile = static_cast(obj); + + bool itrAdvanced = false; + + osg::ref_ptr readObject = fr.readObjectOfType(osgDB::type_wrapper()); + if (readObject.valid()) itrAdvanced = true; + + osgVolume::Locator* locator = dynamic_cast(readObject.get()); + if (locator) volumeTile.setLocator(locator); + + { + osg::ref_ptr readObject = fr.readObjectOfType(osgDB::type_wrapper()); + osgVolume::Layer* readLayer = dynamic_cast(readObject.get()); + if (readLayer) + { + volumeTile.setLayer(readLayer); + } + + itrAdvanced = true; + } + + + readObject = fr.readObjectOfType(osgDB::type_wrapper()); + if (readObject.valid()) + { + volumeTile.setVolumeTechnique(dynamic_cast(readObject.get())); + itrAdvanced = true; + } + + return itrAdvanced; +} + +bool VolumeTile_writeLocalData(const osg::Object& obj, osgDB::Output& fw) +{ + const osgVolume::VolumeTile& volumeTile = static_cast(obj); + + int prec = fw.precision(); + fw.precision(15); + + if (volumeTile.getLocator()) + { + fw.writeObject(*volumeTile.getLocator()); + } + + { + const osgVolume::Layer* layer = volumeTile.getLayer(); + if (layer) + { + fw.writeObject(*layer); + } + } + + if (volumeTile.getVolumeTechnique()) + { + fw.writeObject(*volumeTile.getVolumeTechnique()); + } + + fw.precision(prec); + + return true; +}