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

Chris Hanson xenon at alphapixel.com
Tue Nov 20 13:45:01 PST 2018


I dunno man, your code is making my head explode. I don't think Geode HAS
an addChild method, so I don't even know why what you're doing would
compile.

I don't think I can help any further. I'm missing something or you are.

On Tue, Nov 20, 2018 at 5:46 PM Diego Mancilla <dmancillac at gmail.com> wrote:

> 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
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>


-- 
Chris 'Xenon' Hanson, omo sanza lettere. Xenon at AlphaPixel.com
http://www.alphapixel.com/
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 •
GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS •
osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile •
iPhone/iPad/iOS • Android
@alphapixel <https://twitter.com/alphapixel> facebook.com/alphapixel (775)
623-PIXL [7495]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20181120/a33514ea/attachment.html>


More information about the osg-users mailing list