From e3bf55a266196a22549b8ae79a80f16cbadf8c0a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 11 Dec 2013 12:00:27 +0000 Subject: [PATCH] Added initial support for 1D transfer functions to MultipassTechnique --- examples/osgvolume/osgvolume.cpp | 11 +++++-- src/osgVolume/MultipassTechnique.cpp | 47 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 37d333655..c5fcd8a31 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -1067,6 +1067,7 @@ int main( int argc, char **argv ) sp->setActiveProperty(0); osgVolume::AlphaFuncProperty* ap = new osgVolume::AlphaFuncProperty(alphaFunc); + osgVolume::IsoSurfaceProperty* isop = new osgVolume::IsoSurfaceProperty(alphaFunc); // SampleDensity is now deprecated osgVolume::SampleDensityProperty* sd = new osgVolume::SampleDensityProperty(0.005f); @@ -1087,7 +1088,13 @@ int main( int argc, char **argv ) cp->addProperty(tp); if (sdwm) cp->addProperty(sdwm); - if (tfp) cp->addProperty(tfp); + if (tfp) + { + OSG_NOTICE<<"Adding TransferFunction"<addProperty(tfp); + } + + cp->addProperty(isop); sp->addProperty(cp); } @@ -1112,7 +1119,7 @@ int main( int argc, char **argv ) if (useMultipass) cp->addProperty(sr); else cp->addProperty(sd); cp->addProperty(tp); - cp->addProperty(new osgVolume::IsoSurfaceProperty(alphaFunc)); + cp->addProperty(isop); if (sdwm) cp->addProperty(sdwm); if (tfp) cp->addProperty(tfp); diff --git a/src/osgVolume/MultipassTechnique.cpp b/src/osgVolume/MultipassTechnique.cpp index 5bd822169..2ae1f0097 100644 --- a/src/osgVolume/MultipassTechnique.cpp +++ b/src/osgVolume/MultipassTechnique.cpp @@ -147,6 +147,8 @@ void MultipassTechnique::init() Locator* masterLocator = _volumeTile->getLocator(); Locator* layerLocator = _volumeTile->getLayer()->getLocator(); + osg::TransferFunction1D* tf = 0; + if (!masterLocator && layerLocator) masterLocator = layerLocator; if (!layerLocator && masterLocator) layerLocator = masterLocator; @@ -202,6 +204,17 @@ void MultipassTechnique::init() else stateset->addUniform(new osg::Uniform("AlphaFuncValue",alphaFuncValue)); + + if (cpv._isoProperty.valid()) + stateset->addUniform(cpv._isoProperty->getUniform()); + else + stateset->addUniform(new osg::Uniform("IsoSurfaceValue",alphaFuncValue)); + + if (cpv._tfProperty.valid()) + { + tf = dynamic_cast(cpv._tfProperty->getTransferFunction()); + } + #if 1 osg::ref_ptr texgen = new osg::TexGen; texgen->setMode(osg::TexGen::OBJECT_LINEAR); @@ -269,6 +282,40 @@ void MultipassTechnique::init() OSG_NOTICE<<"Texture Dimensions "<s()<<", "<t()<<", "<r()<(_volumeTile->getLayer()); + if (imageLayer) + { + tfOffset = (imageLayer->getTexelOffset()[3] - tf->getMinimum()) / (tf->getMaximum() - tf->getMinimum()); + tfScale = imageLayer->getTexelScale()[3] / (tf->getMaximum() - tf->getMinimum()); + } + else + { + tfOffset = -tf->getMinimum() / (tf->getMaximum()-tf->getMinimum()); + tfScale = 1.0f / (tf->getMaximum()-tf->getMinimum()); + } + osg::ref_ptr tf_texture = new osg::Texture1D; + tf_texture->setImage(tf->getImage()); + + tf_texture->setResizeNonPowerOfTwoHint(false); + tf_texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); + tf_texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + tf_texture->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); + + unsigned int transferFunctionTextureUnit = volumeTextureUnit+1; + + stateset->setTextureAttributeAndModes(transferFunctionTextureUnit, tf_texture.get(), osg::StateAttribute::ON); + stateset->addUniform(new osg::Uniform("tfTexture",int(transferFunctionTextureUnit))); + stateset->addUniform(new osg::Uniform("tfOffset",tfOffset)); + stateset->addUniform(new osg::Uniform("tfScale",tfScale)); + + } osg::ref_ptr computeRayColorShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_compute_ray_color.frag");