Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added initial support for 1D transfer functions to MultipassTechnique
  • Loading branch information
Robert OSFIELD committed Dec 11, 2013
1 parent 2eabffd commit 70248cf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/osgvolume/osgvolume.cpp
Expand Up @@ -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);
Expand All @@ -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"<<std::endl;
cp->addProperty(tfp);
}

cp->addProperty(isop);

sp->addProperty(cp);
}
Expand All @@ -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);

Expand Down
47 changes: 47 additions & 0 deletions src/osgVolume/MultipassTechnique.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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<osg::TransferFunction1D*>(cpv._tfProperty->getTransferFunction());
}

#if 1
osg::ref_ptr<osg::TexGen> texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::OBJECT_LINEAR);
Expand Down Expand Up @@ -269,6 +282,40 @@ void MultipassTechnique::init()
OSG_NOTICE<<"Texture Dimensions "<<image_3d->s()<<", "<<image_3d->t()<<", "<<image_3d->r()<<std::endl;
}

if (tf)
{
OSG_NOTICE<<"Setting up TransferFunction"<<std::endl;

float tfScale = 1.0f;
float tfOffset = 0.0f;

ImageLayer* imageLayer = dynamic_cast<ImageLayer*>(_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<osg::Texture1D> 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<osg::Shader> computeRayColorShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_compute_ray_color.frag");
Expand Down

0 comments on commit 70248cf

Please sign in to comment.