2002-10-08 22:10:55 +08:00
|
|
|
#include <osgGA/AnimationPathManipulator>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace osgGA;
|
|
|
|
|
|
|
|
AnimationPathManipulator::AnimationPathManipulator(osg::AnimationPath* animationPath)
|
|
|
|
{
|
|
|
|
_animationPath = animationPath;
|
2002-11-06 18:24:33 +08:00
|
|
|
_timeOffset = 0.0f;
|
|
|
|
_timeScale = 1.0f;
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationPathManipulator::AnimationPathManipulator( const std::string& filename )
|
|
|
|
{
|
|
|
|
_animationPath = osgNew osg::AnimationPath;
|
|
|
|
_animationPath->setLoopMode(osg::AnimationPath::LOOP);
|
|
|
|
|
|
|
|
FILE *fp = fopen( filename.c_str(), "r" );
|
|
|
|
if( fp == NULL )
|
|
|
|
{
|
|
|
|
osg::notify(osg::WARN) << "AnimationPathManipulator: Cannot open animation path file \"" << filename << "\".\n";
|
|
|
|
_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while( !feof( fp ))
|
|
|
|
{
|
|
|
|
double time;
|
|
|
|
osg::Vec3 position;
|
|
|
|
osg::Quat rotation;
|
|
|
|
fscanf( fp, "%lf %f %f %f %f %f %f %f\n",
|
|
|
|
&time, &position[0], &position[1], &position[2],
|
|
|
|
&rotation[0], &rotation[1], &rotation[2], &rotation[3] );
|
|
|
|
|
|
|
|
if( !feof(fp))
|
|
|
|
_animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
void AnimationPathManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter&)
|
|
|
|
{
|
|
|
|
if (_animationPath.valid())
|
|
|
|
{
|
|
|
|
_timeOffset = _animationPath->getFirstTime()-ea.time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationPathManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& aa)
|
|
|
|
{
|
|
|
|
home(ea,aa);
|
|
|
|
}
|
|
|
|
|
2002-10-08 22:10:55 +08:00
|
|
|
bool AnimationPathManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
|
|
|
|
{
|
|
|
|
if( !valid() ) return false;
|
|
|
|
|
|
|
|
us = us;
|
|
|
|
|
|
|
|
bool retval = false;
|
|
|
|
switch( ea.getEventType() )
|
|
|
|
{
|
|
|
|
case GUIEventAdapter::FRAME:
|
|
|
|
handleFrame( ea.time() );
|
2002-11-04 17:07:25 +08:00
|
|
|
retval = true;
|
2002-10-08 22:10:55 +08:00
|
|
|
break;
|
|
|
|
case GUIEventAdapter::KEYBOARD:
|
2002-11-06 18:24:33 +08:00
|
|
|
if (ea.getKey()==' ')
|
|
|
|
{
|
|
|
|
home(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
2002-11-04 17:07:25 +08:00
|
|
|
retval = false;
|
2002-10-08 22:10:55 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationPathManipulator::handleFrame( double time )
|
|
|
|
{
|
|
|
|
osg::AnimationPath::ControlPoint cp;
|
2002-11-06 18:24:33 +08:00
|
|
|
_animationPath->getInterpolatedControlPoint( (time+_timeOffset)*_timeScale, cp );
|
2002-10-08 22:10:55 +08:00
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
osg::Matrix matrix;
|
|
|
|
cp.getMatrix( matrix );
|
|
|
|
|
|
|
|
if (_camera.valid())
|
|
|
|
{
|
|
|
|
_camera->home();
|
|
|
|
_camera->transformLookAt(matrix);
|
|
|
|
}
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|