Discussion:
[osg-users] Change color of node read from dxf file
Diego Mancilla
2018-11-17 12:58:42 UTC
Permalink
I'm a newbie on OpenSceneGraph and 3D development.

I have a dxf file that contains a bunch of 3DPOLYLINES (with different colors). So far I have been able to read and display them on a viewer, but I haven been able to change the color of the rendered lines. I believe that I'm not understanding properly the graph relationships.

I'm using the "Quick Start Guide" as reference and modifying an example that I found on the web.

A code snippet of what I have:


Code:
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
geom->setColorArray(c.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));

osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
geom->setNormalArray(n.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
n->push_back(osg::Vec3(0.f, -1.f, 0.f));

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

geode->addChild(lines);
geode->addDrawable(geom.get());

std::cout << "Num Drawables in geode: " << geode->getNumDrawables() << std::endl;

osg::Camera* camera = new osg::Camera;
camera->setViewport(0, 0, this->width(), this->height());
camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
camera->setProjectionMatrixAsPerspective(30.f, aspectRatio, 1.f, 1000.f);
camera->setGraphicsContext(_mGraphicsWindow);

_mViewer->setCamera(camera);
_mViewer->setSceneData(geode);
osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
manipulator->setAllowThrow(false);
this->setMouseTracking(true);
_mViewer->setCameraManipulator(manipulator);
_mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
_mViewer->realize();



Thank you!

Cheers.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75209#75209
Chris Hanson
2018-11-19 21:54:44 UTC
Permalink
That's not really how it's done.

You can't add the Node you got from ReadNodeFile as a child of a Geode. I
don't even think that should compile because Geode isn't derive from
osg::Group.

What you need to do is create a visitor to travel through the children of
the "lines" Node (that Node is almost 100% certain to be an osg::Group with
many children), finding any osg::Nodes you are interested in and examining
how the color is set up on them (per-vertex, overall, etc) and changing the
color data there.


At a glance, this old example shows sort of what I'm talking about, but it
may need tweaking to compile with current sources:
http://forum.openscenegraph.org/viewtopic.php?t=10590&view=next
Post by Diego Mancilla
I'm a newbie on OpenSceneGraph and 3D development.
I have a dxf file that contains a bunch of 3DPOLYLINES (with different
colors). So far I have been able to read and display them on a viewer, but
I haven been able to change the color of the rendered lines. I believe that
I'm not understanding properly the graph relationships.
I'm using the "Quick Start Guide" as reference and modifying an example
that I found on the web.
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
geom->setColorArray(c.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
geom->setNormalArray(n.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
n->push_back(osg::Vec3(0.f, -1.f, 0.f));
osg::Node* lines = osgDB::readNodeFile("lines.dxf");
osg::Geode* geode = new osg::Geode;
geode->addChild(lines);
geode->addDrawable(geom.get());
std::cout << "Num Drawables in geode: " << geode->getNumDrawables() << std::endl;
osg::Camera* camera = new osg::Camera;
camera->setViewport(0, 0, this->width(), this->height());
camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
float aspectRatio = static_cast<float>(this->width()) /
static_cast<float>(this->height());
camera->setProjectionMatrixAsPerspective(30.f, aspectRatio, 1.f, 1000.f);
camera->setGraphicsContext(_mGraphicsWindow);
_mViewer->setCamera(camera);
_mViewer->setSceneData(geode);
osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
manipulator->setAllowThrow(false);
this->setMouseTracking(true);
_mViewer->setCameraManipulator(manipulator);
_mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
_mViewer->realize();
Thank you!
Cheers.
------------------
http://forum.openscenegraph.org/viewtopic.php?p=75209#75209
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
Chris 'Xenon' Hanson, omo sanza lettere. ***@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]
Diego Mancilla
2018-11-20 12:04:27 UTC
Permalink
Hello Chris,

Thank you for your answer.

My code, actually compiles, nevertheless I was expecting a conceptual error from my side.

I will try what you suggest.

Cheers,

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75217#75217
Diego Mancilla
2018-11-20 16:47:19 UTC
Permalink
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
Chris Hanson
2018-11-20 21:45:01 UTC
Permalink
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.
Post by Diego Mancilla
Hi,
The suggestion of Chris solve the problem.
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();
class ColorVisitor : public osg::NodeVisitor
{
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);
osg::Vec4 m_color;
osg::ref_ptr< osg::Vec4Array > m_colorArrays;
#include "ColorVisitor.h"
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);
};
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
geode->addChild(lines)
and instead I pass directly the node to the viewer
_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,
------------------
http://forum.openscenegraph.org/viewtopic.php?p=75219#75219
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
Chris 'Xenon' Hanson, omo sanza lettere. ***@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]
Alberto Luaces
2018-11-21 09:06:04 UTC
Permalink
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.
Hi, Chris, Geode was subclassed from Group some time ago, see

