osg节点访问和遍历
?
OSG中節點的訪問使用的是一種訪問器模式。
一個典型的訪問器涉及抽象訪問者角色(Visitor), 具體訪問者(Concrete Visitor), 節點角色(Node)。
OSG中訪問者角色為NodeVisitor類,其基本結構如下:
NodeVisitor(TraversalMode tm)??? //構造函數,TraversalMode為節點樹的遍歷方式
//TRAVERSE_NONE, 僅當前節點
//TRAVERSE_PARENTS, 向當前節點的父節點遍歷
//TRAVERSE_ALL_CHILDREN, 向子節點遍歷
void traverse(Node& node) //向下一個需要訪問的節點推進
void apply(Node& node) //虛函數,訪問各種節點類型,并執行訪問器中的自定義操作
void apply(Group& node)
void apply(Geode& node)
…………
NodeVisitor 只是訪問器角色的抽象接口,要使用訪問器訪問節點并執行自定義操作時,需要繼承并重寫
apply(……)函數實現自定義功能。osg::Node類中的訪問接口為 void accept(NodeVisitor& nv)。對節點
的訪問從節點接受一個訪問器開始,將一個具體的訪問器對象傳遞給節點,節點反過來執行訪問器的apply(...)
函數,并將自己傳入訪問器。可如下簡單表示:
void Node::accept(NodeVisitor& nv) ?//節點訪問與遍歷從這個函數開始實施
{
? ? if (nv.validNodeMask(*this))?
? ? {
? ? ? ? nv.pushOntoNodePath(this); ?//將節點Node加入到一個Vector<Node*>中
? ? ? ? nv.apply(*this);
? ? ? ? nv.popFromNodePath();
? ? }
}
void NodeVisitor::apply(Node& node) ?//基類的虛函數中沒有實現操作,只需進行遍歷
{
? ? traverse(node);
}
遍歷節點樹:
osg::Node類中有兩個輔助函數:
void ascend(NodeVisitor& nv)???? //虛函數,向上一級節點推進訪問器
void traverse(NodeVisitor& nv)?? //虛函數,向下一級節點推進訪問器
??NodeVisitor的traverse()函數實現如下:
inline void traverse(Node& node)
??????{
??????????? if (_traversalMode == TRAVERSE_PARENTS)?
node.ascend(*this);
??????????? else if (_traversalMode != TRAVERSE_NONE)?
node.traverse(*this); ? //調用子集的accept函數,關聯訪問器進行遍歷操作
??????}
Node類中的traverse()為空,具體實現是在Group類中
void Group::traverse(NodeVisitor& nv)
{
? ? for(NodeList::iterator itr=_children.begin();
? ? ? ? itr!=_children.end();
? ? ? ? ++itr)
? ? {
? ? ? ? (*itr)->accept(nv);
? ? }
}
?
示例代碼 #include <osg/Node> #include <osgDB/ReadFile> #include <iostream>using namespace std;class InfoVisitor: public osg::NodeVisitor{public:InfoVisitor():osg::NodeVisitor(TRAVERSE_ALL_CHILDREN), _indent(0){}virtual void apply(osg::Node& node){for(int i = 0; i < _indent; i++) cout << " ";cout << "[" << _indent << "]"<< node.libraryName()<< "::" << node.className() << endl;_indent++;traverse(node);_indent--;for(int i = 0; i < _indent; i++) cout << " ";cout << "[" << _indent << "] "<< node.libraryName()<< "::" << node.className() << endl;}virtual void apply(osg::Geode& node){for(int i = 0; i < _indent; i++) cout << " ";cout << "[" << _indent << "] "<< node.libraryName()<< "::" << node.className() << endl;_indent++;for(unsigned int n = 0; n < node.getNumDrawables(); n++){osg::Drawable* draw = node.getDrawable(n);if(!draw)continue;for(int i = 0; i < _indent; i++) cout << " ";cout << "[" << _indent << "]" << draw->libraryName() << "::" << draw->className() << endl;}traverse(node);_indent--;for(int i = 0; i < _indent; i++) cout << " ";cout << "[" << _indent << "]"<< node.libraryName()<< "::" << node.className() << endl;}private:int _indent;};int main(int argc, char** argv){osg::ArgumentParser parser(&argc, argv);osg::Node* root = osgDB::readNodeFiles(parser);if(!root){root = osgDB::readNodeFile("2.OSGB");}InfoVisitor infoVisitor;if(root){root->accept(infoVisitor);}system("pause");return 0;}?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的osg节点访问和遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长生劫绝世将军怎么困(长生最新章节)
- 下一篇: 一零计划节奏榜