FirstPersonManipulator 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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. * FirstPersonManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
  14. * while some pieces of code were taken from OSG.
  15. * Thanks to company Cadwork (www.cadwork.ch) and
  16. * Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
  17. */
  18. #ifndef OSGGA_FIRST_PERSON_MANIPULATOR
  19. #define OSGGA_FIRST_PERSON_MANIPULATOR 1
  20. #include <osgGA/StandardManipulator>
  21. namespace osgGA {
  22. /** FirstPersonManipulator is base class for camera control based on position
  23. and orientation of camera, like walk, drive, and flight manipulators. */
  24. class OSGGA_EXPORT FirstPersonManipulator : public StandardManipulator
  25. {
  26. typedef StandardManipulator inherited;
  27. public:
  28. FirstPersonManipulator( int flags = DEFAULT_SETTINGS );
  29. FirstPersonManipulator( const FirstPersonManipulator& fpm,
  30. const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
  31. META_Object( osgGA, FirstPersonManipulator );
  32. virtual void setByMatrix( const osg::Matrixd& matrix );
  33. virtual void setByInverseMatrix( const osg::Matrixd& matrix );
  34. virtual osg::Matrixd getMatrix() const;
  35. virtual osg::Matrixd getInverseMatrix() const;
  36. virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation );
  37. virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up );
  38. virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const;
  39. virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const;
  40. virtual void setVelocity( const double& velocity );
  41. inline double getVelocity() const;
  42. virtual void setAcceleration( const double& acceleration, bool relativeToModelSize = false );
  43. double getAcceleration( bool *relativeToModelSize = NULL ) const;
  44. virtual void setMaxVelocity( const double& maxVelocity, bool relativeToModelSize = false );
  45. double getMaxVelocity( bool *relativeToModelSize = NULL ) const;
  46. virtual void setWheelMovement( const double& wheelMovement, bool relativeToModelSize = false );
  47. double getWheelMovement( bool *relativeToModelSize = NULL ) const;
  48. virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  49. virtual void home( double );
  50. virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  51. protected:
  52. virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  53. virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
  54. virtual bool performMouseDeltaMovement( const float dx, const float dy );
  55. virtual void applyAnimationStep( const double currentProgress, const double prevProgress );
  56. virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
  57. void moveForward( const double distance );
  58. void moveForward( const osg::Quat& rotation, const double distance );
  59. void moveRight( const double distance );
  60. void moveUp( const double distance );
  61. osg::Vec3d _eye;
  62. osg::Quat _rotation;
  63. double _velocity;
  64. double _acceleration;
  65. static int _accelerationFlagIndex;
  66. double _maxVelocity;
  67. static int _maxVelocityFlagIndex;
  68. double _wheelMovement;
  69. static int _wheelMovementFlagIndex;
  70. class FirstPersonAnimationData : public AnimationData {
  71. public:
  72. osg::Quat _startRot;
  73. osg::Quat _targetRot;
  74. void start( const osg::Quat& startRotation, const osg::Quat& targetRotation, const double startTime );
  75. };
  76. virtual void allocAnimationData() { _animationData = new FirstPersonAnimationData(); }
  77. };
  78. //
  79. // inline methods
  80. //
  81. /// Returns velocity.
  82. double FirstPersonManipulator::getVelocity() const { return _velocity; }
  83. }
  84. #endif /* OSGGA_FIRST_PERSON_MANIPULATOR */