| 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 | |
|---|
| 14 | #ifndef OSGGA_TRACKBALLMANIPULATOR |
|---|
| 15 | #define OSGGA_TRACKBALLMANIPULATOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgGA/MatrixManipulator> |
|---|
| 18 | #include <osg/Quat> |
|---|
| 19 | |
|---|
| 20 | namespace osgGA{ |
|---|
| 21 | |
|---|
| 22 | class OSGGA_EXPORT TrackballManipulator : public MatrixManipulator |
|---|
| 23 | { |
|---|
| 24 | public: |
|---|
| 25 | TrackballManipulator(); |
|---|
| 26 | |
|---|
| 27 | virtual const char* className() const { return "Trackball"; } |
|---|
| 28 | |
|---|
| 29 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 30 | virtual void setByMatrix(const osg::Matrixd& matrix); |
|---|
| 31 | |
|---|
| 32 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 33 | virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); } |
|---|
| 34 | |
|---|
| 35 | /** get the position of the manipulator as 4x4 Matrix.*/ |
|---|
| 36 | virtual osg::Matrixd getMatrix() const; |
|---|
| 37 | |
|---|
| 38 | /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/ |
|---|
| 39 | virtual osg::Matrixd getInverseMatrix() const; |
|---|
| 40 | |
|---|
| 41 | /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 42 | virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; } |
|---|
| 43 | |
|---|
| 44 | /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 45 | virtual float getFusionDistanceValue() const { return _distance; } |
|---|
| 46 | |
|---|
| 47 | /** Attach a node to the manipulator. |
|---|
| 48 | Automatically detaches previously attached node. |
|---|
| 49 | setNode(NULL) detaches previously nodes. |
|---|
| 50 | Is ignored by manipulators which do not require a reference model.*/ |
|---|
| 51 | virtual void setNode(osg::Node*); |
|---|
| 52 | |
|---|
| 53 | /** Return node if attached.*/ |
|---|
| 54 | virtual const osg::Node* getNode() const; |
|---|
| 55 | |
|---|
| 56 | /** Return node if attached.*/ |
|---|
| 57 | virtual osg::Node* getNode(); |
|---|
| 58 | |
|---|
| 59 | /** Move the camera to the default position. |
|---|
| 60 | May be ignored by manipulators if home functionality is not appropriate.*/ |
|---|
| 61 | virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 62 | virtual void home(double); |
|---|
| 63 | |
|---|
| 64 | /** Start/restart the manipulator.*/ |
|---|
| 65 | virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 66 | |
|---|
| 67 | /** handle events, return true if handled, false otherwise.*/ |
|---|
| 68 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 69 | |
|---|
| 70 | /** Get the keyboard and mouse usage of this manipulator.*/ |
|---|
| 71 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /** set the minimum distance (as ratio) the eye point can be zoomed in towards the |
|---|
| 75 | center before the center is pushed forward.*/ |
|---|
| 76 | void setMinimumZoomScale(double minimumZoomScale) { _minimumZoomScale=minimumZoomScale; } |
|---|
| 77 | |
|---|
| 78 | /** get the minimum distance (as ratio) the eye point can be zoomed in */ |
|---|
| 79 | double getMinimumZoomScale() const { return _minimumZoomScale; } |
|---|
| 80 | |
|---|
| 81 | /** Set the center of the trackball. */ |
|---|
| 82 | void setCenter(const osg::Vec3d& center) { _center = center; } |
|---|
| 83 | |
|---|
| 84 | /** Get the center of the trackball. */ |
|---|
| 85 | const osg::Vec3d& getCenter() const { return _center; } |
|---|
| 86 | |
|---|
| 87 | /** Set the rotation of the trackball. */ |
|---|
| 88 | void setRotation(const osg::Quat& rotation) { _rotation = rotation; } |
|---|
| 89 | |
|---|
| 90 | /** Get the rotation of the trackball. */ |
|---|
| 91 | const osg::Quat& getRotation() const { return _rotation; } |
|---|
| 92 | |
|---|
| 93 | /** Set the distance of the trackball. */ |
|---|
| 94 | void setDistance(double distance) { _distance = distance; } |
|---|
| 95 | |
|---|
| 96 | /** Get the distance of the trackball. */ |
|---|
| 97 | double getDistance() const { return _distance; } |
|---|
| 98 | |
|---|
| 99 | /** Set the size of the trackball. */ |
|---|
| 100 | void setTrackballSize(float size); |
|---|
| 101 | |
|---|
| 102 | /** Get the size of the trackball. */ |
|---|
| 103 | float getTrackballSize() const { return _trackballSize; } |
|---|
| 104 | protected: |
|---|
| 105 | |
|---|
| 106 | virtual ~TrackballManipulator(); |
|---|
| 107 | |
|---|
| 108 | /** Reset the internal GUIEvent stack.*/ |
|---|
| 109 | void flushMouseEventStack(); |
|---|
| 110 | /** Add the current mouse GUIEvent to internal stack.*/ |
|---|
| 111 | void addMouseEvent(const GUIEventAdapter& ea); |
|---|
| 112 | |
|---|
| 113 | void computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up); |
|---|
| 114 | |
|---|
| 115 | /** For the give mouse movement calculate the movement of the camera. |
|---|
| 116 | Return true is camera has moved and a redraw is required.*/ |
|---|
| 117 | bool calcMovement(); |
|---|
| 118 | |
|---|
| 119 | void trackball(osg::Vec3& axis,float& angle, float p1x, float p1y, float p2x, float p2y); |
|---|
| 120 | float tb_project_to_sphere(float r, float x, float y); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** Check the speed at which the mouse is moving. |
|---|
| 124 | If speed is below a threshold then return false, otherwise return true.*/ |
|---|
| 125 | bool isMouseMoving(); |
|---|
| 126 | |
|---|
| 127 | // Internal event stack comprising last two mouse events. |
|---|
| 128 | osg::ref_ptr<const GUIEventAdapter> _ga_t1; |
|---|
| 129 | osg::ref_ptr<const GUIEventAdapter> _ga_t0; |
|---|
| 130 | |
|---|
| 131 | osg::ref_ptr<osg::Node> _node; |
|---|
| 132 | |
|---|
| 133 | double _modelScale; |
|---|
| 134 | double _minimumZoomScale; |
|---|
| 135 | |
|---|
| 136 | bool _thrown; |
|---|
| 137 | |
|---|
| 138 | osg::Vec3d _center; |
|---|
| 139 | osg::Quat _rotation; |
|---|
| 140 | double _distance; |
|---|
| 141 | float _trackballSize; |
|---|
| 142 | |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | #endif |
|---|
| 148 | |
|---|