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,吼吼

  • 相关阅读:
    字集码(字符编码)
    图片轮播(可实现手动与自动的切换)
    Eclipse常用快捷键
    Java并发编程:Callable、Future和FutureTask
    Java并发之CountDownLatch、CyclicBarrier和Semaphore
    java注解
    JVM加载class原理
    阿里中间件技术及双十一实践--软负载——分布式系统的引路人
    阿里中间件技术及双十一实践--中间件总体介绍
    Java的LockSupport.park()实现分析
  • 原文地址:https://www.cnblogs.com/gattaca/p/4562708.html
Copyright © 2011-2022 走看看