#include <osg/array>
#include <osg/geode>
#include <osg/Geometry>
#include <osg/NodeVisitor>
#include <osg/Vec4>
class CcolorVisitor : public osg::NodeVisitor {
public :
CcolorVisitor() : NodeVisitor( NodeVisitor::TRAVERSE_ALL_CHILDREN ) {
m_color.set( 1.0, 1.0, 1.0, 1.0 );
m_colorArrays = new osg::Vec4Array;
m_colorArrays->push_back( m_color );
};
CcolorVisitor( const osg::Vec4 &color ) : NodeVisitor( NodeVisitor::TRAVERSE_ALL_CHILDREN ) {
m_color = m_color;
m_colorArrays = new osg::Vec4Array;
m_colorArrays->push_back( m_color );
};
virtual ~CcolorVisitor(){};
virtual
void apply ( osg::Node &node ){
traverse( node );
}
virtual
void apply( osg::Geode &geode ){
osg::StateSet *state = NULL;
unsigned int vertNum = 0;
unsigned int numGeoms = geode.getNumDrawables();
for( unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++ ) {
osg::Geometry *curGeom = geode.getDrawable( geodeIdx )->asGeometry();
if ( curGeom ) {
osg::Vec4Array *colorArrays = dynamic_cast< osg::Vec4Array *>(curGeom->getColorArray());
if ( colorArrays ) {
for ( unsigned int i = 0; i < colorArrays->size(); i++ ) {
osg::Vec4 *color = &colorArrays->operator [](i);
color->set( m_color._v[0], m_color._v[1], m_color._v[2], m_color._v[3]);
}
}
else{
curGeom->setColorArray( m_colorArrays.get());
curGeom->setColorBinding( osg::Geometry::BIND_OVERALL );
}
}
}
}
void
setColor( const float r, const float g, const float b, const float a = 1.0f ){
osg::Vec4 *c = &m_colorArrays->operator []( 0 );
m_color.set( r,g,b,a );
*c = m_color;
}
void
setColor( const osg::Vec4 &color ){
osg::Vec4 *c = &m_colorArrays->operator []( 0 );
m_color = color;
*c = m_color;
}
private :
osg::Vec4 m_color;
osg::ref_ptr< osg::Vec4Array > m_colorArrays;
};
|