Commit 10937fd8 authored by Alexander Wiebel's avatar Alexander Wiebel

[ADD] opacity to the triangle mesh renderer module

parent 4344150b
......@@ -80,7 +80,9 @@ void WMTriangleMeshRenderer::connectors()
void WMTriangleMeshRenderer::properties()
{
// Put the code for your properties here. See "src/modules/template/" for an extensively documented example.
m_opacityProp = m_properties2->addProperty( "Opacity %", "Opaqueness of surface.", 100 );
m_opacityProp->setMin( 0 );
m_opacityProp->setMax( 100 );
}
void WMTriangleMeshRenderer::moduleMain()
......@@ -165,7 +167,7 @@ void WMTriangleMeshRenderer::renderMesh( boost::shared_ptr< WTriangleMesh > mesh
// colors
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back( osg::Vec4( .9f, .9f, 0.9f, 1.0f ) );
colors->push_back( osg::Vec4( .3f, .3f, 0.3f, 1.0f ) );
surfaceGeometry->setColorArray( colors );
surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
......@@ -174,9 +176,23 @@ void WMTriangleMeshRenderer::renderMesh( boost::shared_ptr< WTriangleMesh > mesh
state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
state->setMode( GL_BLEND, osg::StateAttribute::ON );
{
osg::ref_ptr< osg::Material > material = new osg::Material();
material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
material->setShininess( osg::Material::FRONT, 25.0 );
state->setAttribute( material );
}
m_moduleNode->insert( m_surfaceGeode );
m_shader = osg::ref_ptr< WShader > ( new WShader( "triangleMeshRenderer" ) );
m_shader->apply( m_surfaceGeode );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_moduleNode );
m_moduleNode->addUpdateCallback( new TriangleMeshRendererCallback( boost::shared_dynamic_cast< WMTriangleMeshRenderer >( shared_from_this() ) ) );
}
void WMTriangleMeshRenderer::activate()
......@@ -198,3 +214,12 @@ void WMTriangleMeshRenderer::activate()
WModule::activate();
}
void WMTriangleMeshRenderer::update()
{
if( m_opacityProp->changed() )
{
osg::StateSet* rootState = m_surfaceGeode->getOrCreateStateSet();
rootState->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "opacity", m_opacityProp->get( true ) ) ) );
}
}
......@@ -86,6 +86,11 @@ public:
*/
virtual const char** getXPMIcon() const;
/**
* updates shader parameters
*/
void update();
protected:
/**
......@@ -115,10 +120,15 @@ private:
*/
boost::shared_ptr< WModuleInputData< WTriangleMesh > > m_input;
WPropInt m_opacityProp; //!< Property holding the opacity valueassigned to the surface
osg::ref_ptr< WGEGroupNode > m_moduleNode; //!< Pointer to the modules group node.
osg::ref_ptr< osg::Geode > m_surfaceGeode; //!< Pointer to geode containing the surface.
osg::ref_ptr< WShader > m_shader; //!< The shader used for the iso surface in m_geode
/**
* This function generates the osg geometry from the WTriangleMesh.
* \param mesh The triangle mesh that will be rendered.
......@@ -126,4 +136,42 @@ private:
void renderMesh( boost::shared_ptr< WTriangleMesh > mesh );
};
/**
* Adapter object for realizing callbacks of the node representing the surface in the osg
*/
class TriangleMeshRendererCallback : public osg::NodeCallback
{
public:
/**
* Constructor of the callback adapter.
* \param module A function of this module will be called
*/
explicit TriangleMeshRendererCallback( boost::shared_ptr< WMTriangleMeshRenderer > module );
/**
* Function that is called by the osg and that call the function in the module.
* \param node The node we are called.
* \param nv the visitor calling us.
*/
virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
private:
boost::shared_ptr< WMTriangleMeshRenderer > m_module; //!< Pointer to the module to which the function that is called belongs to.
};
inline TriangleMeshRendererCallback::TriangleMeshRendererCallback( boost::shared_ptr< WMTriangleMeshRenderer > module )
: m_module( module )
{
}
inline void TriangleMeshRendererCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
{
if ( m_module )
{
m_module->update();
}
traverse( node, nv );
}
#endif // WMTRIANGLEMESHRENDERER_H
varying vec4 VaryingTexCoord0;
uniform int opacity;
#include "lighting.fs"
void main()
{
vec4 col = vec4(0.3, 0.3, 0.3, 1.0);
vec4 ambient = vec4(0.0);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
calculateLighting(-normal, gl_FrontMaterial.shininess, ambient, diffuse, specular);
col = col + (ambient * col / 2.0) + (diffuse * col) + (specular * col / 2.0);
col = clamp(col, 0.0, 1.0);
col.a = opacity * .01;
gl_FragColor = col;
}
varying vec4 VaryingTexCoord0;
#include "lighting.vs"
void main()
{
VaryingTexCoord0 = gl_MultiTexCoord0;
prepareLight();
gl_Position = ftransform();
}
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