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. 字节流、字符串、16进制字符串转换__Java(转)

    /** * @Package: * @ClassName:TypeConversion * @Description:字节流.字符串.16进制字符串转换 * @author:xk * @date:Ja ...

  2. 用PowerShell脚本实现对SharePoint页面Title的修改

    存在这样一种情况,对应的page已经部署到product的SharePoint环境中,那么在部署下一个版本的时候就不允许把已经创建好的page删除再创建,因此page中修改过的属性就不能再次部署到Sh ...

  3. 软引用SoftReference异步加载图片

    HashMap<String, SoftReference<Drawable>> imageCache 关于SoftReference这个类多少知道些机制,会用就ok了. 机制 ...

  4. SQL Server2008函数大全(完整版)

    SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合. 1. 字符串函数 函数 名称 参数 示例 说明 ascii(字符串表达式) select ascii('abc') 返回 97 返 ...

  5. LoadRunner检查点学习实例

    LoadRunner只会检测脚本中事务的执行状态,而实际的事务执行结果则需要通过检查点来完成. 例如一个登录事务,LR只关心事务本身的执行状态,也就是说哪怕实际操作密码错误产生登录失败的业务操作,其事 ...

  6. nginx日志中文变成类型\xE9\xA6\x96\xE9\xA1\xB5-\xE6\x8E\xA8\xE8\x8D\x90的东西

    感谢 http://my.oschina.net/leejun2005/blog/106791 代码如下: public class App { public static String str2He ...

  7. LoadRunner检查点

    web_reg_find("Text=ABC", "SaveCount=abc_count", LAST);51Testing软件测试网V?2Rs.J Gmdw ...

  8. CSS3_loading效果

    写个div给他个基本样式: <body> <div class="load-container load" id="loader" > ...

  9. Console.log,Window.alert,Document.write三者区别

    1.Console.log不会阻断程序继续进行,在控制台可以看到测试结果. 2.Window.alert弹出框会阻断程序运行,在弹出框可以看到测试结果. 3.Document.write不会阻断程序继 ...

  10. js从字符串中提取身份证号,连续18位数字

    <!DOCTYPE html> <html> <head> <title>提取身份证号</title> <meta charset=& ...