DriveManipulator 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_DRIVEMANIPULATOR
  14. #define OSGGA_DRIVEMANIPULATOR 1
  15. #include <osgGA/CameraManipulator>
  16. #include <osg/Quat>
  17. namespace osgGA{
  18. /**
  19. DriveManipulator is a camera manipulator which provides drive-like
  20. functionality. By default, the left mouse button accelerates, the right
  21. mouse button decelerates, and the middle mouse button (or left and
  22. right simultaneously) stops dead.
  23. */
  24. class OSGGA_EXPORT DriveManipulator : public CameraManipulator
  25. {
  26. public:
  27. DriveManipulator();
  28. virtual const char* className() const { return "Drive"; }
  29. /** Get the position of the matrix manipulator using a 4x4 Matrix.*/
  30. virtual void setByMatrix(const osg::Matrixd& matrix);
  31. /** Set the position of the matrix manipulator using a 4x4 Matrix.*/
  32. virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); }
  33. /** Get the position of the manipulator as 4x4 Matrix.*/
  34. virtual osg::Matrixd getMatrix() const;
  35. /** Get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
  36. virtual osg::Matrixd getInverseMatrix() const;
  37. virtual void setNode(osg::Node*);
  38. virtual const osg::Node* getNode() const;
  39. virtual osg::Node* getNode();
  40. virtual void computeHomePosition();
  41. virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us);
  42. virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us);
  43. virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
  44. /** Get the keyboard and mouse usage of this manipulator.*/
  45. virtual void getUsage(osg::ApplicationUsage& usage) const;
  46. void setModelScale( double in_ms ) { _modelScale = in_ms; }
  47. double getModelScale() const { return _modelScale; }
  48. void setVelocity( double in_vel ) { _velocity = in_vel; }
  49. double getVelocity() const { return _velocity; }
  50. void setHeight( double in_h ) { _height = in_h; }
  51. double getHeight() const { return _height; }
  52. protected:
  53. virtual ~DriveManipulator();
  54. bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, osg::Vec3d& intersection, osg::Vec3d& normal) const;
  55. /** Reset the internal GUIEvent stack.*/
  56. void flushMouseEventStack();
  57. /** Add the current mouse GUIEvent to internal stack.*/
  58. void addMouseEvent(const GUIEventAdapter& ea);
  59. void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up);
  60. /** For the given mouse movement calculate the movement of the camera.
  61. * Return true if camera has moved and a redraw is required.
  62. */
  63. bool calcMovement();
  64. // Internal event stack comprising last two mouse events.
  65. osg::ref_ptr<const GUIEventAdapter> _ga_t1;
  66. osg::ref_ptr<const GUIEventAdapter> _ga_t0;
  67. osg::observer_ptr<osg::Node> _node;
  68. double _modelScale;
  69. double _velocity;
  70. double _height;
  71. double _buffer;
  72. enum SpeedControlMode {
  73. USE_MOUSE_Y_FOR_SPEED,
  74. USE_MOUSE_BUTTONS_FOR_SPEED
  75. };
  76. SpeedControlMode _speedMode;
  77. osg::Vec3d _eye;
  78. osg::Quat _rotation;
  79. double _pitch;
  80. double _distance;
  81. bool _pitchUpKeyPressed;
  82. bool _pitchDownKeyPressed;
  83. };
  84. }
  85. #endif