zoukankan      html  css  js  c++  java
  • OSG程序设计之osg::NodeVisitor

      本文所有内容来自《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,吼吼

  • 相关阅读:
    Ubuntu18.04+CUDA+CUDNN+Pytorch环境配置
    VIM入门必读(转)
    简述C和C++的学习历程(转)
    队列
    排序实现
    十进制转二进制
    北邮1467二叉树题引起的思考
    计算机是如何启动的?
    c语言字符串操作实现
    北邮机试——huffman权值问题
  • 原文地址:https://www.cnblogs.com/gattaca/p/4562708.html
Copyright © 2011-2022 走看看