OSG 自定义数据类型 关键帧动画

转自:http://blog.csdn.net/zhuyingqingfen/article/details/12651017

/*
1.创建一个AnimManager一般继承于osg::NodeCallback
2.在AnimManager中创建一个采样器sampler(例如Vec3LinearSampler,有各种sammpler)
3. sammpler 配置了各种Interpolator(插值器,如Vec3LinearInterpolator)
4.sampler 中有KeyframeContainer(关键帧容器,如Vec3KeyframeContainer)
5.向keyframeContainer中插入对应的关键帧(如Vec3Keyframe 关键帧中包含时间戳和对应的数据类型(如Vec3));
6.取值 sampler->getValueAt(time t,p1) t 为时间,p1为对应的数据类型如Vec3,当时间超出sampler的结束时间,或取值永远是最后一帧。
7.取出的p1 用到你想用的地方。
*/ #include <iostream>
#include <osg/io_utils>
#include <osg/Geometry>
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/MatrixTransform>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgAnimation/Sampler> class MyNode
{
public:
MyNode(std::string f,float x,float y,float z)
{
id = f;
_v = osg::Vec3(x,y,z);
}
MyNode(){ id = "";}
MyNode( const MyNode&no)
{
id = no.id;
_v = no._v; }
MyNode & operator = (const MyNode&no)
{
id = no.id;
_v = no._v; return *this;
}
const MyNode operator * (float v) const //不能返回引用,因为no是临时对象
{
MyNode no;
no.id = id;
no._v = _v*v;
return no;
}
inline float operator * (const MyNode rhs) const
{
return _v*rhs._v;
} inline MyNode operator + (const MyNode & rhs) const//不能返回引用,因为no是临时对象
{
MyNode no;
no.id = id;
no._v = _v+rhs._v;
return no;
}
std::string id;
osg::Vec3 _v;
};
typedef osgAnimation::TemplateKeyframe<MyNode> MyNodeKeyframe;
typedef osgAnimation::TemplateKeyframeContainer<MyNode> MyNodeKeyframeContainer;
typedef osgAnimation::TemplateLinearInterpolator<MyNode, MyNode> MyNodeLinearInterpolator;
typedef osgAnimation::TemplateSampler<MyNodeLinearInterpolator> MyNodeLinearSampler; class AnimManager : public osg::NodeCallback
{
public: AnimManager()
{
_sampler = new MyNodeLinearSampler;
_playing = false;
_lastUpdate = 0; }
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR &&
nv->getFrameStamp() &&
nv->getFrameStamp()->getFrameNumber() != _lastUpdate)
{ _lastUpdate = nv->getFrameStamp()->getFrameNumber(); _currentTime = osg::Timer::instance()->tick(); if (_playing && _sampler.get() && _sampler->getKeyframeContainer())
{
osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);
if (transform) {
osg::Vec3 result;
float t = osg::Timer::instance()->delta_s(_startTime, _currentTime);
//float duration = _sampler->getEndTime() - _sampler->getStartTime();
//t = fmod(t, duration);//循环 if(t> _sampler->getEndTime())
{
stop();
} MyNode no; _sampler->getValueAt(t, no );
result = no._v; std::cout<<no.id<<" "<<result.x()<<" "<<result.y()<<" "<<result.z()<<std::endl;
transform->setMatrix(osg::Matrix::translate(result));
}
}
} traverse(node,nv);
} void start() { _startTime = osg::Timer::instance()->tick(); _currentTime = _startTime; _playing = true;}
void stop() { _currentTime = _startTime; _playing = false;} osg::ref_ptr<MyNodeLinearSampler> _sampler;
osg::Timer_t _startTime;
osg::Timer_t _currentTime;
bool _playing;
unsigned int _lastUpdate;
};
osg::MatrixTransform* setupAnimtkNode(osg::Geode* staticGeode)
{
osg::MatrixTransform* node = new osg::MatrixTransform(); AnimManager* callback = new AnimManager();
MyNodeKeyframeContainer* keys = callback->_sampler->getOrCreateKeyframeContainer(); keys->push_back( MyNodeKeyframe(0.0,MyNode("A",1,0,0)));
keys->push_back( MyNodeKeyframe(1.0,MyNode("B",5,0,9)));
keys->push_back( MyNodeKeyframe(2.0,MyNode("C",1,8,0)));
keys->push_back( MyNodeKeyframe(3.0,MyNode("D",1,8,-5))); callback->start();
node->setUpdateCallback(callback); osg::Geode* geode = new osg::Geode();
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 2))); node->addChild(geode); return node;
} int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
osgViewer::Viewer viewer(arguments); osgGA::TrackballManipulator* tbm = new osgGA::TrackballManipulator(); viewer.setCameraManipulator(tbm); viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.addEventHandler(new osgViewer::WindowSizeHandler()); osg::Group* root = new osg::Group();
osg::Geode* geode = new osg::Geode(); root->setInitialBound(osg::BoundingSphere(osg::Vec3(10,0,20), 50));
root->addChild(setupAnimtkNode(geode));
root->addChild(geode); viewer.setSceneData(root);
return viewer.run();
}

  

