日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

osg节点访问和遍历

發(fā)布時間:2023/12/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osg节点访问和遍历 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

 
  OSG中節(jié)點的訪問使用的是一種訪問器模式。
一個典型的訪問器涉及抽象訪問者角色(Visitor), 具體訪問者(Concrete Visitor), 節(jié)點角色(Node)。

OSG中訪問者角色為NodeVisitor類,其基本結(jié)構(gòu)如下:
  NodeVisitor(TraversalMode tm)??? //構(gòu)造函數(shù),TraversalMode為節(jié)點樹的遍歷方式
                   //TRAVERSE_NONE, 僅當(dāng)前節(jié)點
                   //TRAVERSE_PARENTS, 向當(dāng)前節(jié)點的父節(jié)點遍歷
                   //TRAVERSE_ALL_CHILDREN, 向子節(jié)點遍歷
  void traverse(Node& node)  //向下一個需要訪問的節(jié)點推進(jìn)
  void apply(Node& node)   //虛函數(shù),訪問各種節(jié)點類型,并執(zhí)行訪問器中的自定義操作
  void apply(Group& node)
  void apply(Geode& node)
  …………

NodeVisitor 只是訪問器角色的抽象接口,要使用訪問器訪問節(jié)點并執(zhí)行自定義操作時,需要繼承并重寫
apply(……)函數(shù)實現(xiàn)自定義功能。osg::Node類中的訪問接口為 void accept(NodeVisitor& nv)。對節(jié)點
的訪問從節(jié)點接受一個訪問器開始,將一個具體的訪問器對象傳遞給節(jié)點,節(jié)點反過來執(zhí)行訪問器的apply(...)
函數(shù),并將自己傳入訪問器。可如下簡單表示:
void Node::accept(NodeVisitor& nv) ?//節(jié)點訪問與遍歷從這個函數(shù)開始實施
{
? ? if (nv.validNodeMask(*this))?
? ? {
? ? ? ? nv.pushOntoNodePath(this); ?//將節(jié)點Node加入到一個Vector<Node*>中
? ? ? ? nv.apply(*this);
? ? ? ? nv.popFromNodePath();
? ? }
}

void NodeVisitor::apply(Node& node) ?//基類的虛函數(shù)中沒有實現(xiàn)操作,只需進(jìn)行遍歷
{
? ? traverse(node);
}

遍歷節(jié)點樹:
 osg::Node類中有兩個輔助函數(shù):
  void ascend(NodeVisitor& nv)???? //虛函數(shù),向上一級節(jié)點推進(jìn)訪問器
  void traverse(NodeVisitor& nv)?? //虛函數(shù),向下一級節(jié)點推進(jìn)訪問器
??NodeVisitor的traverse()函數(shù)實現(xiàn)如下:
  inline void traverse(Node& node)
??????{
??????????? if (_traversalMode == TRAVERSE_PARENTS)?
        node.ascend(*this);
??????????? else if (_traversalMode != TRAVERSE_NONE)?
        node.traverse(*this); ? //調(diào)用子集的accept函數(shù),關(guān)聯(lián)訪問器進(jìn)行遍歷操作
??????}

Node類中的traverse()為空,具體實現(xiàn)是在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;}

?

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的osg节点访问和遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。