Commit b795776b authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD #161] added transparency support for isosurfaces

parent df3d553e
......@@ -63,7 +63,8 @@ WMMarchingCubes::WMMarchingCubes():
m_tIsoLevel( 0 ),
m_dataSet(),
m_shaderUseTexture( true ),
m_shaderUseLighting( false )
m_shaderUseLighting( false ),
m_shaderUseTransparency( false )
{
// WARNING: initializing connectors inside the constructor will lead to an exception.
// Implement WModule::initializeConnectors instead.
......@@ -129,6 +130,7 @@ void WMMarchingCubes::moduleMain()
// settings for normal isosurface
m_shaderUseLighting = true;
m_shaderUseTransparency = true;
renderSurface();
......@@ -160,6 +162,7 @@ void WMMarchingCubes::properties()
m_properties->addBool( "textureChanged", false, true );
m_properties->addBool( "active", true, true )->connect( boost::bind( &WMMarchingCubes::slotPropertyChanged, this, _1 ) );
m_properties->addDouble( "Iso Value", 100 )->connect( boost::bind( &WMMarchingCubes::slotPropertyChanged, this, _1 ) );
m_properties->addInt( "Opacity %", 100 )->connect( boost::bind( &WMMarchingCubes::slotPropertyChanged, this, _1 ) );
m_properties->addBool( "Use Texture", true )->connect( boost::bind( &WMMarchingCubes::slotPropertyChanged, this, _1 ) );
}
......@@ -180,6 +183,11 @@ void WMMarchingCubes::slotPropertyChanged( std::string propertyName )
m_shaderUseTexture = m_properties->getValue< bool >( propertyName );
updateTextures();
}
else if( propertyName == "Opacity %" )
{
debugLog() << "Change opacity." << std::endl;
updateTextures();
}
else if( propertyName == "active" )
{
if ( m_properties->getValue<bool>( propertyName ) )
......@@ -662,6 +670,7 @@ void WMMarchingCubes::renderMesh( WTriangleMesh* mesh )
osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
lightModel->setTwoSided( true );
state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
state->setMode( GL_BLEND, osg::StateAttribute::ON );
// ------------------------------------------------
// Shader stuff
......@@ -732,8 +741,23 @@ void WMMarchingCubes::renderMesh( WTriangleMesh* mesh )
state->addUniform( m_samplerUniforms[i] );
}
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", m_shaderUseTexture ) ) );
if( !m_shaderUseTexture )
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", m_properties->getValue< bool >( "Use Texture" ) ) ) );
}
else
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", true ) ) );
}
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useLighting", m_shaderUseLighting ) ) );
if( m_shaderUseTransparency )
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "opacity", m_properties->getValue< int >( "Opacity %" ) ) ) );
}
else
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "opacity", 100 ) ) );
}
std::vector< boost::shared_ptr< WDataSet > > dsl = WKernel::getRunningKernel()->getGui()->getDataSetList( 0, true );
if ( dsl.size() > 0 )
......@@ -988,16 +1012,32 @@ void WMMarchingCubes::updateTextures()
m_typeUniforms[i]->set( 0 );
}
osg::StateSet* rootState = m_geode->getOrCreateStateSet();
rootState->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", m_shaderUseTexture ) ) );
rootState->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useLighting", m_shaderUseLighting ) ) );
osg::StateSet* state = m_geode->getOrCreateStateSet();
if( !m_shaderUseTexture )
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", m_properties->getValue< bool >( "Use Texture" ) ) ) );
}
else
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useTexture", true ) ) );
}
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "useLighting", m_shaderUseLighting ) ) );
if( m_shaderUseTransparency )
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "opacity", m_properties->getValue< int >( "Opacity %" ) ) ) );
}
else
{
state->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "opacity", 100 ) ) );
}
int c = 0;
for ( size_t i = 0; i < dsl.size(); ++i )
{
osg::ref_ptr<osg::Texture3D> texture3D = dsl[i]->getTexture()->getTexture();
rootState->setTextureAttributeAndModes( c, texture3D, osg::StateAttribute::ON );
state->setTextureAttributeAndModes( c, texture3D, osg::StateAttribute::ON );
float t = dsl[i]->getTexture()->getThreshold()/ 100.0;
float a = dsl[i]->getTexture()->getAlpha();
......
......@@ -258,6 +258,7 @@ private:
bool m_shaderUseTexture; //!< shall the shader use texturing?
bool m_shaderUseLighting; //!< shall the shader use lighting?
bool m_shaderUseTransparency; //!< shall the shader use transparency?
/**
* Lock to prevent concurrent threads trying to update the osg node
......
......@@ -2,6 +2,7 @@ varying vec4 VaryingTexCoord0;
uniform bool useLighting;
uniform bool useTexture;
uniform int opacity;
uniform sampler3D tex0;
uniform sampler3D tex1;
......@@ -74,7 +75,7 @@ void lookupTex(inout vec4 col, in int type, in sampler3D tex, in float threshol
void main()
{
vec4 col = vec4(0.3, 0.3, 0.3, 0.0);
vec4 col = vec4(0.3, 0.3, 0.3, 1.0);
vec4 ambient = vec4(0.0);
vec4 diffuse = vec4(0.0);
......@@ -101,11 +102,12 @@ void main()
col = clamp(col, 0.0, 1.0);
col.a = sqrt( col.r * col.r + col.g * col.g + col.b * col.b);
//discard completely black voxels. So if no texture is active we will see nothing
if ( ( col.r + col.g + col.b ) < 0.01 )
discard;
col.a = opacity * .01;
gl_FragColor = col;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment