[osg-users] Color Change Question

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Thu Jul 7 00:30:20 PDT 2016

Am 07.07.2016 um 00:05 schrieb Daniel Lecklider:
> Hi,
> At my work we use OSG for simulation purposes and I am looking to display a model with each of its individual parts/components displayed in a different color.
Colors can mean a lot of different things. It can be vertex colors, 
material colors or textures.
So basically you need to remove/replace all of them, depending on what 
you want to achieve.
>   I am new to OSG so I still dont understand everything yet. But I know I need to remove all of the skins and apply a new color. A co-worker already created a way to give the entire model a color but I need to do it for each sub component.
A good exampe that you shouldn't always rely on co-worker follows ;-)
Use the visitor pattern to traverse the scenegraph. There you can 
override the various apply virtual funtions to change the nodes you want 
to modify. If you want to remove the textures you should inspect the 
statesets of the nodes (overwriting apply of osg::Node is sufficient) 
and remove the texture based attributes. Another way is to set a uniform 
with the given color to the root of your parts that you want to be 
colored and and an shader program that simply ignores all other 
attributes but displays your choosen color instead.
The OSG has a lot of examples, so you should take a look for 
osg::NodeVisitor, osg::Material (and if you want to go with the shader, 
potentially osg::Uniform, osg::Program).
If you need in-depth support, you can also give me an example model with 
an description and I might give some in-depth advice.
> Here is the function my co-worker has used to remove the textures and add the new material.
> Code:
> void removeTextures(osg::Node* x)
> {
> if(x != nullptr)
>       {
>            auto state = x->getOrCreateStateSet();
>            state->setAttributeAndModes(this->material, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
>            state->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF);
>            auto group = x->asGroup();
>            if(group != nullptr)
>            {
>                 auto numChildren = group->getNumChildren();
>                 for(decltype(numChildren) i = 0; i < numChildren; i++)
>                 {
>                      this->removeTextures(group->getChild(i));
>                 }
>            }
>       }
> }
> How would I go about changing the material of each geode/geometry/drawable and what are the differences between these three classes?
Geometry is simply what it says: A bunch of vertices with potentially 
normals etc. A drawable is the base-class for the scenegraph's leafs. 
Geode is the "old" way of having a group of drawables as a leaf of the 
You should really read a book (OSG Cookbook e.g.) or take a look into 
the examples.

> Thank you!
> Cheers,
> Daniel
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=68032#68032
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

More information about the osg-users mailing list