AnimationPathManipulator 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGGA_ANIMATION_PATH_MANIPULATOR
  14. #define OSGGA_ANIMATION_PATH_MANIPULATOR 1
  15. #include <osg/AnimationPath>
  16. #include <osg/Notify>
  17. #include <osgGA/CameraManipulator>
  18. namespace osgGA{
  19. //
  20. // The AnimationPathManipulator is a Matrix Manipulator that reads an
  21. // animation path from a file and plays it back. The file is expected
  22. // to be ascii and a succession of lines with 8 floating point values
  23. // per line. The succession of values are:
  24. // time px py pz ax ay az aw
  25. // where:
  26. // time = elapsed time in seconds from the beginning of the animation
  27. // px py pz = World position in cartesian coordinates
  28. // ax ay az aw = Orientation (attitude) defined as a quaternion
  29. class OSGGA_EXPORT AnimationPathManipulator : public CameraManipulator
  30. {
  31. public:
  32. AnimationPathManipulator( osg::AnimationPath* animationPath=0 );
  33. AnimationPathManipulator( const std::string& filename );
  34. virtual const char* className() const { return "AnimationPath"; }
  35. void setTimeScale(double s) { _timeScale = s; }
  36. double getTimeScale() const { return _timeScale; }
  37. void setTimeOffset(double o) { _timeOffset = o; }
  38. double getTimeOffset() const { return _timeOffset; }
  39. struct AnimationCompletedCallback : public virtual osg::Referenced
  40. {
  41. virtual void completed(const AnimationPathManipulator* apm) = 0;
  42. };
  43. void setAnimationCompletedCallback(AnimationCompletedCallback* acc) { _animationCompletedCallback = acc; }
  44. AnimationCompletedCallback* getAnimationCompletedCallback() { return _animationCompletedCallback.get(); }
  45. const AnimationCompletedCallback* getAnimationCompletedCallback() const { return _animationCompletedCallback.get(); }
  46. void setPrintOutTimingInfo(bool printOutTimingInfo) { _printOutTimingInfo=printOutTimingInfo; }
  47. bool getPrintOutTimingInfo() const { return _printOutTimingInfo; }
  48. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  49. virtual void setByMatrix(const osg::Matrixd& matrix) { _matrix = matrix; }
  50. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  51. virtual void setByInverseMatrix(const osg::Matrixd& matrix) { _matrix.invert(matrix); }
  52. /** get the position of the manipulator as 4x4 Matrix.*/
  53. virtual osg::Matrixd getMatrix() const { return _matrix; }
  54. /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
  55. virtual osg::Matrixd getInverseMatrix() const { return osg::Matrixd::inverse(_matrix); }
  56. void setAnimationPath( osg::AnimationPath* animationPath ) { _animationPath=animationPath; }
  57. osg::AnimationPath* getAnimationPath() { return _animationPath.get(); }
  58. const osg::AnimationPath* getAnimationPath() const { return _animationPath.get(); }
  59. bool valid() const { return _animationPath.valid(); }
  60. void init(const GUIEventAdapter& ea,GUIActionAdapter& us);
  61. void home(const GUIEventAdapter& ea,GUIActionAdapter& us);
  62. void home(double currentTime);
  63. virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
  64. /** Get the keyboard and mouse usage of this manipulator.*/
  65. virtual void getUsage(osg::ApplicationUsage& usage) const;
  66. protected:
  67. bool _valid;
  68. bool _printOutTimingInfo;
  69. void handleFrame( double time );
  70. osg::ref_ptr<osg::AnimationPath> _animationPath;
  71. double _timeOffset;
  72. double _timeScale;
  73. osg::ref_ptr<AnimationCompletedCallback> _animationCompletedCallback;
  74. double _pauseTime;
  75. bool _isPaused;
  76. double _realStartOfTimedPeriod;
  77. double _animStartOfTimedPeriod;
  78. int _numOfFramesSinceStartOfTimedPeriod;
  79. osg::Matrixd _matrix;
  80. };
  81. }
  82. #endif