osg 路径 动画 效果

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

#include <osg/Group>
#include <osg/ShapeDrawable>

#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>

#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector>
#include <osgGA/GUIEventHandler>
#include <osgGA/AnimationPathManipulator>

#include <iostream>
using namespace std;

class PickEventHandle :public osgGA::GUIEventHandler
{
public:
PickEventHandle(){
_points=new osg::Vec3Array;
}
virtual ~PickEventHandle(){
}
osg::Geode*createBox(osg::Vec3 center){
osg::ref_ptr<osg::Geode>geode=new osg::Geode;
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
osg::ref_ptr<osg::ShapeDrawable>sd=new osg::ShapeDrawable(new osg::Box(center,5,5,5));
sd->setColor(osg::Vec4(1,0,0,1));
geode->addDrawable(sd);
return geode.release();
}
float getRunTime(osg::Vec3 res,osg::Vec3 des){
float xx=(res.x()-des.x())*(res.x()-des.x());
float yy=(res.y()-des.y())*(res.y()-des.y());
float zz=(res.z()-des.z())*(res.z()-des.z());
float distant=sqrt(xx+yy+zz);
return distant*0.1;
}

osg::AnimationPath*createPath(){
osg::ref_ptr<osg::AnimationPath>anim=new osg::AnimationPath;
anim->setLoopMode(osg::AnimationPath::LOOP);

float time=0.0;
float angle=0.0;
float roll=1.57;//osg::inDegrees(90);

if(_points.valid()){
osg::Vec3Array::iterator iter=_points->begin();
for(;;){
osg::Vec3 pos(*iter);
iter++;
if(iter!=_points->end()) {
if(iter->x() > pos.x())
{
angle=1.57-atan((iter->y()-pos.y())/(iter->x()-pos.x()));
if(angle<0)
angle+=1.57;
}
else {
angle=-(1.57+atan((iter->y()-pos.y())/(iter->x()-pos.x())));
if(angle>0){
angle=-(1.57-angle);
}

}

osg::Quat rotate (osg::Quat(roll,osg::Vec3(1.0,0,0))*osg::Quat(-angle,osg::Vec3(0,0,1)));

anim->insert(time,osg::AnimationPath::ControlPoint(pos,rotate));
time+=getRunTime(pos, *iter);
}
else {
break;
}
}
}

ofstream out("/root/a.path");//把信息保存
anim->write(out);
out.close();
return anim.release();
}

bool handle(const osgGA::GUIEventAdapter &ea ,osgGA::GUIActionAdapter &aa)
{

osgViewer::Viewer *viewer=dynamic_cast<osgViewer::Viewer*>(&aa);
if(!viewer)
return false;
switch (ea.getEventType()){
case osgGA::GUIEventAdapter::PUSH://寻找关键点
{
osgUtil::LineSegmentIntersector::Intersections inters;
if(viewer->computeIntersections(ea.getX(),ea.getY(),inters)){
osgUtil::LineSegmentIntersector::Intersections::iterator iter=inters.begin();
osg::Vec3d pos=iter->getWorldIntersectPoint();
cout<<pos.x()<<" "<<pos.y()<<" "<<pos.z()<<endl;
_points->push_back(osg::Vec3(pos.x(),pos.y(),3));
viewer->getSceneData()->asGroup()->addChild(createBox(pos));
}
}
case osgGA::GUIEventAdapter::KEYDOWN:{
if(ea.getKey()=='f' || ea.getKey()=='F'){//启动漫游(可以在这保存下操纵器,漫游完毕可以返回原来状态
if(viewer)
{
osg::ref_ptr<osgGA::AnimationPathManipulator>apm=new osgGA::AnimationPathManipulator;
apm->setAnimationPath(createPath());
viewer->setCameraManipulator(apm);
}
}
if(ea.getKey()=='g' || ea.getKey()=='G'){//用已经保存的文件里的信息
osg::ref_ptr<osgGA::AnimationPathManipulator>apm=new osgGA::AnimationPathManipulator("/root/a.path");
viewer->setCameraManipulator(apm);
}
}
break;
}
return false;

}

private:
osg::ref_ptr<osg::Vec3Array> _points;
};

int main(int argc, char *argv[])
{

osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
viewer->setSceneData(osgDB::readNodeFile("ceep.ive"));
viewer->addEventHandler(new PickEventHandle());
viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
viewer->run();
}

