OSG programming of osg :: NodeVisitor

  This article all the content from the "OpenSceneGraph 3D rendering engine design and Practice," a book.

  This article focuses on is the OSG node access.

  The access node for receiving an access node from the beginning, the user of a node in an accept () function, a specific object passed to the access node.

  The second step, in turn, performs access node's apply () function and pass their own access control.

  This two-step process can be achieved with a very simple line of code to the function expression:

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

  The following is a specific example of the access node:

#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::cost<<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;
}

  Continue to learn OSG, Hou Hou

Reproduced in: https: //www.cnblogs.com/gattaca/p/4562708.html

Guess you like

Origin blog.csdn.net/weixin_34310785/article/details/93401840