【OSG】NodeVisitor

實現代碼

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/MatrixTransform>
#include <osg/NodeVisitor>
#include <iostream>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/ShapeDrawable>

#ifdef _DEBUG
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgViewerd.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "OpenThreads.lib")
#endif
/**
class VisitorNodePath : public osg::NodeVisitor
{
public:
    VisitorNodePath():osg::NodeVisitor(TRAVERSE_PARENTS){}
    void apply(osg::Node & node)
    {
        std::cout<<"Apply node:"<<node.getName()<<std::endl;
        if(node.getName() == "root")
        {
            //do something
        }
        traverse(node);
    }

    void apply(osg::Group & group)
    {
        std::cout<<"Apply group:"<<group.getName()<<std::endl;
        if(group.getName() == "glider")
        {
            //do something
        }
        if(group.getName() == "")
        {
            std::cout<<group.className()<<std::endl;
        }
        traverse(group);
    }
    void apply(osg::MatrixTransform & mt)
    {
        std::cout<<"Apply mtransfor:"<<mt.getName()<<std::endl;
        if(mt.getName() == "child2")
        {
            //do something
        }
        traverse(mt);
    }
};*/

osg::ref_ptr<osg::Geode> CreateBox(osg::Vec3Array *va)
{
    osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
    for(osg::Vec3Array::iterator iter = va->begin();iter !=va->end();iter++)
    {
        gnode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(iter->x(),iter->y(),iter->z()),0.02,0.02,0.02)));
        std::cout<<iter->x()<<"   :   "<<iter->y()<<"   :   "<<iter->z()<<std::endl;
    }

    return  gnode;
}

class BoundVisitor : public osg::NodeVisitor
{
public:
    void apply(osg::Geode &gnode)
    {
        int vxNum = gnode.getNumDrawables();
        osg::ref_ptr<osg::Drawable> dw;

        for(int iter = 0;iter !=vxNum;iter++)
        {
            dw = gnode.getDrawable(iter);
            osg::ref_ptr<osg::Geometry> gm = dynamic_cast<osg::Geometry*>(dw.get());
            osg::Vec3Array *vx = dynamic_cast<osg::Vec3Array*>(gm->getVertexArray());
            if(group.valid())
            {
                if(group->getName() == "root")
                {
                    group->addChild(CreateBox(vx));
                }
            }
        }
    }
    void setGroup(osg::Group *gp)
    {
        group = gp;
    }
private:
    osg::ref_ptr<osg::Group> group;
};

int main()
{
    /**
    osg::ref_ptr<osgViewer::Viewer>       viewer       = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group>              root         = new osg::Group;
    osg::ref_ptr<osg::Node>               glider       = osgDB::readNodeFile("glider.osg");
    osg::ref_ptr<osg::Node>               cow          = osgDB::readNodeFile("cow.osg");
    osg::ref_ptr<osg::MatrixTransform>    child1       = new osg::MatrixTransform;
    osg::ref_ptr<osg::MatrixTransform>    child2       = new osg::MatrixTransform;
    osg::ref_ptr<osg::MatrixTransform>    child3       = new osg::MatrixTransform;
    osg::ref_ptr<osg::MatrixTransform>    child4       = new osg::MatrixTransform;
    osg::ref_ptr<osg::MatrixTransform>    child5       = new osg::MatrixTransform;
    VisitorNodePath vn;

    root->setName("root");
    glider->setName("glider");
    cow->setName("cow");

    root->addChild(glider);//root->glider
    root->addChild(child1);
    root->addChild(child2);


    child1->setName("child1");
    child1->setMatrix(osg::Matrix::translate(-5,-5,0));
    child1->addChild(glider);
    child1->addChild(child3);
    child1->addChild(child4);


    child2->setName("child2");
    child2->setMatrix(osg::Matrix::translate(5,-5,0));
    child2->addChild(glider);
    child2->addChild(child5);

    child3->setName("child3");
    child3->setMatrix(osg::Matrix::translate(-5,-5,0));
    child3->addChild(cow);

    child4->setName("child4");
    child4->setMatrix(osg::Matrix::translate(5,-5,0));
    child4->addChild(cow);

    child5->setName("child5");
    child5->setMatrix(osg::Matrix::translate(5,-5,0));
    child5->addChild(cow);

    viewer->setSceneData(root);
    cow->accept(vn);
    //viewer->addEventHandler(new ChangeWindow());
    return viewer->run();*/

    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Node> glider = osgDB::readNodeFile("glider.osg");
     BoundVisitor bv;
    osg::ref_ptr<osg::Group> group = new osg::Group;
    group->setName("root");
  
    bv.setGroup(group);
    glider->accept(bv);
    group->addChild(glider);

    viewer->setSceneData(group);
    //viewer->addEventHandler(new changewindow());
    return viewer->run();
}

實現效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章