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