[osg-users] Can I find a StateSet which has been setName?

0 views
Skip to first unread message

william nily

unread,
Sep 28, 2010, 4:24:04 AM9/28/10
to osg-...@lists.openscenegraph.org
Hi!
Since the StateSet inherits from Object,so i can setName to a specific StateSet.Then can I find the StateSet by its name?Is there any way to do this such like to find a Node through its name?
Thanks very much!

Robert Osfield

unread,
Sep 28, 2010, 8:50:33 AM9/28/10
to OpenSceneGraph Users
Hi William,

The tool for the job is a custom NodeVisitor, which does the traversal
of the scene graph for you and then picks out the StateSet's on all
the Node and Drawables. Something like:


class MyFindStateSet : public osg::NodeVisitor
{
public:
std::string _name;

MyFindStateSet(const std::string& name):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_name(name) {}

void checkStateSet(const osg::StateSate* stateset)
{
if (node.getStateSet() && node.getStateSet()->getName()==_name)
{
// found a node with a the correct stateset, now do what you want...
}
}

void apply(osg::Node& node)
{
checkStateSet(node.getStateSet());
// must traverse children otherwise we want descend scene graph
traverse(node);
}

void apply(osg::Geode& geode)
{
checkStateSet(geode.getStateSet());

// traverse the drawables of the geode.
for(unsigned int i = 0; i<geode.getNumChildren(); ++i)
{
checkStateSet(geode.getDrawable(i)->getStateSet());
}
}

};

..


{
..
// now create the visitor and pass it to the scene graph to do
the traversal
MyFindStateSet mfss("mystateset");
myscenegraph->accept(mffs);
..
}

For further examples of NodeVisitor's in action just do a search of
the OSG code base, you'll find many of them all over the place. It's
one of the most useful classes in the OSG.

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

Reply all
Reply to author
Forward
0 new messages