OSG 自定义数据类型 关键帧动画的更多相关文章

  1. Silverlight动画的基本知识、关键帧动画

    基础知识 (一)动画:是快速播放一系列图像(其中每个图像与下一个图像略微不同)给人造成的一种幻觉 (二)动画类型:两类    (1)From/To/By动画:在起始值和结束值之间进行动画处理.     ...

  2. Core Animation中的关键帧动画

    键帧动画就是在动画控制过程中开发者指定主要的动画状态,至于各个状态间动画如何进行则由系统自动运算补充(每两个关键帧之间系统形成的动画称为“补间动画”),这种动画的好处就是开发者不用逐个控制每个动画帧, ...

  3. ios基础动画、关键帧动画、动画组、转场动画等

    概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...

  4. 【WPF学习】第五十四章 关键帧动画

    到目前为止,看到的所有动画都使用线性插值从起点到终点.但如果需要创建具有多个分段的动画和不规则移动的动画.例如,可能希望创建一个动画,快速地将一个元素滑入到视图中,然后慢慢地将它移到正确位置.可通过创 ...

  5. 关键帧动画:@keyframes

    关键帧动画:@keyframes: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  6. 通过SQL Server自定义数据类型实现导入数据

    写在前面 在看同事写的代码时看到了SQL Server中可以自定义数据类型,而且定义的是DataTable类型的数据类型. 后我想起了以前我们导入数据时要么是循环insert写入,要么是SqlBulk ...

  7. 背水一战 Windows 10 (14) - 动画: 线性动画, 关键帧动画

    [源码下载] 背水一战 Windows 10 (14) - 动画: 线性动画, 关键帧动画 作者:webabcd 介绍背水一战 Windows 10 之 动画 线性动画 - ColorAnimatio ...

  8. iOS开发UI篇—核心动画(关键帧动画)

    转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...

  9. Swift - 使用CAKeyframeAnimation实现关键帧动画

    1,CAKeyframeAnimation介绍 CAKeyframeAnimation可以实现关键帧动画,这个类可以实现某一属性按照一串的数值进行动画,就像是一帧一帧的制作出来一样.   2,使用样例 ...

随机推荐

  1. 攻城狮在路上(叁)Linux(十八)--- 文件系统的简单操作

    本篇仅作为补漏. 一.查看磁盘和目录的容量:df  du df:列出文件系统的整体磁盘使用量. du:评估文件系统的磁盘使用量(常用于评估目录所占容量) 二.连接文件:ln 1.hard link:硬 ...

  2. hdu 4023 2011上海赛区网络赛C 贪心+模拟

    以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...

  3. 关于socket——SO_SNDBUF and SO_RECVBUF

    转自:http://blog.csdn.net/wf1982/article/details/38871521 参见 http://stackoverflow.com/questions/425741 ...

  4. Android ViewPager使用详解(转)

    这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...

  5. JNI数据类型(转)

    本文原创,转载请注明出处:http://blog.csdn.net/qinjuning 在Java存在两种数据类型: 基本类型 和 引用类型 ,大家都懂的 . 在JNI的世界里也存在类似的数据类型,与 ...

  6. 在Salesforce中通过 Debug Log 方式 跟踪逻辑流程

    在Salesforce中通过 Debug Log方式 跟踪逻辑流程 具体位置如下所示: Setup ---> Logs ---> Debug Logs ---> Monitored ...

  7. hdu 5833 Zhu and 772002 高斯消元

    Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...

  8. 《大话》之 装饰模式 Vs 建造者模式

    一.简介: 装饰模式:     背景:小菜要见美女娇娇,感慨自己不会着装,怕给娇娇留下坏印象               内容:动态的给一个对象添加一些额外职责               图文并茂: ...

  9. pythonpython-eggs异常解决方法

    : UserWarning: /home/server/.python-eggs is writable by group/others and vulnerable to attack when u ...

  10. Streaming data from Oracle using Oracle GoldenGate and Kafka Connect

    This is a guest blog from Robin Moffatt. Robin Moffatt is Head of R&D (Europe) at Rittman Mead, ...