osg 路径 动画 效果的更多相关文章

  1. SVG的路径动画效果

    使用SVG animateMotion实现的一个动画路径效果,相关代码如下. 在线调试唯一地址:http://www.gbtags.com/gb/debug/c88f4099-5056-4ad7-af ...

  2. SVG路径动画解密

    原文:SVG路径动画解密 原文链接:http://www.gbtags.com/gb/share/5581.htm SVG路径动画效果现在貌似越来越多网站都使用了,给我的感觉就像是一段时间的流行而已, ...

  3. 探秘神奇的运动路径动画 Motion Path

    CSS 中有一个非常有意思的模块 -- CSS Motion Path Module Level 1,翻译过来也就是运动路径.本文将对 motion path 一探究竟,通过本文,你可以了解到: 什么 ...

  4. canvas绘制折线路径动画

    最近有读者加我微信咨询这个问题: 其中的效果是一个折线路径动画效果,如下图所示: 要实现以上路径动画,一般可以使用svg的动画功能.或者使用canvas绘制,结合路径数学计算来实现. 如果用canva ...

  5. 纯CSS实现帅气的SVG路径描边动画效果(转载)

    本文转载自: 纯CSS实现帅气的SVG路径描边动画效果

  6. iOS简单动画效果:闪烁、移动、旋转、路径、组合

    #define kDegreesToRadian(x) (M_PI * (x) / 180.0) #define kRadianToDegrees(radian) (radian*180.0)/(M_ ...

  7. DrawSVG - SVG 路径动画 jQuery 插件

    jQuery DrawSVG 使用了 jQuery 内置的动画引擎实现 SVG 路径动画,用到了 stroke-dasharray 和 stroke-dashoffset 属性.DrawSVG 是完全 ...

  8. iOS开发 QQ粘性动画效果

    QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00  博客园精华区 原文  http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...

  9. iOS基本动画/关键帧动画/利用缓动函数实现物理动画效果

    先说下基本动画部分 基本动画部分比较简单, 但能实现的动画效果也很局限 使用方法大致为: #1. 创建原始UI或者画面 #2. 创建CABasicAnimation实例, 并设置keypart/dur ...

随机推荐

  1. 攻城狮在路上(叁)Linux(十三)--- 文件与目录的管理

    一.查看文件与目录: 1.命令格式:ls [-aAdfFhilnrRSt] 目录名称; 2.参数说明: -a:显示所有的文件,包括隐藏文件(隐藏文件一般以.开头). -d:仅列出目录本身. -l:列出 ...

  2. Win10 UAP 绑定

    Compiled DataBinding in Windows Universal Applications (UAP) http://nicksnettravels.builttoroam.com/ ...

  3. php的时间输出格式

    php中时间一般分为两种格式,一种是标准时间格式timestamp,即Y-m-d G:i:s.另一种就是时间戳. 例如: 一.标准时间与时间戳转换: //获得服务端系统时间 date_default_ ...

  4. 配置ogg异构oracle-mysql 双向同步注意事项

    双向同步需要考虑的是怎么解决循环复制,以及同时更新一张表以谁为基准. 配置过程就不写了,大致和oracle到mysql的单向+mysql到oracle的单向差不多. 需要注意的有如下几点: 1.ora ...

  5. HTML5——摒弃插件和前端框架的异步文件上传

    之前我从来没有体会到HTML5的便利,直到这次需要一个异步上传的功能功能.一开始我以为文件的一些声明必须为HTML5才管用,后来才知道添加了很多以前没有的标签,并可以直接播放视频,音频等.可以不再使用 ...

  6. 【jQuery 冻结任意行列】冻结任意行和列的jQuery插件

    实现原理: 创建多个div,div之间通过css实现层叠,每个div放置当前表格的克隆.例如:需要行冻结时,创建存放冻结行表格的div,通过设置z-index属性和position属性,让冻结行表格在 ...

  7. windows mysql 自动备份的几种方法

    转自:http://www.cnblogs.com/liongis/archive/2013/03/12/2956573.html 1.复制date文件夹备份===================== ...

  8. 解读Web Page Diagnostics网页细分图

    解读Web Page Diagnostics网页细分图 http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostic ...

  9. git学习 分支特殊处理和配置03

    Bug分支: 当在一个分支上工作的时候:突然到其它分支修复bug,当前分支工作还没到要提交的程度:这时候可以使用git stash来将工作分支暂时存储起来: 用git stash list查看stas ...

  10. HTTP基础08--追加协议

    消除 HTTP 瓶颈的 SPDY HTTP 的瓶颈 Web 网站为了保存这些新增内容,在很短的时间内就会发生大量的内容更新;为了尽可能实时地显示这些更新的内容,服务器上一有内容更新,就需要直接把那些内 ...