[osg-users] Update node color on demand

Diego Mancilla dmancillac at gmail.com
Mon Nov 26 14:42:57 PST 2018


Hello Trajce,

 The visitor class implementation is on my previous post on this thread. I took that code from Gordon Tomlison's OSG Samples, and it works when is used previous to the rendering as you can see on my initial post (other thread: http://forum.openscenegraph.org/viewtopic.php?p=75209#75209).


 As I said everything gets called when it should, but on runtime, the lines wont change color. I you look at the code snippet of my main:

 

Code:
_lines = osgDB::readNodeFile("lines.dxf"); 
_topo->setDataVariance(osg::Object::DYNAMIC); 
osg::Geode* geode = new osg::Geode; 

_mViewer->addEventHandler(new ColorHandler); 

ColorVisitor newColor; 
newColor.setColor( 1.0f, 0.0f, 0.0f ); 
_lines->accept(newColor); 
geode->addChild(_lines); 
_mViewer->realize();



The color of the lines turns red on start. But then, when I try to change it to another color on runetime, nothing happens.

Anyway, here is the visitor implementation (.cpp):


Code:
ColorVisitor::ColorVisitor(): osg::NodeVisitor(osg::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); 
}; 

ColorVisitor::ColorVisitor(const osg::Vec4 &color): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) 
{ 
    m_color = color; 
    m_colorArrays = new osg::Vec4Array; 
    m_colorArrays->push_back(m_color); 

}; 

ColorVisitor::~ColorVisitor() 
{ 
}; 

void ColorVisitor::apply(osg::Node &node) { 
    // -------------------------------------------- 
    // 
    //  Handle traversal of osg::Node node types 
    // 
    // -------------------------------------------- 
    traverse(node); 
}; 

void ColorVisitor::apply(osg::Geode &geode) { 
    // ------------------------------------------------ 
    // 
    //  Handle traversal of osg::Geode node types 
    // 
    // ------------------------------------------------ 

    osg::StateSet *state = NULL; 
    unsigned int    vertNum = 0; 
    //  
    //  We need to iterate through all the drawables check if 
    //  the contain any geometry that we will need to process 
    // 

    unsigned int numGeoms = geode.getNumDrawables(); 

    for (unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++) 
    { 
        // 
        // Use 'asGeometry' as its supposed to be faster than a dynamic_cast 
        // every little saving counts 
        // 
        osg::Geometry *curGeom = geode.getDrawable(geodeIdx)->asGeometry(); 
        // 
        // Only process if the drawable is geometry 
        // 
        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); 
                    // 
                    // could also use *color = m_color 
                    // 
                    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 ColorVisitor::setColor(const float r, const float g, const float b, const float a) 
{ 
    // ------------------------------------------------------------------- 
    // 
    // Set the color to change apply to the nodes geometry 
    // 
    // ------------------------------------------------------------------- 
    osg::Vec4 *c = &m_colorArrays->operator [](0); 
    m_color.set(r, g, b, a); 
    *c = m_color; 
}; 

void ColorVisitor::setColor(const osg::Vec4 &color) { 
    // ------------------------------------------------------------------- 
    // 
    // Set the color to change apply to the nodes geometry 
    // 
    // ------------------------------------------------------------------- 
    osg::Vec4 *c = &m_colorArrays->operator [](0); 
    m_color = color; 
    *c = m_color; 
};





Cheers,

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75248#75248







More information about the osg-users mailing list