CameraManipulator 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_CameraManipulator
  14. #define OSGGA_CameraManipulator 1
  15. #include <osg/Node>
  16. #include <osg/Matrixd>
  17. #include <osg/CoordinateSystemNode>
  18. #include <osgUtil/SceneView>
  19. #include <osgGA/Export>
  20. #include <osgGA/GUIEventHandler>
  21. #include <osgGA/GUIEventAdapter>
  22. #include <osgGA/GUIActionAdapter>
  23. namespace osgGA{
  24. #define NEW_HOME_POSITION
  25. /**
  26. CameraManipulator is an abstract base class defining the interface, and a certain
  27. amount of default functionality, for classes which wish to control OSG cameras
  28. in response to GUI events.
  29. */
  30. class OSGGA_EXPORT CameraManipulator : public GUIEventHandler
  31. {
  32. typedef GUIEventHandler inherited;
  33. public:
  34. // We are not using META_Object as this is abstract class.
  35. // Use META_Object(osgGA,YourManipulator); in your descendant non-abstract classes.
  36. virtual const char* className() const { return "CameraManipulator"; }
  37. /** callback class to use to allow matrix manipulators to query the application for the local coordinate frame.*/
  38. class CoordinateFrameCallback : public osg::Referenced
  39. {
  40. public:
  41. virtual osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const = 0;
  42. protected:
  43. virtual ~CoordinateFrameCallback() {}
  44. };
  45. /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/
  46. virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb) { _coordinateFrameCallback = cb; }
  47. /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/
  48. CoordinateFrameCallback* getCoordinateFrameCallback() { return _coordinateFrameCallback.get(); }
  49. /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/
  50. const CoordinateFrameCallback* getCoordinateFrameCallback() const { return _coordinateFrameCallback.get(); }
  51. /** get the coordinate frame.*/
  52. osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const
  53. {
  54. if (_coordinateFrameCallback.valid()) return _coordinateFrameCallback->getCoordinateFrame(position);
  55. return osg::CoordinateFrame();
  56. }
  57. osg::Vec3d getSideVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(0,0),cf(0,1),cf(0,2)); }
  58. osg::Vec3d getFrontVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(1,0),cf(1,1),cf(1,2)); }
  59. osg::Vec3d getUpVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(2,0),cf(2,1),cf(2,2)); }
  60. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  61. virtual void setByMatrix(const osg::Matrixd& matrix) = 0;
  62. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  63. virtual void setByInverseMatrix(const osg::Matrixd& matrix) = 0;
  64. /** get the position of the manipulator as 4x4 Matrix.*/
  65. virtual osg::Matrixd getMatrix() const = 0;
  66. /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
  67. virtual osg::Matrixd getInverseMatrix() const = 0;
  68. /** update the camera for the current frame, typically called by the viewer classes.
  69. Default implementation simply set the camera view matrix. */
  70. virtual void updateCamera(osg::Camera& camera) { camera.setViewMatrix(getInverseMatrix()); }
  71. /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
  72. virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::PROPORTIONAL_TO_SCREEN_DISTANCE; }
  73. /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
  74. virtual float getFusionDistanceValue() const { return 1.0f; }
  75. /** Set the mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.
  76. * The intersection traversal mask is useful for controlling what parts of the scene graph should be used for intersection purposes.*/
  77. void setIntersectTraversalMask(unsigned int mask) { _intersectTraversalMask = mask; }
  78. /** Get the mask to use when set up intersection traversal such as used in manipulators that follow terrain or have collision detection.*/
  79. unsigned int getIntersectTraversalMask() const { return _intersectTraversalMask; }
  80. /**
  81. Attach a node to the manipulator, automatically detaching any previously attached node.
  82. setNode(NULL) detaches previous nodes.
  83. May be ignored by manipulators which do not require a reference model.
  84. */
  85. virtual void setNode(osg::Node*) {}
  86. /** Return const node if attached.*/
  87. virtual const osg::Node* getNode() const { return NULL; }
  88. /** Return node if attached.*/
  89. virtual osg::Node* getNode() { return NULL; }
  90. /** Manually set the home position, and set the automatic compute of home position. */
  91. virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false)
  92. {
  93. setAutoComputeHomePosition(autoComputeHomePosition);
  94. _homeEye = eye;
  95. _homeCenter = center;
  96. _homeUp = up;
  97. }
  98. /** Get the manually set home position. */
  99. virtual void getHomePosition(osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up) const
  100. {
  101. eye = _homeEye;
  102. center = _homeCenter;
  103. up = _homeUp;
  104. }
  105. /** Set whether the automatic compute of the home position is enabled.*/
  106. virtual void setAutoComputeHomePosition(bool flag) { _autoComputeHomePosition = flag; }
  107. /** Get whether the automatic compute of the home position is enabled.*/
  108. bool getAutoComputeHomePosition() const { return _autoComputeHomePosition; }
  109. /** Compute the home position.*/
  110. virtual void computeHomePosition(const osg::Camera *camera = NULL, bool useBoundingBox = false);
  111. /** finish any active manipulator animations.*/
  112. virtual void finishAnimation() {}
  113. /**
  114. Move the camera to the default position.
  115. May be ignored by manipulators if home functionality is not appropriate.
  116. */
  117. virtual void home(const GUIEventAdapter& ,GUIActionAdapter&) {}
  118. /**
  119. Move the camera to the default position.
  120. This version does not require GUIEventAdapter and GUIActionAdapter so may be
  121. called from somewhere other than a handle() method in GUIEventHandler. Application
  122. must be aware of implications.
  123. */
  124. virtual void home(double /*currentTime*/) {}
  125. /**
  126. Start/restart the manipulator.
  127. */
  128. virtual void init(const GUIEventAdapter& ,GUIActionAdapter&) {}
  129. /** Handle event. Override the handle(..) method in your event handlers to respond to events. */
  130. virtual bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) { return GUIEventHandler::handle(event, object, nv); }
  131. /** Handle events, return true if handled, false otherwise. */
  132. virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
  133. protected:
  134. CameraManipulator();
  135. CameraManipulator(const CameraManipulator& mm, const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY);
  136. virtual ~CameraManipulator();
  137. std::string getManipulatorName() const;
  138. unsigned int _intersectTraversalMask;
  139. bool _autoComputeHomePosition;
  140. osg::Vec3d _homeEye;
  141. osg::Vec3d _homeCenter;
  142. osg::Vec3d _homeUp;
  143. osg::ref_ptr<CoordinateFrameCallback> _coordinateFrameCallback;
  144. };
  145. }
  146. #endif