[osg-users] Changing the color of a model

667 views
Skip to first unread message

Andre Sanchez

unread,
Oct 8, 2015, 9:43:40 AM10/8/15
to osg-...@lists.openscenegraph.org
Hi OpenSceneGraph forum!

I'm very new to OSG, and while I'm very familiar with programming, and somewhat familiar with 3D graphics, I am now devoting actual time working in a lab to properly learn C++, and OSG as well.

My issue is as follows:
I am loading an existing model as a Node, and I want to be able to change the color of the model in real time. How should I go about doing this?

I know how attach an update callback to the Node, but it's the color that I can't seem to change. I am able to set the diffuse color on a primitive object using a material like so:


Code:

osg::ref_ptr<osg::Material> mat = new osg::Material;
mat->setDiffuse (osg::Material::FRONT_AND_BACK, osg::Vec4(0.8, 0.1, 0.4, 1.0));
myNode->getOrCreateStateSet()->setAttributeAndModes(mat, osg::StateAttribute::ON);


but this doesn't seem to work on the model.

I found that if I changed the model's .osg file to not define material colors on particular components of the model, then this color I defined for the model would affect those parts. However I feel there should be a better way to do this, as I will probably end up using models that actually need these colors within their respective .osg files. I know I can probably do this with a shader, and if that's necessary then fine, but if there's an easier way then that would be great.

So essentially my question is:
What is the easiest way to change the overall diffuse color of an object?

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

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Trajce Nikolov NICK

unread,
Oct 8, 2015, 9:50:21 AM10/8/15
to OpenSceneGraph Users
Hi,

maybe you try

myNode->getOrCreateStateSet()->setAttributeAndModes(mat, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
--
trajce nikolov nick

Andre Sanchez

unread,
Oct 9, 2015, 5:06:46 AM10/9/15
to osg-...@lists.openscenegraph.org
Ah, that seems to have done the trick for what I'm doing at the moment, thanks!

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=65334#65334
Reply all
Reply to author
Forward
0 new messages