<div dir="ltr"><div>Hi,</div><div><br></div><div>try in void ColorVisitor::apply(osg::Geode &geode) { <br></div><div>.....<br></div><div> if (colorArrays) { <br>
for (unsigned int i = 0; i < colorArrays->size(); i++) <br>
{ <br>
osg::Vec4 *color = &colorArrays->operator [](i); <br>
// <br>
// could also use *color = m_color <br>
// <br>
color->set(m_color._v[0], m_color._v[1], m_color._v[2], m_color._v[3]); <br>
} <br>
</div><div>colorArrays->dirty() or ->dirtyBufferObject() I am not sure what was the right call. Please look it up. It is not enough only to change the color, you have to dirty the array to be updated<br></div><div>
} </div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 27, 2018 at 2:10 AM Diego Mancilla <<a href="mailto:dmancillac@gmail.com">dmancillac@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Trajce,<br>
<br>
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: <a href="http://forum.openscenegraph.org/viewtopic.php?p=75209#75209" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=75209#75209</a>).<br>
<br>
<br>
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:<br>
<br>
<br>
<br>
Code:<br>
_lines = osgDB::readNodeFile("lines.dxf"); <br>
_topo->setDataVariance(osg::Object::DYNAMIC); <br>
osg::Geode* geode = new osg::Geode; <br>
<br>
_mViewer->addEventHandler(new ColorHandler); <br>
<br>
ColorVisitor newColor; <br>
newColor.setColor( 1.0f, 0.0f, 0.0f ); <br>
_lines->accept(newColor); <br>
geode->addChild(_lines); <br>
_mViewer->realize();<br>
<br>
<br>
<br>
The color of the lines turns red on start. But then, when I try to change it to another color on runetime, nothing happens.<br>
<br>
Anyway, here is the visitor implementation (.cpp):<br>
<br>
<br>
Code:<br>
ColorVisitor::ColorVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) <br>
{ <br>
m_color.set(1.0, 1.0, 1.0, 1.0); <br>
m_colorArrays = new osg::Vec4Array; <br>
m_colorArrays->push_back(m_color); <br>
}; <br>
<br>
ColorVisitor::ColorVisitor(const osg::Vec4 &color): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) <br>
{ <br>
m_color = color; <br>
m_colorArrays = new osg::Vec4Array; <br>
m_colorArrays->push_back(m_color); <br>
<br>
}; <br>
<br>
ColorVisitor::~ColorVisitor() <br>
{ <br>
}; <br>
<br>
void ColorVisitor::apply(osg::Node &node) { <br>
// -------------------------------------------- <br>
// <br>
// Handle traversal of osg::Node node types <br>
// <br>
// -------------------------------------------- <br>
traverse(node); <br>
}; <br>
<br>
void ColorVisitor::apply(osg::Geode &geode) { <br>
// ------------------------------------------------ <br>
// <br>
// Handle traversal of osg::Geode node types <br>
// <br>
// ------------------------------------------------ <br>
<br>
osg::StateSet *state = NULL; <br>
unsigned int vertNum = 0; <br>
// <br>
// We need to iterate through all the drawables check if <br>
// the contain any geometry that we will need to process <br>
// <br>
<br>
unsigned int numGeoms = geode.getNumDrawables(); <br>
<br>
for (unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++) <br>
{ <br>
// <br>
// Use 'asGeometry' as its supposed to be faster than a dynamic_cast <br>
// every little saving counts <br>
// <br>
osg::Geometry *curGeom = geode.getDrawable(geodeIdx)->asGeometry(); <br>
// <br>
// Only process if the drawable is geometry <br>
// <br>
if (curGeom) <br>
{ <br>
osg::Vec4Array *colorArrays = dynamic_cast<osg::Vec4Array *>(curGeom->getColorArray()); <br>
if (colorArrays) { <br>
for (unsigned int i = 0; i < colorArrays->size(); i++) <br>
{ <br>
osg::Vec4 *color = &colorArrays->operator [](i); <br>
// <br>
// could also use *color = m_color <br>
// <br>
color->set(m_color._v[0], m_color._v[1], m_color._v[2], m_color._v[3]); <br>
} <br>
<br>
} <br>
else <br>
{ <br>
curGeom->setColorArray(m_colorArrays.get()); <br>
curGeom->setColorBinding(osg::Geometry::BIND_OVERALL); <br>
} <br>
} <br>
} <br>
}; <br>
<br>
void ColorVisitor::setColor(const float r, const float g, const float b, const float a) <br>
{ <br>
// ------------------------------------------------------------------- <br>
// <br>
// Set the color to change apply to the nodes geometry <br>
// <br>
// ------------------------------------------------------------------- <br>
osg::Vec4 *c = &m_colorArrays->operator [](0); <br>
m_color.set(r, g, b, a); <br>
*c = m_color; <br>
}; <br>
<br>
void ColorVisitor::setColor(const osg::Vec4 &color) { <br>
// ------------------------------------------------------------------- <br>
// <br>
// Set the color to change apply to the nodes geometry <br>
// <br>
// ------------------------------------------------------------------- <br>
osg::Vec4 *c = &m_colorArrays->operator [](0); <br>
m_color = color; <br>
*c = m_color; <br>
};<br>
<br>
<br>
<br>
<br>
<br>
Cheers,<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=75248#75248" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=75248#75248</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">trajce nikolov nick<br></div>