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. HTML - DOCTYPE

    HTML - DOCTYPE HTML 5 doctype <!DOCTYPE html> HTML4.01 Based on SGML. so the browser need the ...

  2. android 入门-动画与容器

    set 动画容器 可作为资源id添加R.anim.xxxx   可用于在样式表中添加  http://blog.csdn.net/liuhe688/article/details/6660823 in ...

  3. php+jquery+ajax实现用户名验证

    大多数情况下,jquery代码的编写,都要求我们将jquery的代码放在以下三种中任一个function里. 有三种写法,同样效果,有点像Window.onload,但也有不同,就是window.on ...

  4. Reporting Services 的伸缩性和性能表现规划(转载)

    简介 Microsoft? SQL Server? Reporting Services 是一个将集中管理的报告服务器具有的伸缩性和易管理性与基于 Web 和桌面的报告交付手段集于一身的报告平台.Re ...

  5. 谈谈网站插入youtube视频播放

    最近需要在网页首页追加视频播放功能. 需要播放youtube视频.中间遇到一些波折.特来分享一下. 首先像网页添加视频文件我们通常够采用embed标签. 标签里可以设置很多的关键子.我们可以配置为fl ...

  6. loj 1429(可相交的最小路径覆盖)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...

  7. barabasilab-networkScience学习笔记1-网络科学简介

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  8. 为什么要使用 Node.js

    这是一个移动端工程师涉足前端和后端开发的学习笔记,如有错误或理解不到位的地方,万望指正. Node.js 是什么 传统意义上的 JavaScript 运行在浏览器上,这是因为浏览器内核实际上分为两个部 ...

  9. Linux学习笔记(4)Linux常用命令之权限管理命令

    (1)chmod chmod命令用于改变文件或目录权限,英文原意为change the permissions mode of a file,所在路径为/bin/chmod,其语法格式为: chmod ...

  10. Java数组课后作业

    1.运行TestArrays.java,了解Arrays中的一些重要方法的用法. Arrays.equals(a 1, a2):判断数组是否相等. int[] b = Arrays.copyOf(a, ...