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

时间: 2024-08-25 02:47:04

OSG程序设计之osg::NodeVisitor的相关文章

OSG程序设计之osg::Group

以下是一个简单的模型读取程序: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osg/Node> void main() { osgViewer::Viewer viewer; osg::Group *root = new osg::Group(); root->addChild(osgDB::readNodeFile("glider.osg")); root-&

OSG程序设计之Hello World

对于从未接触过OSG的我来说,首先需要一个入门教程.在OSG论坛逛了半天,再加上google,最终决定使用<OSG程序设计>这本书. 下面就贴出书中的第一个例子:Hello World. 使用vs2010新建一个win32控制台应用程序,然后配置一下osg环境,接下来就可以上代码了. //库文件:osgDBd.lib.osgViewerd.lib #include <osgDB/ReadFile> #include <osgViewer/Viewer> void mai

OSG程序设计之更新回调

更新回调(Update Callback)涉及到一个类:osg::NodeCallback.这个类重载了函数调用操作符.当回调动作发生时,将会执行这一操作符的内容. 如果节点绑定了更新回调函数,那么在每一帧系统遍历到此节点时,回调函数都会被调用. 下面给出一个例子: #include <osg/io_utils> #include <osg/PositionAttitudeTransform> #include <osgDB/ReadFile> #include <

OSG程序设计之Hello World 4.0

代码如下: //需要添加两个库:osgUtild.lib.osgTextd.lib #include <osgDB/ReadFile> #include <osgUtil/Optimizer> #include <osg/CoordinateSystemNode> #include <osg/Switch> #include <osgText/Text> #include <osgViewer/Viewer> #include <

OSG程序设计之Hello World 2.0

现在为Hello World添加一些键盘响应事件. //需要多添加两个库:osgGAd.lib.osgd.lib 代码如下: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgGA/StateSetManipulator> void main() { osgViewer::Viewer view

Qt + OpenSenceGraph(osg) 加载OSG模型

原文地址:https://www.cnblogs.com/herd/p/9944716.html

osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标

class MyNodeVisitor:public osg::NodeVisitor { pulic: MyNodeVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {} void apply(osg::Geode& geode) { //计算当前geode节点对应的世界变换矩阵,用来计算geode中顶点对应的世界坐标 osg::Matrix geodeMatrix=osg::computeLocalToWo

OSG开发概览(转载)

OSG开发概览 1 OSG基础知识 Ø OSG是Open Scene Graphic 的缩写,OSG于1997年诞生于以为滑翔机爱好者之手,Don burns  为了对滑翔机的飞行进行模拟,对openGL的库进行了封装,osg的雏形就这样诞生了,1998年Don burns 遇到了同样喜欢滑翔机和计算机图形学的Robert Osfield ,从此Robert Osfield加入了osg小组的开发并一直担任开发小组的组长. Ø OSG不但有openGL的跨平台的特性和较高的渲染性能,还提供了一系列

OSG 3.0 三维视景仿真技术开发详解

第一章 OSG三维渲染引擎概述 OSG的主要功能包括以下几个方面: 1. 可以实时高效地绘制和控制使用建模软件所建立的3D模型, 如3DMAX.MAYA.Creator等制作的3D模型, 该功能是场景渲染的基本功能. 2. 支持多种外设, 如操作杆.游戏柄.轨迹球.方向盘.键盘鼠标等. 3. 除了传统的二维屏幕上进行三维展示外, OSG还可以完成红绿偏移的立体投影, 实现真正的立体展示. 4. 支持骨骼动画.关键帧动画.颜色动画等各种流行的动画. OSG的相关扩展: OSG的相关扩展,OSG针对