Commit 5594ca90 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD #293] added new functionality to create flat shaded surface from triangle...

[ADD #293] added new functionality to create flat shaded surface from triangle mesh and use the functionality in module
parent 67797a64
...@@ -340,6 +340,62 @@ osg::ref_ptr< osg::Geometry > wge::convertToOsgGeometry( WTriangleMesh::SPtr mes ...@@ -340,6 +340,62 @@ osg::ref_ptr< osg::Geometry > wge::convertToOsgGeometry( WTriangleMesh::SPtr mes
return geometry; return geometry;
} }
osg::ref_ptr< osg::Geometry > wge::convertMeshToOsgGeometryFlat( WTriangleMesh::SPtr mesh,
const WColor& color )
{
osg::ref_ptr< osg::Geometry> geometry( new osg::Geometry );
geometry->setVertexArray( mesh->getVertexArray() );
osg::DrawElementsUInt* surfaceElement;
surfaceElement = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
std::vector< size_t > tris = mesh->getTriangles();
surfaceElement->reserve( tris.size() );
for( unsigned int vertId = 0; vertId < tris.size(); ++vertId )
{
surfaceElement->push_back( tris[vertId] );
}
geometry->addPrimitiveSet( surfaceElement );
{
osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
colors->push_back( color );
geometry->setColorArray( colors );
geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
}
// ------------------------------------------------
// normals
{
geometry->setNormalArray( mesh->getTriangleNormalArray() );
geometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
// if normals are specified, we also setup a default lighting.
osg::StateSet* state = geometry->getOrCreateStateSet();
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 );
{
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, 0.0 );
state->setAttribute( material );
}
}
// enable VBO
geometry->setUseDisplayList( false );
geometry->setUseVertexBufferObjects( true );
return geometry;
}
osg::ref_ptr< osg::Geometry > wge::convertToOsgGeometryLines( WTriangleMesh::SPtr mesh, osg::ref_ptr< osg::Geometry > wge::convertToOsgGeometryLines( WTriangleMesh::SPtr mesh,
const WColor& defaultColor, const WColor& defaultColor,
bool useMeshColor ) bool useMeshColor )
......
...@@ -128,6 +128,18 @@ namespace wge ...@@ -128,6 +128,18 @@ namespace wge
bool lighting = false bool lighting = false
); );
/**
* Extract the vertices and triangles from a WTriangleMesh and save them
* into an osg::Geometry. It uses per triangle normals.
*
* \param mesh the WTriangleMesh used as input
* \param color This color is used for all vertices
* \return an osg::Geometry containing the mesh
* \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
*/
osg::ref_ptr< osg::Geometry > convertMeshToOsgGeometryFlat( WTriangleMesh::SPtr mesh,
const WColor& color = WColor( 1.0, 1.0, 1.0, 1.0 ) );
/** /**
* Convert triangle mesh to lines representing it. Draws lines twice (ATM). * Convert triangle mesh to lines representing it. Draws lines twice (ATM).
* *
......
...@@ -162,6 +162,7 @@ void WMTriangleMeshRenderer::properties() ...@@ -162,6 +162,7 @@ void WMTriangleMeshRenderer::properties()
// setup all the properties. See header file for their meaning and purpose. // setup all the properties. See header file for their meaning and purpose.
m_showOutline = m_properties->addProperty( "Outline", "Show all edges of the trinagulation as lines.", false, m_propCondition ); m_showOutline = m_properties->addProperty( "Outline", "Show all edges of the trinagulation as lines.", false, m_propCondition );
m_flatShading = m_properties->addProperty( "Flat", "If showing the triangulation as a surface use flat shading.", false, m_propCondition );
m_mainComponentOnly = m_properties->addProperty( "Main component", "Main component only", false, m_propCondition ); m_mainComponentOnly = m_properties->addProperty( "Main component", "Main component only", false, m_propCondition );
m_showCoordinateSystem = m_properties->addProperty( "Coordinate system", "If enabled, the coordinate system of the mesh will be shown.", m_showCoordinateSystem = m_properties->addProperty( "Coordinate system", "If enabled, the coordinate system of the mesh will be shown.",
false, m_propCondition ); false, m_propCondition );
...@@ -393,7 +394,14 @@ void WMTriangleMeshRenderer::renderMesh( boost::shared_ptr< WTriangleMesh > mesh ...@@ -393,7 +394,14 @@ void WMTriangleMeshRenderer::renderMesh( boost::shared_ptr< WTriangleMesh > mesh
if( colorModeSelector.getItemIndexOfSelected( 0 ) == 0 ) if( colorModeSelector.getItemIndexOfSelected( 0 ) == 0 )
{ {
// use single color // use single color
geometry = wge::convertToOsgGeometry( mesh, m_color->get(), true, true, false ); if( m_flatShading->get( true ) )
{
geometry = wge::convertMeshToOsgGeometryFlat( mesh, m_color->get() );
}
else
{
geometry = wge::convertToOsgGeometry( mesh, m_color->get(), true, true, false );
}
} }
else if( colorModeSelector.getItemIndexOfSelected( 0 ) == 1 ) else if( colorModeSelector.getItemIndexOfSelected( 0 ) == 1 )
{ {
......
...@@ -263,6 +263,11 @@ private: ...@@ -263,6 +263,11 @@ private:
*/ */
WPropBool m_showOutline; WPropBool m_showOutline;
/**
* Toggle rendering surface flat shaded (per triangle normals) instead of Phong shaded.
*/
WPropBool m_flatShading;
/** /**
* En/Disable display of only the main component (biggest vertices number) * En/Disable display of only the main component (biggest vertices number)
*/ */
......
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