| 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_NODETRACKERMANIPULATOR |
|---|
| 15 | #define OSGGA_NODETRACKERMANIPULATOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgGA/MatrixManipulator> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Quat> |
|---|
| 20 | #include <osg/observer_ptr> |
|---|
| 21 | |
|---|
| 22 | namespace osgGA{ |
|---|
| 23 | |
|---|
| 24 | class OSGGA_EXPORT NodeTrackerManipulator : public MatrixManipulator |
|---|
| 25 | { |
|---|
| 26 | public: |
|---|
| 27 | |
|---|
| 28 | NodeTrackerManipulator(); |
|---|
| 29 | |
|---|
| 30 | virtual const char* className() const { return "NodeTrackerManipulator"; } |
|---|
| 31 | |
|---|
| 32 | typedef std::vector< osg::observer_ptr<osg::Node> > ObserverNodePath; |
|---|
| 33 | |
|---|
| 34 | void setTrackNodePath(const osg::NodePath& nodePath) |
|---|
| 35 | { |
|---|
| 36 | _trackNodePath.clear(); |
|---|
| 37 | _trackNodePath.reserve(nodePath.size()); |
|---|
| 38 | std::copy(nodePath.begin(), nodePath.end(), std::back_inserter(_trackNodePath)); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void setTrackNodePath(const ObserverNodePath& nodePath) { _trackNodePath = nodePath; } |
|---|
| 42 | ObserverNodePath& getTrackNodePath() { return _trackNodePath; } |
|---|
| 43 | |
|---|
| 44 | void setTrackNode(osg::Node* node); |
|---|
| 45 | osg::Node* getTrackNode() { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } |
|---|
| 46 | const osg::Node* getTrackNode() const { return _trackNodePath.empty() ? 0 : _trackNodePath.back().get(); } |
|---|
| 47 | |
|---|
| 48 | enum TrackerMode |
|---|
| 49 | { |
|---|
| 50 | /** Track the center of the node's bounding sphere, but not rotations of the node. |
|---|
| 51 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
|---|
| 52 | */ |
|---|
| 53 | NODE_CENTER, |
|---|
| 54 | /** Track the center of the node's bounding sphere, and the azimuth rotation (about the z axis of the current coordinate frame). |
|---|
| 55 | * For databases which have a CoordinateSystemNode, the orientation is kept relative the coordinate frame if the center of the node. |
|---|
| 56 | */ |
|---|
| 57 | NODE_CENTER_AND_AZIM, |
|---|
| 58 | /** Tack the center of the node's bounding sphere, and the all rotations of the node. |
|---|
| 59 | */ |
|---|
| 60 | NODE_CENTER_AND_ROTATION |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | void setTrackerMode(TrackerMode mode); |
|---|
| 64 | TrackerMode getTrackerMode() const { return _trackerMode; } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | enum RotationMode |
|---|
| 68 | { |
|---|
| 69 | /** Use a trackball style manipulation of the view direction w.r.t the tracked orientation. |
|---|
| 70 | */ |
|---|
| 71 | TRACKBALL, |
|---|
| 72 | /** Allow the elevation and azimuth angles to be adjust w.r.t the tracked orientation. |
|---|
| 73 | */ |
|---|
| 74 | ELEVATION_AZIM |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | void setRotationMode(RotationMode mode); |
|---|
| 78 | RotationMode getRotationMode() const { return _rotationMode; } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 82 | virtual void setByMatrix(const osg::Matrixd& matrix); |
|---|
| 83 | |
|---|
| 84 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 85 | virtual void setByInverseMatrix(const osg::Matrixd& matrix) { setByMatrix(osg::Matrixd::inverse(matrix)); } |
|---|
| 86 | |
|---|
| 87 | /** get the position of the manipulator as 4x4 Matrix.*/ |
|---|
| 88 | virtual osg::Matrixd getMatrix() const; |
|---|
| 89 | |
|---|
| 90 | /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/ |
|---|
| 91 | virtual osg::Matrixd getInverseMatrix() const; |
|---|
| 92 | |
|---|
| 93 | /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 94 | virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE; } |
|---|
| 95 | |
|---|
| 96 | /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/ |
|---|
| 97 | virtual float getFusionDistanceValue() const { return _distance; } |
|---|
| 98 | |
|---|
| 99 | /** Attach a node to the manipulator. |
|---|
| 100 | Automatically detaches previously attached node. |
|---|
| 101 | setNode(NULL) detaches previously nodes. |
|---|
| 102 | Is ignored by manipulators which do not require a reference model.*/ |
|---|
| 103 | virtual void setNode(osg::Node*); |
|---|
| 104 | |
|---|
| 105 | /** Return node if attached.*/ |
|---|
| 106 | virtual const osg::Node* getNode() const; |
|---|
| 107 | |
|---|
| 108 | /** Return node if attached.*/ |
|---|
| 109 | virtual osg::Node* getNode(); |
|---|
| 110 | |
|---|
| 111 | virtual void computeHomePosition(); |
|---|
| 112 | |
|---|
| 113 | /** Move the camera to the default position. |
|---|
| 114 | May be ignored by manipulators if home functionality is not appropriate.*/ |
|---|
| 115 | virtual void home(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 116 | |
|---|
| 117 | /** Start/restart the manipulator.*/ |
|---|
| 118 | virtual void init(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 119 | |
|---|
| 120 | /** handle events, return true if handled, false otherwise.*/ |
|---|
| 121 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 122 | |
|---|
| 123 | /** Get the keyboard and mouse usage of this manipulator.*/ |
|---|
| 124 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 125 | |
|---|
| 126 | protected: |
|---|
| 127 | |
|---|
| 128 | virtual ~NodeTrackerManipulator(); |
|---|
| 129 | |
|---|
| 130 | osg::NodePath getNodePath() const; |
|---|
| 131 | |
|---|
| 132 | bool validateNodePath() const; |
|---|
| 133 | |
|---|
| 134 | /** Reset the internal GUIEvent stack.*/ |
|---|
| 135 | void flushMouseEventStack(); |
|---|
| 136 | /** Add the current mouse GUIEvent to internal stack.*/ |
|---|
| 137 | void addMouseEvent(const GUIEventAdapter& ea); |
|---|
| 138 | |
|---|
| 139 | void computeNodeWorldToLocal(osg::Matrixd& worldToLocal) const; |
|---|
| 140 | void computeNodeLocalToWorld(osg::Matrixd& localToWorld) const; |
|---|
| 141 | |
|---|
| 142 | void computeNodeCenterAndRotation(osg::Vec3d& center, osg::Quat& rotation) const; |
|---|
| 143 | |
|---|
| 144 | void computePosition(const osg::Vec3d& eye,const osg::Vec3d& lv,const osg::Vec3d& up); |
|---|
| 145 | |
|---|
| 146 | /** For the give mouse movement calculate the movement of the camera. |
|---|
| 147 | Return true is camera has moved and a redraw is required.*/ |
|---|
| 148 | bool calcMovement(); |
|---|
| 149 | |
|---|
| 150 | void trackball(osg::Vec3& axis,double& angle, double p1x, double p1y, double p2x, double p2y); |
|---|
| 151 | double tb_project_to_sphere(double r, double x, double y); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** Check the speed at which the mouse is moving. |
|---|
| 155 | If speed is below a threshold then return false, otherwise return true.*/ |
|---|
| 156 | bool isMouseMoving(); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | void clampOrientation(); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | // Internal event stack comprising last two mouse events. |
|---|
| 163 | osg::ref_ptr<const GUIEventAdapter> _ga_t1; |
|---|
| 164 | osg::ref_ptr<const GUIEventAdapter> _ga_t0; |
|---|
| 165 | |
|---|
| 166 | osg::ref_ptr<osg::Node> _node; |
|---|
| 167 | |
|---|
| 168 | ObserverNodePath _trackNodePath; |
|---|
| 169 | |
|---|
| 170 | TrackerMode _trackerMode; |
|---|
| 171 | RotationMode _rotationMode; |
|---|
| 172 | |
|---|
| 173 | bool _thrown; |
|---|
| 174 | |
|---|
| 175 | osg::Quat _nodeRotation; |
|---|
| 176 | osg::Quat _rotation; |
|---|
| 177 | float _distance; |
|---|
| 178 | |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|