https://github.com/openscenegraph/OpenSceneGraph/commit/3dde165f140ab5f973e50709235cf40c4862bc17

I don't remember exactly the rationale, but it was about making the code
more straightforward.
--
Alberto
Trajce Nikolov NICK
2018-11-21 09:11:09 UTC
Permalink
It happened when Drawable become a Node
Post by Chris Hanson
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.
Post by Chris Hanson
I don't think I can help any further. I'm missing something or you are.
Hi, Chris, Geode was subclassed from Group some time ago, see
https://github.com/openscenegraph/OpenSceneGraph/commit/3dde165f140ab5f973e50709235cf40c4862bc17
I don't remember exactly the rationale, but it was about making the code
more straightforward.
--
Alberto
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
Chris Hanson
2018-11-21 20:57:42 UTC
Permalink
I guess I missed that one. I don't even know what it means to add a Node as
a child of a Drawable so I can't comment on why this works the way it does.

On Wed, Nov 21, 2018 at 10:11 AM Trajce Nikolov NICK <
Post by Trajce Nikolov NICK
It happened when Drawable become a Node
Post by Chris Hanson
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.
Post by Chris Hanson
I don't think I can help any further. I'm missing something or you are.
Hi, Chris, Geode was subclassed from Group some time ago, see
https://github.com/openscenegraph/OpenSceneGraph/commit/3dde165f140ab5f973e50709235cf40c4862bc17
I don't remember exactly the rationale, but it was about making the code
more straightforward.
--
Alberto
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
Chris 'Xenon' Hanson, omo sanza lettere. ***@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]
Trajce Nikolov NICK
2018-11-21 21:13:36 UTC
Permalink
"... to add a Node as a child of a Drawable ..."

child of a Geode ;-) .. Now Drawable as a Node can be as a Geode child
Post by Chris Hanson
I guess I missed that one. I don't even know what it means to add a Node
as a child of a Drawable so I can't comment on why this works the way it
does.
On Wed, Nov 21, 2018 at 10:11 AM Trajce Nikolov NICK <
Post by Trajce Nikolov NICK
It happened when Drawable become a Node
Post by Chris Hanson
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.
Post by Chris Hanson
I don't think I can help any further. I'm missing something or you are.
Hi, Chris, Geode was subclassed from Group some time ago, see
https://github.com/openscenegraph/OpenSceneGraph/commit/3dde165f140ab5f973e50709235cf40c4862bc17
I don't remember exactly the rationale, but it was about making the code
more straightforward.
--
Alberto
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
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]
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
Chris Hanson
2018-11-21 21:29:24 UTC
Permalink
Yeah, that's what I meant. Sorry. ;)

On Wed, Nov 21, 2018 at 10:13 PM Trajce Nikolov NICK <
Post by Trajce Nikolov NICK
"... to add a Node as a child of a Drawable ..."
child of a Geode ;-) .. Now Drawable as a Node can be as a Geode child
Post by Chris Hanson
I guess I missed that one. I don't even know what it means to add a Node
as a child of a Drawable so I can't comment on why this works the way it
does.
On Wed, Nov 21, 2018 at 10:11 AM Trajce Nikolov NICK <
Post by Trajce Nikolov NICK
It happened when Drawable become a Node
Post by Chris Hanson
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.
Post by Chris Hanson
I don't think I can help any further. I'm missing something or you
are.
Hi, Chris, Geode was subclassed from Group some time ago, see
https://github.com/openscenegraph/OpenSceneGraph/commit/3dde165f140ab5f973e50709235cf40c4862bc17
I don't remember exactly the rationale, but it was about making the code
more straightforward.
--
Alberto
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
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]
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
trajce nikolov nick
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
Chris 'Xenon' Hanson, omo sanza lettere. ***@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]
Robert Osfield
2018-11-22 07:42:10 UTC
Permalink
Post by Chris Hanson
I guess I missed that one. I don't even know what it means to add a Node
as a child of a Drawable so I can't comment on why this works the way it
does.
Geode is really just a Group of Drawable, now that Drawable is a Node, it's
essentially just a Group of Nodes... which means that you can happily just
use Group instead of Geode Geode will be kept around for backwards
compatibility.

Having Drawable a Node and usable directly in the scene graph as any normal
child Node simplifies the scene graph structurally and should simplify
learning the OSG as you can just assign a Drawable to a Viewer via
setSceneData(), or just add a Drawable to a Group, no need to learn about a
"special" Geode that was never "special" in the first place.

Robert.

Loading...