2010-05-27 21:59:34 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
2010-05-25 20:05:13 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*
|
|
|
|
* StandardManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
|
2010-05-27 21:59:34 +08:00
|
|
|
* while some pieces of code were taken from OSG.
|
2010-05-25 20:05:13 +08:00
|
|
|
* Thanks to company Cadwork (www.cadwork.ch) and
|
|
|
|
* Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGGA_CAMERA_MANIPULATOR
|
|
|
|
#define OSGGA_CAMERA_MANIPULATOR 1
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
#include <osgGA/CameraManipulator>
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace osgGA {
|
|
|
|
|
|
|
|
|
|
|
|
/** StandardManipulator class provides basic functionality
|
|
|
|
for user controlled manipulation.*/
|
2010-05-27 23:54:37 +08:00
|
|
|
class OSGGA_EXPORT StandardManipulator : public CameraManipulator
|
2010-05-25 20:05:13 +08:00
|
|
|
{
|
2010-05-27 23:54:37 +08:00
|
|
|
typedef CameraManipulator inherited;
|
2010-05-26 00:27:21 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// flags
|
|
|
|
enum UserInteractionFlags
|
|
|
|
{
|
|
|
|
UPDATE_MODEL_SIZE = 0x01,
|
|
|
|
COMPUTE_HOME_USING_BBOX = 0x02,
|
|
|
|
PROCESS_MOUSE_WHEEL = 0x04,
|
2010-06-14 23:20:47 +08:00
|
|
|
SET_CENTER_ON_WHEEL_FORWARD_MOVEMENT = 0x08,
|
2010-06-16 23:56:42 +08:00
|
|
|
DEFAULT_SETTINGS = UPDATE_MODEL_SIZE /*| COMPUTE_HOME_USING_BBOX*/ | PROCESS_MOUSE_WHEEL
|
2010-05-26 00:27:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
StandardManipulator( int flags = DEFAULT_SETTINGS );
|
|
|
|
StandardManipulator( const StandardManipulator& m,
|
2010-05-26 01:02:11 +08:00
|
|
|
const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
|
2010-05-26 00:27:21 +08:00
|
|
|
|
|
|
|
// We are not using META_Object as this is abstract class.
|
|
|
|
// Use META_Object(osgGA,YourManipulator); in your descendant non-abstract classes.
|
|
|
|
virtual const char* className() const { return "StandardManipulator"; }
|
|
|
|
|
2010-08-16 19:03:24 +08:00
|
|
|
/** Sets manipulator by eye position and eye orientation.*/
|
2010-05-26 00:27:21 +08:00
|
|
|
virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation ) = 0;
|
2010-08-16 19:03:24 +08:00
|
|
|
|
|
|
|
/** Sets manipulator by eye position, center of rotation, and up vector.*/
|
|
|
|
virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up ) = 0;
|
|
|
|
|
|
|
|
/** Gets manipulator's eye position and eye orientation.*/
|
2010-05-26 00:27:21 +08:00
|
|
|
virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const = 0;
|
2010-08-16 19:03:24 +08:00
|
|
|
|
|
|
|
/** Gets manipulator's focal center, eye position, and up vector.*/
|
|
|
|
virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const = 0;
|
2010-05-26 00:27:21 +08:00
|
|
|
|
|
|
|
virtual void setNode( osg::Node* );
|
|
|
|
virtual const osg::Node* getNode() const;
|
|
|
|
virtual osg::Node* getNode();
|
|
|
|
|
|
|
|
virtual void setVerticalAxisFixed( bool value );
|
|
|
|
inline bool getVerticalAxisFixed() const;
|
2010-06-14 23:20:47 +08:00
|
|
|
inline bool getAllowThrow() const;
|
|
|
|
virtual void setAllowThrow( bool allowThrow );
|
2010-05-26 00:27:21 +08:00
|
|
|
|
|
|
|
virtual void setAnimationTime( const double t );
|
|
|
|
double getAnimationTime() const;
|
|
|
|
bool isAnimating() const;
|
|
|
|
virtual void finishAnimation();
|
|
|
|
|
|
|
|
virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual void home( double );
|
|
|
|
|
|
|
|
virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual void getUsage( osg::ApplicationUsage& usage ) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual bool handleFrame( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleResize( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMouseMove( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMouseDrag( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMousePush( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMouseRelease( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleKeyDown( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleKeyUp( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool handleMouseDeltaMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
|
|
|
|
virtual bool performMovement();
|
2010-05-27 21:59:34 +08:00
|
|
|
virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
|
|
|
|
virtual bool performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy );
|
|
|
|
virtual bool performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy );
|
2010-05-26 00:27:21 +08:00
|
|
|
virtual bool performMouseDeltaMovement( const float dx, const float dy );
|
|
|
|
virtual bool performAnimationMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual void applyAnimationStep( const double currentProgress, const double prevProgress );
|
|
|
|
|
|
|
|
void addMouseEvent( const osgGA::GUIEventAdapter& ea );
|
|
|
|
void flushMouseEventStack();
|
|
|
|
virtual bool isMouseMoving() const;
|
2010-05-27 21:59:34 +08:00
|
|
|
float getThrowScale( const double eventTimeDelta ) const;
|
2010-05-26 00:27:21 +08:00
|
|
|
virtual void centerMousePointer( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
|
|
|
|
static void rotateYawPitch( osg::Quat& rotation, const double yaw, const double pitch,
|
|
|
|
const osg::Vec3d& localUp = osg::Vec3d( 0.,0.,0.) );
|
|
|
|
static void fixVerticalAxis( osg::Quat& rotation, const osg::Vec3d& localUp, bool disallowFlipOver );
|
|
|
|
void fixVerticalAxis( osg::Vec3d& eye, osg::Quat& rotation, bool disallowFlipOver );
|
|
|
|
static void fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp,
|
2010-05-27 21:59:34 +08:00
|
|
|
const osg::Vec3d& localUp, bool disallowFlipOver );
|
2010-05-26 00:27:21 +08:00
|
|
|
virtual bool setCenterByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
|
|
|
|
|
|
|
|
// mouse state
|
|
|
|
bool _thrown;
|
2010-06-14 23:20:47 +08:00
|
|
|
bool _allowThrow;
|
2010-05-26 00:27:21 +08:00
|
|
|
float _mouseCenterX, _mouseCenterY;
|
|
|
|
|
|
|
|
// internal event stack comprising last two mouse events.
|
|
|
|
osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t1;
|
|
|
|
osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t0;
|
|
|
|
|
|
|
|
/** The approximate amount of time it is currently taking to draw a frame.
|
|
|
|
* This is used to compute the delta in translation/rotation during a thrown display update.
|
|
|
|
* It allows us to match an delta in position/rotation independent of the rendering frame rate.
|
|
|
|
*/
|
|
|
|
double _delta_frame_time;
|
|
|
|
|
|
|
|
/** The time the last frame started.
|
|
|
|
* Used when _rate_sensitive is true so that we can match display update rate to rotation/translation rate.
|
|
|
|
*/
|
|
|
|
double _last_frame_time;
|
|
|
|
|
|
|
|
// scene data
|
|
|
|
osg::ref_ptr< osg::Node > _node;
|
|
|
|
double _modelSize;
|
|
|
|
bool _verticalAxisFixed;
|
|
|
|
|
|
|
|
// animation stuff
|
2011-01-12 01:20:25 +08:00
|
|
|
class OSGGA_EXPORT AnimationData : public osg::Referenced {
|
2010-05-26 00:27:21 +08:00
|
|
|
public:
|
|
|
|
double _animationTime;
|
|
|
|
bool _isAnimating;
|
|
|
|
double _startTime;
|
|
|
|
double _phase;
|
2016-06-24 18:58:38 +08:00
|
|
|
|
2010-05-26 00:27:21 +08:00
|
|
|
AnimationData();
|
|
|
|
void start( const double startTime );
|
|
|
|
};
|
|
|
|
osg::ref_ptr< AnimationData > _animationData;
|
|
|
|
virtual void allocAnimationData() { _animationData = new AnimationData(); }
|
|
|
|
|
|
|
|
// flags
|
|
|
|
int _flags;
|
|
|
|
|
|
|
|
// flags indicating that a value is relative to model size
|
|
|
|
int _relativeFlags;
|
|
|
|
inline bool getRelativeFlag( int index ) const;
|
|
|
|
inline void setRelativeFlag( int index, bool value );
|
|
|
|
static int numRelativeFlagsAllocated;
|
|
|
|
static int allocateRelativeFlag();
|
2010-05-25 20:05:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inline methods
|
|
|
|
//
|
|
|
|
|
|
|
|
inline bool StandardManipulator::getRelativeFlag( int index ) const
|
|
|
|
{
|
2010-05-26 01:02:11 +08:00
|
|
|
return ( _relativeFlags & (0x01 << index) ) != 0;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void StandardManipulator::setRelativeFlag( int index, bool value )
|
|
|
|
{
|
|
|
|
if( value ) _relativeFlags |= (0x01 << index);
|
2016-09-14 20:32:35 +08:00
|
|
|
else _relativeFlags &= ~(0x01 << index);
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether manipulator preserves camera's "UP" vector.
|
|
|
|
inline bool StandardManipulator::getVerticalAxisFixed() const
|
|
|
|
{
|
2010-05-26 01:02:11 +08:00
|
|
|
return _verticalAxisFixed;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
|
|
|
|
2010-06-14 23:20:47 +08:00
|
|
|
/// Returns true if the camera can be thrown, false otherwise. It defaults to true.
|
|
|
|
inline bool StandardManipulator::getAllowThrow() const
|
|
|
|
{
|
|
|
|
return _allowThrow;
|
|
|
|
}
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* OSGGA_CAMERA_MANIPULATOR */
|