10 #include <osg/Geometry> 
   11 #include <osg/NodeVisitor> 
   13 #include <osg/ShapeDrawable> 
   14 #include <osg/Texture2D> 
   30     CcolorVisitor() : NodeVisitor( NodeVisitor::TRAVERSE_ALL_CHILDREN ) {
 
   41         m_color.set( 1.0, 1.0, 1.0, 1.0 );
 
   43         m_colorArrays = 
new osg::Vec4Array;
 
   45         m_colorArrays->push_back( m_color );
 
   50     CcolorVisitor( 
const osg::Vec4 &color ) : NodeVisitor( NodeVisitor::TRAVERSE_ALL_CHILDREN ) {
 
   60         m_colorArrays = 
new osg::Vec4Array;
 
   62         m_colorArrays->push_back( m_color );
 
   73     void apply ( osg::Node &node ){
 
   86     void apply( osg::Geode &geode ){
 
   92     osg::StateSet *state   = NULL;
 
   93     unsigned int    vertNum = 0;
 
   99     unsigned int numGeoms = geode.getNumDrawables();
 
  102     for( 
unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++ ) {
 
  108        osg::Geometry *curGeom = geode.getDrawable( geodeIdx )->asGeometry();
 
  115            osg::Vec4Array *colorArrays = 
dynamic_cast< osg::Vec4Array *
>(curGeom->getColorArray());
 
  118              for ( 
unsigned int i = 0; i < colorArrays->size(); i++ ) {
 
  120                 osg::Vec4 *color = &colorArrays->operator [](i);
 
  126                 color->set( color->_v[0]*m_color._v[0], color->_v[0]*m_color._v[1], color->_v[0]*m_color._v[2], color->_v[3]*m_color._v[3]);
 
  130               curGeom->setColorArray( m_colorArrays.get());
 
  131               curGeom->setColorBinding( osg::Geometry::BIND_OVERALL );
 
  135         osg::ShapeDrawable * sd =  
dynamic_cast<osg::ShapeDrawable*
>(geode.getDrawable( geodeIdx ));
 
  137           if(m_color._v[3] == 0.0){
 
  139             unsigned char* pData = 
new unsigned char[s*s*3];
 
  140             for ( 
int y=0; y<s; y++ )
 
  141               for ( 
int x=0; x<s; x++ )
 
  143                 unsigned char c = (( ((y&0x8)==0) ^ (((x&0x8))==0) ))*255;
 
  145                   pData[x*3+y*s*3+0] = 255;
 
  146                   pData[x*3+y*s*3+1] = 255;
 
  147                   pData[x*3+y*s*3+2] = 255;
 
  149                   pData[x*3+y*s*3+0] = 255*m_color._v[0];
 
  150                   pData[x*3+y*s*3+1] = 255*m_color._v[1];
 
  151                   pData[x*3+y*s*3+2] = 255*m_color._v[2];
 
  155             osg::Image* pImage = 
new osg::Image;
 
  159               GL_RGB,GL_UNSIGNED_BYTE,          
 
  161               osg::Image::USE_NEW_DELETE,       
 
  164             osg::Texture2D* pTex = 
new osg::Texture2D;
 
  165             pTex->setImage( pImage );
 
  167             osg::StateSet* pStateSet = geode.getOrCreateStateSet();
 
  168             pStateSet->setTextureAttributeAndModes( 0, pTex, osg::StateAttribute::ON );
 
  170             osg::StateSet* ss = sd->getStateSet();
 
  172               ss = 
new osg::StateSet();
 
  173               sd->setStateSet( ss );
 
  174               ss->setMode( GL_BLEND, osg::StateAttribute::ON );
 
  176             sd->setColor(osg::Vec4(m_color._v[0], m_color._v[1], m_color._v[2], m_color._v[3]));
 
  185     setColor( 
const float r, 
const float g, 
const float b, 
const float a = 1.0f ){
 
  192         osg::Vec4 *c = &m_colorArrays->operator []( 0 );
 
  194         m_color.set( r,g,b,a );
 
  203     setColor( 
const osg::Vec4 &color  ){
 
  210         osg::Vec4 *c = &m_colorArrays->operator []( 0 );
 
  227     osg::ref_ptr< osg::Vec4Array > m_colorArrays;
 
This OSG NodeVisitor colors all Geometry & ShapeDrawable of a single node. 
Definition: Color.h:24