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. JetBrains发布了IntelliJ IDEA 2016.1

    JetBrains日前发布了IntelliJ IDEA 2016.1,即他们最受欢迎的IDE的最新版本.这个新版本应该是考虑了多语言开发者的需求,其在许多语言和技术都有很多的增强:然而最惹人注目的变化 ...

  2. oracle pctfree和pctused详解

    一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...

  3. BAT批量处理 命令

    第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令统称批处理命令.小知识:可以 ...

  4. Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)

    题目链接:  http://codeforces.com/problemset/problem/14/D 思路:直接枚举每一天路径的两端,然后求以每一端为树根的树上最长路径,然后相乘就可以了. # ...

  5. ios编码转换 国标 UTF-8

    我们知道,使用NSURLConnection的代理方法下载网页,存到一个NSData中, NSMutableData *pageData; [pageData appendData:data]; 如果 ...

  6. 认识B/S架构

    Browser/Server即浏览器/服务器模式. Web浏览器主要功能 1. 申请服务,包括服务器的Ip地址和文件 2. 从服务器上下载 3. 解析下载的文件 4. 用过http协议进行通信 Web ...

  7. 15 个 Android 通用流行框架大全

      1. 缓存 名称 描述 DiskLruCache Java实现基于LRU的磁盘缓存 2.图片加载 名称 描述 Android Universal Image Loader 一个强大的加载,缓存,展 ...

  8. hdu1712 分组背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1712 题意:有n门课程,和m天时间,完成mp[i][j]得到的价值为第i行j列的数字,求最 ...

  9. hdu1106 排序水题

    Problem Description 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整 ...

  10. xor方程组消元 UVA 11542 Square

    题目传送门 题意:给n个数,选择一些数字乘积为平方数的选择方案数.训练指南题目. 分析:每一个数字分解质因数.比如4, 6, 10, 15,, , , , 令,表示选择第i个数字,那么,如果p是平方数 ...