查找节点名称
#include <Windows.h>
#include <osg\Node>
#include <osg\Group>
#include <osg\Geometry>
#include <osgViewer/Viewer>
class FindNameVisitor : public osg::NodeVisitor
{
public:
FindNameVisitor(const std::string& name, TraversalMode tm = TRAVERSE_ALL_CHILDREN):
osg::NodeVisitor(tm),
name_(name)
{
}
~FindNameVisitor()
{
}
virtual void apply(osg::Node& node) override
{
if (node.getName() == name_)
node_ = &node;
std::cout << "Node Name:" << node.getName() << std::endl;
traverse(node);
}
osg::Node* getName() { return node_.get(); }
private:
std::string name_;
osg::ref_ptr<osg::Node> node_;
};
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> group = new osg::Group();
group->addChild(osgDB::readNodeFile("cow.osg"));
viewer.setSceneData(group);
FindNameVisitor nv("cow.osg");
group->accept(nv);
osg::ref_ptr<osg::Node> node = nv.getName();
std::cout <<"Find Node:"<< node.get() << std::endl;
viewer.addEventHandler(new PickHander());
viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.setUpViewInWindow(100, 100, 500, 400);
return viewer.run();
}