From Romano José Magacho da Silva, added 'p' toggle to animation path

manipulator to allow animations to be paused and resumed.

Added frame rate stats which are output on each repetition of the path.
This commit is contained in:
Robert Osfield 2003-04-08 20:06:37 +00:00
parent 06e773cefb
commit c3fd384eb1
2 changed files with 84 additions and 24 deletions

View File

@ -66,8 +66,14 @@ class OSGGA_EXPORT AnimationPathManipulator : public CameraManipulator
osg::ref_ptr<osg::AnimationPath> _animationPath; osg::ref_ptr<osg::AnimationPath> _animationPath;
double _timeOffset; double _timeOffset;
double _timeScale; double _timeScale;
double _pauseTime;
bool _isPaused;
double _realStartOfTimedPeriod;
double _animStartOfTimedPeriod;
int _numOfFramesSinceStartOfTimedPeriod;
}; };

View File

@ -6,8 +6,13 @@ using namespace osgGA;
AnimationPathManipulator::AnimationPathManipulator(osg::AnimationPath* animationPath) AnimationPathManipulator::AnimationPathManipulator(osg::AnimationPath* animationPath)
{ {
_animationPath = animationPath; _animationPath = animationPath;
_timeOffset = 0.0f; _timeOffset = 0.0;
_timeScale = 1.0f; _timeScale = 1.0;
_isPaused = false;
_realStartOfTimedPeriod = 0.0;
_animStartOfTimedPeriod = 0.0;
_numOfFramesSinceStartOfTimedPeriod = -1; // need to init.
} }
AnimationPathManipulator::AnimationPathManipulator( const std::string& filename ) AnimationPathManipulator::AnimationPathManipulator( const std::string& filename )
@ -16,25 +21,26 @@ AnimationPathManipulator::AnimationPathManipulator( const std::string& filename
_animationPath->setLoopMode(osg::AnimationPath::LOOP); _animationPath->setLoopMode(osg::AnimationPath::LOOP);
_timeOffset = 0.0f; _timeOffset = 0.0f;
_timeScale = 1.0f; _timeScale = 1.0f;
_isPaused = false;
FILE *fp = fopen( filename.c_str(), "r" ); FILE *fp = fopen( filename.c_str(), "r" );
if( fp == NULL ) if( fp == NULL )
{ {
osg::notify(osg::WARN) << "AnimationPathManipulator: Cannot open animation path file \"" << filename << "\".\n"; osg::notify(osg::WARN) << "AnimationPathManipulator: Cannot open animation path file \"" << filename << "\".\n";
_valid = false; _valid = false;
return; return;
} }
while( !feof( fp )) while( !feof( fp ))
{ {
double time; double time;
osg::Vec3 position; osg::Vec3 position;
osg::Quat rotation; osg::Quat rotation;
fscanf( fp, "%lf %f %f %f %f %f %f %f\n", fscanf( fp, "%lf %f %f %f %f %f %f %f\n",
&time, &position[0], &position[1], &position[2], &time, &position[0], &position[1], &position[2],
&rotation[0], &rotation[1], &rotation[2], &rotation[3] ); &rotation[0], &rotation[1], &rotation[2], &rotation[3] );
if( !feof(fp)) if( !feof(fp))
_animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); _animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
} }
fclose(fp); fclose(fp);
} }
@ -61,11 +67,18 @@ bool AnimationPathManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GU
bool retval = false; bool retval = false;
switch( ea.getEventType() ) switch( ea.getEventType() )
{ {
case GUIEventAdapter::FRAME: case GUIEventAdapter::FRAME:
handleFrame( ea.time() ); if( _isPaused )
{
handleFrame( _pauseTime );
}
else
{
handleFrame( ea.time() );
}
retval = true; retval = true;
break; break;
case GUIEventAdapter::KEYDOWN: case GUIEventAdapter::KEYDOWN:
if (ea.getKey()==' ') if (ea.getKey()==' ')
{ {
home(ea,us); home(ea,us);
@ -73,10 +86,25 @@ bool AnimationPathManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GU
us.requestContinuousUpdate(false); us.requestContinuousUpdate(false);
return true; return true;
} }
return false; else if(ea.getKey() == 'p')
{
retval = false; if( _isPaused )
break; {
_isPaused = false;
_timeOffset -= ea.time() - _pauseTime;
}
else
{
_isPaused = true;
_pauseTime = ea.time();
}
us.requestRedraw();
us.requestContinuousUpdate(false);
return true;
}
retval = false;
break;
default: default:
break; break;
} }
@ -86,12 +114,38 @@ bool AnimationPathManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GU
void AnimationPathManipulator::getUsage(osg::ApplicationUsage& usage) const void AnimationPathManipulator::getUsage(osg::ApplicationUsage& usage) const
{ {
usage.addKeyboardMouseBinding("AnimationPath: Space","Reset the viewing position to start of animation"); usage.addKeyboardMouseBinding("AnimationPath: Space","Reset the viewing position to start of animation");
usage.addKeyboardMouseBinding("AnimationPath: p","Pause/resume animation.");
} }
void AnimationPathManipulator::handleFrame( double time ) void AnimationPathManipulator::handleFrame( double time )
{ {
osg::AnimationPath::ControlPoint cp; osg::AnimationPath::ControlPoint cp;
_animationPath->getInterpolatedControlPoint( (time+_timeOffset)*_timeScale, cp );
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;
}
osg::Matrix matrix; osg::Matrix matrix;
cp.getMatrix( matrix ); cp.getMatrix( matrix );