您的位置:首页 > Web前端 > Node.js

OSG程序设计之osg::NodeVisitor

2015-06-09 10:13 771 查看
  本文所有内容来自《OpenSceneGraph三维渲染引擎设计与实践》一书。

  本文主要讨论的是OSG中节点的访问。

  对于节点的访问是从节点接收一个访问器开始的,用户执行某个节点的accept()函数,将一个具体的访问器对象传递给节点。

  第二步,节点反过来执行访问器的apply()函数,并将自身传入访问器。

  这两步的实现过程可以用一行十分简单的函数代码来表达:

void Node::accept(NodeVisitor& nv)
{
nv.apply(*this);
}


  下面是一个具体的访问节点的例子:

#include <osg/Node>
#include <osgDB/ReadFile>
#include <iostream>

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)
std::cout<<" ";
std::cout<<"["<<_indent + 1<<"]"<<node.libraryName()
<<"::"<<node.className()<<std::endl;

_indent++;
traverse(node);
_indent--;
}

virtual void apply(osg::Geode &node)
{
for (int i = 0; i < _indent; ++i)
std::cout<<" ";
std::cout<<"["<<_indent + 1<<"]"<<node.libraryName()
<<"::"<<node.className()<<std::endl;

for (unsigned int n = 0; n < node.getNumDrawables(); ++n)
{
osg::Drawable *drawable = node.getDrawable(n);
if (!drawable)
continue;
for (int i = 0; i < _indent; ++i)
std::cout<<" ";
std::cout<<drawable->libraryName()
<<"::"<<drawable->className()<<std::endl;
}
_indent++;
traverse(node);
_indent--;
}
protected:
int _indent;
};

int main()
{
osg::Node *root = osgDB::readNodeFile("osgcool.osgt");
InfoVisitor infoVisitor;
if(root)
root->accept(infoVisitor);
return 0;
}


  继续学习OSG,吼吼
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: