[osg-users] Change color of node read from dxf file

Diego Mancilla dmancillac at gmail.com
Tue Nov 20 08:47:19 PST 2018


Hi,

The suggestion of Chris solve the problem.

The actual code:


Code:
osg::Node* lines = osgDB::readNodeFile("lines.dxf");
osg::Geode* geode = new osg::Geode;

ColorVisitor newColor;
newColor.setColor( 1.0f, 0.0f, 0.0f );
topography->accept(newColor);

geode->addChild(lines); 
_mViewer->setSceneData(geode);
_mViewer->realize();




Where ColorVisitor is a derived class from osg::NodeVistor:


Code:


class ColorVisitor : public osg::NodeVisitor
{
public:
ColorVisitor();
ColorVisitor(const osg::Vec4 &color);
virtual ~ColorVisitor();
virtual void ColorVisitor::apply(osg::Node &node);
virtual void ColorVisitor::apply(osg::Geode &geode);
virtual void ColorVisitor::setColor(const float r, const float g, const float b, const float a = 1.0f);
virtual void ColorVisitor::setColor(const osg::Vec4 &color);

private:
osg::Vec4 m_color;
osg::ref_ptr< osg::Vec4Array > m_colorArrays;



and the implementation:


Code:

#include "ColorVisitor.h"

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) {
traverse(node);
};

void ColorVisitor::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++) {
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 ColorVisitor::setColor(const float r, const float g, const float b, const float a) {

osg::Vec4 *c = &m_colorArrays->operator [](0);
m_color.set(r, g, b, a);
*c = m_color;

};

void ColorVisitor::setColor(const osg::Vec4 &color) {

osg::Vec4 *c = &m_colorArrays->operator [](0);
m_color = color;
*c = m_color;
};



The ColorVistor class I took it from Gordon Tomlison's OSG Samples (cant post links yet)

One thing stills bother me. If I dont use the line 
Code:
geode->addChild(lines)

 and instead I pass directly the node to the viewer

Code:
_mViewer->setSceneData(lines);

 the application crashes. Can anyone tell why this is happening? 

Thank you!
PS: Sorry I coudnt get the code blocks indented... :(

Cheers,

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







More information about the osg-users mailing list