1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGGA_CameraManipulator
15#define OSGGA_CameraManipulator 1
19#include <osg/CoordinateSystemNode>
21#include <osgUtil/SceneView>
23#include <osgGA/Export>
24#include <osgGA/GUIEventHandler>
25#include <osgGA/GUIEventAdapter>
26#include <osgGA/GUIActionAdapter>
30#define NEW_HOME_POSITION
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.
39class OSGGA_EXPORT CameraManipulator : public GUIEventHandler
41 typedef GUIEventHandler inherited;
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"; }
49 /** callback class to use to allow matrix manipulators to query the application for the local coordinate frame.*/
50 class CoordinateFrameCallback : public osg::Referenced
53 virtual osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const = 0;
55 virtual ~CoordinateFrameCallback() {}
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; }
61 /** get the coordinate frame callback which tells the manipulator which way is up, east and north.*/
62 CoordinateFrameCallback* getCoordinateFrameCallback() { return _coordinateFrameCallback.get(); }
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(); }
67 /** get the coordinate frame.*/
68 osg::CoordinateFrame getCoordinateFrame(const osg::Vec3d& position) const
70 if (_coordinateFrameCallback.valid()) return _coordinateFrameCallback->getCoordinateFrame(position);
71 return osg::CoordinateFrame();
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)); }
78 /** set the position of the matrix manipulator using a 4x4 Matrix.*/
79 virtual void setByMatrix(const osg::Matrixd& matrix) = 0;
81 /** set the position of the matrix manipulator using a 4x4 Matrix.*/
82 virtual void setByInverseMatrix(const osg::Matrixd& matrix) = 0;
84 /** get the position of the manipulator as 4x4 Matrix.*/
85 virtual osg::Matrixd getMatrix() const = 0;
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;
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()); }
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; }
97 /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
98 virtual float getFusionDistanceValue() const { return 1.0f; }
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; }
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; }
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.
112 virtual void setNode(osg::Node*) {}
114 /** Return const node if attached.*/
115 virtual const osg::Node* getNode() const { return NULL; }
117 /** Return node if attached.*/
118 virtual osg::Node* getNode() { return NULL; }
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)
123 setAutoComputeHomePosition(autoComputeHomePosition);
125 _homeCenter = center;
129 /** Get the manually set home position. */
130 virtual void getHomePosition(osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up) const
133 center = _homeCenter;
137 /** Set whether the automatic compute of the home position is enabled.*/
138 virtual void setAutoComputeHomePosition(bool flag) { _autoComputeHomePosition = flag; }
140 /** Get whether the automatic compute of the home position is enabled.*/
141 bool getAutoComputeHomePosition() const { return _autoComputeHomePosition; }
143 /** Compute the home position.*/
144 virtual void computeHomePosition(const osg::Camera *camera = NULL, bool useBoundingBox = false);
146 /** finish any active manipulator animations.*/
147 virtual void finishAnimation() {}
150 Move the camera to the default position.
151 May be ignored by manipulators if home functionality is not appropriate.
153 virtual void home(const GUIEventAdapter& ,GUIActionAdapter&) {}
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.
161 virtual void home(double /*currentTime*/) {}
164 Start/restart the manipulator.
166 virtual void init(const GUIEventAdapter& ,GUIActionAdapter&) {}
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); }
171 /** Handle events, return true if handled, false otherwise. */
172 virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
177 CameraManipulator(const CameraManipulator& mm, const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY);
179 virtual ~CameraManipulator();
181 std::string getManipulatorName() const;
183 unsigned int _intersectTraversalMask;
185 bool _autoComputeHomePosition;
188 osg::Vec3d _homeCenter;
191 osg::ref_ptr<CoordinateFrameCallback> _coordinateFrameCallback;