2002-10-08 22:10:55 +08:00
|
|
|
#include <osgGA/AnimationPathManipulator>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace osgGA;
|
|
|
|
|
|
|
|
AnimationPathManipulator::AnimationPathManipulator(osg::AnimationPath* animationPath)
|
|
|
|
{
|
|
|
|
_animationPath = animationPath;
|
2003-04-09 04:06:37 +08:00
|
|
|
_timeOffset = 0.0;
|
|
|
|
_timeScale = 1.0;
|
|
|
|
_isPaused = false;
|
|
|
|
|
|
|
|
_realStartOfTimedPeriod = 0.0;
|
|
|
|
_animStartOfTimedPeriod = 0.0;
|
|
|
|
_numOfFramesSinceStartOfTimedPeriod = -1; // need to init.
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationPathManipulator::AnimationPathManipulator( const std::string& filename )
|
|
|
|
{
|
2002-12-16 21:40:58 +08:00
|
|
|
_animationPath = new osg::AnimationPath;
|
2002-10-08 22:10:55 +08:00
|
|
|
_animationPath->setLoopMode(osg::AnimationPath::LOOP);
|
2002-11-06 19:15:23 +08:00
|
|
|
_timeOffset = 0.0f;
|
|
|
|
_timeScale = 1.0f;
|
2003-04-09 04:06:37 +08:00
|
|
|
_isPaused = false;
|
2002-10-08 22:10:55 +08:00
|
|
|
|
|
|
|
FILE *fp = fopen( filename.c_str(), "r" );
|
|
|
|
if( fp == NULL )
|
|
|
|
{
|
2003-04-09 04:06:37 +08:00
|
|
|
osg::notify(osg::WARN) << "AnimationPathManipulator: Cannot open animation path file \"" << filename << "\".\n";
|
|
|
|
_valid = false;
|
|
|
|
return;
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|
|
|
|
while( !feof( fp ))
|
|
|
|
{
|
2003-04-09 04:06:37 +08:00
|
|
|
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] );
|
2002-10-08 22:10:55 +08:00
|
|
|
|
2003-04-09 04:06:37 +08:00
|
|
|
if( !feof(fp))
|
|
|
|
_animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|
|
|
|
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();
|
2003-04-09 18:26:50 +08:00
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
}
|
2003-04-09 18:26:50 +08:00
|
|
|
|
|
|
|
// reset the timing of the animation.
|
|
|
|
_numOfFramesSinceStartOfTimedPeriod=-1;
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() )
|
|
|
|
{
|
2003-04-09 04:06:37 +08:00
|
|
|
case GUIEventAdapter::FRAME:
|
|
|
|
if( _isPaused )
|
|
|
|
{
|
|
|
|
handleFrame( _pauseTime );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handleFrame( ea.time() );
|
|
|
|
}
|
2002-11-04 17:07:25 +08:00
|
|
|
retval = true;
|
2003-04-09 04:06:37 +08:00
|
|
|
break;
|
|
|
|
case GUIEventAdapter::KEYDOWN:
|
2002-11-06 18:24:33 +08:00
|
|
|
if (ea.getKey()==' ')
|
|
|
|
{
|
2003-04-09 21:29:12 +08:00
|
|
|
_isPaused = false;
|
2002-11-06 18:24:33 +08:00
|
|
|
home(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
2003-04-09 18:26:50 +08:00
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
2003-04-09 04:06:37 +08:00
|
|
|
else if(ea.getKey() == 'p')
|
|
|
|
{
|
|
|
|
if( _isPaused )
|
|
|
|
{
|
|
|
|
_isPaused = false;
|
|
|
|
_timeOffset -= ea.time() - _pauseTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_isPaused = true;
|
|
|
|
_pauseTime = ea.time();
|
|
|
|
}
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = false;
|
|
|
|
break;
|
2002-10-08 22:10:55 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-02-19 18:43:02 +08:00
|
|
|
void AnimationPathManipulator::getUsage(osg::ApplicationUsage& usage) const
|
|
|
|
{
|
|
|
|
usage.addKeyboardMouseBinding("AnimationPath: Space","Reset the viewing position to start of animation");
|
2003-04-09 04:06:37 +08:00
|
|
|
usage.addKeyboardMouseBinding("AnimationPath: p","Pause/resume animation.");
|
2003-02-19 18:43:02 +08:00
|
|
|
}
|
|
|
|
|
2002-10-08 22:10:55 +08:00
|
|
|
void AnimationPathManipulator::handleFrame( double time )
|
|
|
|
{
|
|
|
|
osg::AnimationPath::ControlPoint cp;
|
2003-04-09 04:06:37 +08:00
|
|
|
|
|
|
|
double animTime = (time+_timeOffset)*_timeScale;
|
|
|
|
_animationPath->getInterpolatedControlPoint( animTime, cp );
|
|
|
|
|
|
|
|
if (_numOfFramesSinceStartOfTimedPeriod==-1)
|
|
|
|
{
|
|
|
|
_realStartOfTimedPeriod = time;
|
|
|
|
_animStartOfTimedPeriod = animTime;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
++_numOfFramesSinceStartOfTimedPeriod;
|
|
|
|
|
|
|
|
double delta = (animTime-_animStartOfTimedPeriod);
|
|
|
|
if (delta>=_animationPath->getPeriod())
|
|
|
|
{
|
|
|
|
double frameRate = (double)_numOfFramesSinceStartOfTimedPeriod/delta;
|
|
|
|
std::cout <<"AnimatonPath completed in "<<delta<<" seconds, completing "<<_numOfFramesSinceStartOfTimedPeriod<<" frames,"<<std::endl;
|
|
|
|
std::cout <<" average frame rate = "<<frameRate<<std::endl;
|
|
|
|
|
|
|
|
// reset counters for next loop.
|
|
|
|
_realStartOfTimedPeriod = time;
|
|
|
|
_animStartOfTimedPeriod = animTime;
|
|
|
|
|
|
|
|
_numOfFramesSinceStartOfTimedPeriod = 0;
|
|
|
|
}
|
2002-10-08 22:10:55 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
cp.getMatrix( _matrix );
|
2002-10-08 22:10:55 +08:00
|
|
|
}
|