| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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_MatrixManipulator |
|---|
| 15 | #define OSGGA_MatrixManipulator 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Node> |
|---|
| 18 | #include <osg/Matrixd> |
|---|
| 19 | #include <osg/CoordinateSystemNode> |
|---|
| 20 | |
|---|
| 21 | #include <osgUtil/SceneView> |
|---|
| 22 | |
|---|
| 23 | #include <osgGA/Export> |
|---|
| 24 | #include <osgGA/GUIEventHandler> |
|---|
| 25 | #include <osgGA/GUIEventAdapter> |
|---|
| 26 | #include <osgGA/GUIActionAdapter> |
|---|
| 27 | |
|---|
| 28 | namespace osgGA{ |
|---|
| 29 | |
|---|
| 30 | #define NEW_HOME_POSITION |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | |
|---|
| 34 | MatrixManipulator is an abstract base class defining the interface, and a certain |
|---|
| 35 | amount of default functionality, for classes which wish to control OSG cameras |
|---|
| 36 | in response to GUI events. |
|---|
| 37 | |
|---|
| 38 | */ |
|---|
| 39 | class OSGGA_EXPORT MatrixManipulator : public GUIEventHandler |
|---|
| 40 | { |
|---|
| 41 | public: |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | virtual const char* className() const { return "MatrixManipulator"; } |
|---|
| 45 | |
|---|
| 46 | /** callback class to use to allow matrix manipulators to querry the application for the local coordinate frame.*/ |
|---|
| 47 | class CoordinateFrameCallback : public osg::Referenced |
|---|
| 48 | { |
|---|
| 49 | public: |
|---|
| 50 | virtual osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const = 0; |
|---|
| 51 | protected: |
|---|
| 52 | virtual ~CoordinateFrameCallback() {} |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** set the minimum distance (as ratio) the eye point can be zoomed in towards the |
|---|
| 58 | center before the center is pushed forward.*/ |
|---|
| 59 | virtual void setMinimumDistance(float minimumDistance) { _minimumDistance=minimumDistance; } |
|---|
| 60 | |
|---|
| 61 | /** get the minimum distance (as ratio) the eye point can be zoomed in */ |
|---|
| 62 | float getMinimumDistance() const { return _minimumDistance; } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/ |
|---|
| 66 | virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb) { _coordinateFrameCallback = cb; } |
|---|
| 67 | |
|---|
| 68 | /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/ |
|---|
| 69 | CoordinateFrameCallback* getCoordinateFrameCallback() { return _coordinateFrameCallback.get(); } |
|---|
| 70 | |
|---|
| 71 | /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/ |
|---|
| 72 | const CoordinateFrameCallback* getCoordinateFrameCallback() const { return _coordinateFrameCallback.get(); } |
|---|
| 73 | |
|---|
| 74 | /** get the coordinate frame.*/ |
|---|
| 75 | osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const |
|---|
| 76 | { |
|---|
| 77 | if (_coordinateFrameCallback.valid()) return _coordinateFrameCallback->getCoordinateFrame(position); |
|---|
| 78 | return osg::CoordinateFrame(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | osg::Vec3d getSideVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(0,0),cf(0,1),cf(0,2)); } |
|---|
| 82 | osg::Vec3d getFrontVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(1,0),cf(1,1),cf(1,2)); } |
|---|
| 83 | osg::Vec3d getUpVector(const osg::CoordinateFrame& cf) const { return osg::Vec3d(cf(2,0),cf(2,1),cf(2,2)); } |
|---|
| 84 | |
|---|
| 85 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 86 | virtual void setByMatrix(const osg::Matrixd& matrix) = 0; |
|---|
| 87 | |
|---|
| 88 | /** set the position of the matrix manipulator using a 4x4 Matrix.*/ |
|---|
| 89 | virtual void setByInverseMatrix(const osg::Matrixd& matrix) = 0; |
|---|
| 90 | |
|---|
| 91 | /** get the position of the manipulator as 4x4 Matrix.*/ |
|---|
| 92 | virtual osg::Matrixd getMatrix() const = 0; |
|---|
| 93 | |
|---|
| 94 | /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/ |
|---|
| 95 | virtual osg::Matrixd getInverseMatrix() const = 0; |
|---|
| 96 | |
|---|
| 97 | /** Get the FusionDistanceMode. Used by SceneView for setting up setereo convergence.*/ |
|---|
| 98 | virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return osgUtil::SceneView::PROPORTIONAL_TO_SCREEN_DISTANCE; } |
|---|
| 99 | |
|---|
| 100 | /** Get the FusionDistanceValue. Used by SceneView for setting up setereo convergence.*/ |
|---|
| 101 | virtual float getFusionDistanceValue() const { return 1.0f; } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | Attach a node to the manipulator, automatically detaching any previously attached node. |
|---|
| 106 | setNode(NULL) detaches previous nodes. |
|---|
| 107 | May be ignored by manipulators which do not require a reference model. |
|---|
| 108 | */ |
|---|
| 109 | virtual void setNode(osg::Node*) {} |
|---|
| 110 | |
|---|
| 111 | /** Return const node if attached.*/ |
|---|
| 112 | virtual const osg::Node* getNode() const { return NULL; } |
|---|
| 113 | |
|---|
| 114 | /** Return node if attached.*/ |
|---|
| 115 | virtual osg::Node* getNode() { return NULL; } |
|---|
| 116 | |
|---|
| 117 | virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up) |
|---|
| 118 | { |
|---|
| 119 | _homeEye = eye; |
|---|
| 120 | _homeCenter = center; |
|---|
| 121 | _homeUp = up; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | virtual void getHomePosition(osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up) |
|---|
| 125 | { |
|---|
| 126 | _homeEye = eye; |
|---|
| 127 | _homeCenter = center; |
|---|
| 128 | _homeUp = up; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | virtual void setAutoComputeHomePosition(bool flag) { _autoComputeHomePosition = flag; } |
|---|
| 132 | |
|---|
| 133 | bool getAutoComputeHomePosition() const { return _autoComputeHomePosition; } |
|---|
| 134 | |
|---|
| 135 | virtual void computeHomePosition() |
|---|
| 136 | { |
|---|
| 137 | if(getNode()) |
|---|
| 138 | { |
|---|
| 139 | const osg::BoundingSphere& boundingSphere=getNode()->getBound(); |
|---|
| 140 | |
|---|
| 141 | setHomePosition(boundingSphere._center+osg::Vec3( 0.0,-3.5f * boundingSphere._radius,0.0f), |
|---|
| 142 | boundingSphere._center, |
|---|
| 143 | osg::Vec3(0.0f,0.0f,1.0f)); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | Move the camera to the default position. |
|---|
| 150 | May be ignored by manipulators if home functionality is not appropriate. |
|---|
| 151 | */ |
|---|
| 152 | virtual void home(const GUIEventAdapter& ,GUIActionAdapter&) {} |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | Start/restart the manipulator. |
|---|
| 156 | FIXME: what does this actually mean? Provide examples. |
|---|
| 157 | */ |
|---|
| 158 | virtual void init(const GUIEventAdapter& ,GUIActionAdapter&) {} |
|---|
| 159 | |
|---|
| 160 | /** Handle events, return true if handled, false otherwise. */ |
|---|
| 161 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); |
|---|
| 162 | |
|---|
| 163 | /** Handle visitations */ |
|---|
| 164 | virtual void accept(GUIEventHandlerVisitor& v) { v.visit(*this); } |
|---|
| 165 | |
|---|
| 166 | protected: |
|---|
| 167 | |
|---|
| 168 | MatrixManipulator(); |
|---|
| 169 | virtual ~MatrixManipulator(); |
|---|
| 170 | |
|---|
| 171 | double _minimumDistance; |
|---|
| 172 | |
|---|
| 173 | bool _autoComputeHomePosition; |
|---|
| 174 | |
|---|
| 175 | osg::Vec3d _homeEye; |
|---|
| 176 | osg::Vec3d _homeCenter; |
|---|
| 177 | osg::Vec3d _homeUp; |
|---|
| 178 | |
|---|
| 179 | osg::ref_ptr<CoordinateFrameCallback> _coordinateFrameCallback; |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | #endif |
|---|