zoukankan      html  css  js  c++  java
  • OSG例程(3) 利用更新回调制作路径动画

    OSG程序设计教程 第七章 

    CreateMovingNode

    #include 
    <osg/MatrixTransform>
    #include 
    <osg/PositionAttitudeTransform>
    #include 
    <osg/Geode>
    #include 
    <osgDB/Registry>
    #include 
    <osgDB/ReadFile>
    #include 
    <osgGA/TrackballManipulator>
    #include 
    <osgViewer/Viewer>

    // create a animation path
    osg::AnimationPath * createAnimationPath(const osg::Vec3 &center, float radius,double looptime)
    {
        
    // declare animation path
        osg::AnimationPath * animationPath = new osg::AnimationPath;
        
    // set loop
        animationPath->setLoopMode(osg::AnimationPath::LOOP);
        
    // set the number of control points
        int numSamples = 40;
        
    float yaw = 0.0f;
        
    float yaw_delta = 2.0f * osg::PI/((float)numSamples - 1.0f);
        
    float roll = osg::inDegrees(30.0f);
        
    // set interval between control points
        double time = 0.0f;
        
    double time_delta = looptime/(double)numSamples;
        
    // insert cocntrol points
        for (int i=0; i< numSamples; ++i)
        {
            osg::Vec3 position(
    0,0,0);
            osg::Quat rotation( osg::Quat(roll, osg::Vec3(
    0.0,1.0,0.0)) * osg::Quat(-(yaw+osg::inDegrees(90.0f)), osg::Vec3(0.0,0.0,1.0)) );
            animationPath
    ->insert(time, osg::AnimationPath::ControlPoint(position,rotation));
            yaw 
    += yaw_delta;
            time 
    += time_delta;
        }
        
    return animationPath;
    }

    osg::Node 
    * createMovingModel(const osg::Vec3 &center,float radius)
    {
        
    float animationLength = 10.0f;
        
    // animation path
        osg::AnimationPath *animationPath =  createAnimationPath(center,radius,animationLength);
        osg::Group 
    * model = new osg::Group;
        
    // read model, and hide the first child node
        osg::Node * fountain = osgDB::readNodeFile("fountain.osg");
        fountain
    ->asGroup()->getChild(0)->setNodeMask(0);
        
    // if fountain, update its nanimation path
        if (fountain)
        {
            osg::PositionAttitudeTransform 
    *xform = new osg::PositionAttitudeTransform;
            
    // set update callback
            xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
            
    // add children node
            xform->addChild(fountain);
            model
    ->addChild(xform);
        }
        
    return model;
    }

    osg::Node 
    * createModel()
    {
        osg::Vec3 center(
    0.0f,0.0f,0.0f);
        
    float radius = 1.0f;
        osg::Group 
    *root = new osg::Group;
        
    // create moving node, with radius
        osg::Node *movingModel = createMovingModel(center, radius *0.8f);
        
    // add node to root
        root->addChild(movingModel);
        
    return root;
    }


    int main()
    {

        osg::ref_ptr
    <osgViewer::Viewer> viewer = new osgViewer::Viewer();

        viewer
    ->setSceneData(createModel());
        viewer
    ->setCameraManipulator(new osgGA::TrackballManipulator());

        viewer
    ->realize();
        viewer
    ->run();

    }


    TrackballManipulator貌似没有用。

  • 相关阅读:
    队列的定义与实现(C语言实现)
    在CTime类中重载&lt;&lt;和&gt;&gt;
    华为OJ:统计大写字母个数
    sql server smo
    应用服务器负载平衡集群
    存储过程如何执行的快速
    sql server 分布式事务
    代理服务器
    怎样实现数据库负载均衡集群
    多层插件开发框架
  • 原文地址:https://www.cnblogs.com/mumuliang/p/1884565.html
Copyright © 2011-2022 走看看