遍历节点树:
osg::Node类中有两个辅助函数:
1void ascend(NodeVisitor& nv) //虚函数,向上一级节点推进访问器 2void traverse(NodeVisitor& nv) //虚函数,向下一级节点推进访问器 3NodeVisitor的traverse()函数实现如下: 4inline void traverse(Node& node) 5{ 6 if (_traversalMode == TRAVERSE_PARENTS) 7 { 8 node.ascend(*this); 9 } 10 else if (_traversalMode != TRAVERSE_NONE) 11 { 12 node.traverse(*this); 13 } 14} 15 16#include <osg/Node> 17#include <osgDB/ReadFile> 18#include <iostream> 19 20using namespace std; 21 22class InfoVisitor: public osg::NodeVisitor 23{ 24public: 25 InfoVisitor() 26 :osg::NodeVisitor(TRAVERSE_ALL_CHILDREN), _indent(0) 27 {} 28 29 virtual void apply(osg::Node& node) 30 { 31 for(int i = 0; i < _indent; i++) cout << " "; 32 cout << "[" << _indent << "]"<< node.libraryName() 33 << "::" << node.className() << endl; 34 35 _indent++; 36 traverse(node); 37 _indent--; 38 39 for(int i = 0; i < _indent; i++) cout << " "; 40 cout << "[" << _indent << "] "<< node.libraryName() 41 << "::" << node.className() << endl; 42 } 43 44 virtual void apply(osg::Geode& node) 45 { 46 for(int i = 0; i < _indent; i++) cout << " "; 47 cout << "[" << _indent << "] "<< node.libraryName() 48 << "::" << node.className() << endl; 49 50 _indent++; 51 52 for(unsigned int n = 0; n < node.getNumDrawables(); n++) 53 { 54 osg::Drawable* draw = node.getDrawable(n); 55 if(!draw) 56 continue; 57 for(int i = 0; i < _indent; i++) cout << " "; 58 cout << "[" << _indent << "]" << draw->libraryName() << "::" 59 << draw->className() << endl; 60 } 61 62 traverse(node); 63 _indent--; 64 65 for(int i = 0; i < _indent; i++) cout << " "; 66 cout << "[" << _indent << "]"<< node.libraryName() 67 << "::" << node.className() << endl; 68 } 69private: 70 int _indent; 71}; 72 73int main(int argc, char** argv) 74{ 75 osg::ArgumentParser parser(&argc, argv); 76 osg::Node* root = osgDB::readNodeFiles(parser); 77 78 if(!root) 79 { 80 root = osgDB::readNodeFile("avatar.osg"); 81 } 82 83 InfoVisitor infoVisitor; 84 if(root) 85 { 86 root->accept(infoVisitor); 87 } 88 89 system("pause"); 90 return 0; 91}
转自:https://www.cnblogs.com/hzhg/archive/2010/12/17/1908764.html