OSG Path Animation effect

Source: Internet
Author: User
Tags apm

OSG Path Animation effect

Transferred from: 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:p ublic 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");//Save the information
Anim->write (out);
Out.close ();
return Anim.release ();
}


BOOL Handle (const Osgga::guieventadapter &ea, Osgga::guiactionadapter &aa)
{

Osgviewer::viewer *viewer=dynamic_cast<osgviewer::viewer*> (&AMP;AA);
if (!viewer)
return false;
Switch (Ea.geteventtype ()) {
Case Osgga::guieventadapter: Finding key points:P ush://
{
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 ') {//Start roaming (you can save the manipulator here, you can return to the original state after roaming)
if (viewer)
{
Osg::ref_ptr<osgga::animationpathmanipulator>apm=new Osgga::animationpathmanipulator;
Apm->setanimationpath (Createpath ());
Viewer->setcameramanipulator (APM);
}
}
if (ea.getkey () = = ' G ' | | ea.getkey () = = ' G ') {//With the information in the saved file
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 Path Animation effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.