2010-05-25 20:05:13 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-10-24 18:53:01 +08:00
|
|
|
*
|
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
|
2005-10-24 18:53:01 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2010-05-25 20:05:13 +08:00
|
|
|
*
|
2005-10-24 18:53:01 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-05-25 20:05:13 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-10-24 18:53:01 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgGA;
|
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
/// Constructor.
|
|
|
|
FlightManipulator::FlightManipulator( int flags )
|
2010-05-26 01:02:22 +08:00
|
|
|
: inherited( flags ),
|
|
|
|
_yawMode( YAW_AUTOMATICALLY_WHEN_BANKED )
|
2010-05-25 20:05:13 +08:00
|
|
|
{
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
/// Constructor.
|
|
|
|
FlightManipulator::FlightManipulator( const FlightManipulator& fm, const CopyOp& copyOp )
|
2016-06-08 17:18:23 +08:00
|
|
|
: osg::Object(fm, copyOp),
|
|
|
|
osg::Callback(fm, copyOp),
|
2013-06-28 20:00:43 +08:00
|
|
|
inherited( fm, copyOp ),
|
2010-05-26 01:02:22 +08:00
|
|
|
_yawMode( fm._yawMode )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
void FlightManipulator::init( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
inherited::init( ea, us );
|
2004-07-13 03:54:54 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
// center mouse pointer
|
|
|
|
centerMousePointer( ea, us );
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
void FlightManipulator::home( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
inherited::home( ea, us );
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
// center mouse pointer
|
|
|
|
centerMousePointer( ea, us );
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
// doc in parent
|
|
|
|
bool FlightManipulator::handleFrame( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
addMouseEvent( ea );
|
|
|
|
if( performMovement() )
|
|
|
|
us.requestRedraw();
|
2010-05-25 20:05:13 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
return false;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
// doc in parent
|
|
|
|
bool FlightManipulator::handleMouseMove( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
return flightHandleEvent( ea, us );
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
// doc in parent
|
|
|
|
bool FlightManipulator::handleMouseDrag( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
return flightHandleEvent( ea, us );
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
// doc in parent
|
|
|
|
bool FlightManipulator::handleMousePush( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
return flightHandleEvent( ea, us );
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
// doc in parent
|
|
|
|
bool FlightManipulator::handleMouseRelease( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
return flightHandleEvent( ea, us );
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
bool FlightManipulator::handleKeyDown( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
if( inherited::handleKeyDown( ea, us ) )
|
|
|
|
return true;
|
2007-02-26 21:01:17 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
if( ea.getKey() == 'q' )
|
|
|
|
{
|
2007-02-26 21:01:17 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
_yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
|
|
|
|
return true;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
}
|
|
|
|
else if (ea.getKey()=='a')
|
|
|
|
{
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
_yawMode = NO_AUTOMATIC_YAW;
|
|
|
|
return true;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
return false;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-14 23:20:47 +08:00
|
|
|
/// General flight-style event handler
|
2010-05-25 20:05:13 +08:00
|
|
|
bool FlightManipulator::flightHandleEvent( const GUIEventAdapter& ea, GUIActionAdapter& us )
|
2002-08-30 04:41:19 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
addMouseEvent( ea );
|
|
|
|
us.requestContinuousUpdate( true );
|
|
|
|
if( performMovement() )
|
|
|
|
us.requestRedraw();
|
2003-05-19 23:15:17 +08:00
|
|
|
|
2010-05-26 01:02:22 +08:00
|
|
|
return true;
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
void FlightManipulator::getUsage( ApplicationUsage& usage ) const
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
inherited::getUsage( usage );
|
|
|
|
usage.addKeyboardMouseBinding( getManipulatorName() + ": q", "Automatically yaw when banked (default)" );
|
|
|
|
usage.addKeyboardMouseBinding( getManipulatorName() + ": a", "No yaw when banked" );
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
/** Configure the Yaw control for the flight model. */
|
|
|
|
void FlightManipulator::setYawControlMode( YawControlMode ycm )
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
_yawMode = ycm;
|
2002-08-30 04:41:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
bool FlightManipulator::performMovement()
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
// return if less then two events have been added.
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
|
|
|
|
2010-05-27 21:59:34 +08:00
|
|
|
double eventTimeDelta = _ga_t0->getTime()-_ga_t1->getTime();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-27 21:59:34 +08:00
|
|
|
if (eventTimeDelta<0.0f)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2010-05-28 23:53:32 +08:00
|
|
|
OSG_WARN << "Manipulator warning: eventTimeDelta = " << eventTimeDelta << std::endl;
|
2010-05-27 21:59:34 +08:00
|
|
|
eventTimeDelta = 0.0f;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int buttonMask = _ga_t1->getButtonMask();
|
|
|
|
if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON)
|
|
|
|
{
|
2010-05-27 21:59:34 +08:00
|
|
|
performMovementLeftMouseButton(eventTimeDelta, 0., 0.);
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
|
|
|
|
buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON))
|
|
|
|
{
|
2010-05-27 21:59:34 +08:00
|
|
|
performMovementMiddleMouseButton(eventTimeDelta, 0., 0.);
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
|
|
|
|
{
|
2010-05-27 21:59:34 +08:00
|
|
|
performMovementRightMouseButton(eventTimeDelta, 0., 0.);
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
2003-04-05 03:10:37 +08:00
|
|
|
float dx = _ga_t0->getXnormalized();
|
|
|
|
float dy = _ga_t0->getYnormalized();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
CoordinateFrame cf=getCoordinateFrame(_eye);
|
2003-04-14 21:23:12 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
Matrixd rotation_matrix;
|
2002-08-30 04:41:19 +08:00
|
|
|
rotation_matrix.makeRotate(_rotation);
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
Vec3d up = Vec3d(0.0,1.0,0.0) * rotation_matrix;
|
|
|
|
Vec3d lv = Vec3d(0.0,0.0,-1.0) * rotation_matrix;
|
|
|
|
|
|
|
|
Vec3d sv = lv^up;
|
2002-08-30 04:41:19 +08:00
|
|
|
sv.normalize();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-27 21:59:34 +08:00
|
|
|
double pitch = -inDegrees(dy*50.0f*eventTimeDelta);
|
|
|
|
double roll = inDegrees(dx*50.0f*eventTimeDelta);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
Quat delta_rotate;
|
2002-08-30 04:41:19 +08:00
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
Quat roll_rotate;
|
|
|
|
Quat pitch_rotate;
|
2002-08-30 04:41:19 +08:00
|
|
|
|
|
|
|
pitch_rotate.makeRotate(pitch,sv.x(),sv.y(),sv.z());
|
|
|
|
roll_rotate.makeRotate(roll,lv.x(),lv.y(),lv.z());
|
|
|
|
|
|
|
|
delta_rotate = pitch_rotate*roll_rotate;
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
if (_yawMode==YAW_AUTOMATICALLY_WHEN_BANKED)
|
|
|
|
{
|
2004-04-30 06:18:54 +08:00
|
|
|
//float bank = asinf(sv.z());
|
2004-05-20 18:15:48 +08:00
|
|
|
double bank = asinf(sv *getUpVector(cf));
|
2010-05-27 21:59:34 +08:00
|
|
|
double yaw = inRadians(bank)*eventTimeDelta;
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
Quat yaw_rotate;
|
2004-04-30 06:18:54 +08:00
|
|
|
//yaw_rotate.makeRotate(yaw,0.0f,0.0f,1.0f);
|
|
|
|
|
2004-05-06 19:01:16 +08:00
|
|
|
yaw_rotate.makeRotate(yaw,getUpVector(cf));
|
2004-04-30 06:18:54 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
|
|
|
|
delta_rotate = delta_rotate*yaw_rotate;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
2010-05-27 21:59:34 +08:00
|
|
|
lv *= (_velocity*eventTimeDelta);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
_eye += lv;
|
|
|
|
_rotation = _rotation*delta_rotate;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-25 20:05:13 +08:00
|
|
|
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
bool FlightManipulator::performMovementLeftMouseButton( const double eventTimeDelta, const double /*dx*/, const double /*dy*/ )
|
2010-05-25 20:05:13 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
// pan model
|
2010-05-27 21:59:34 +08:00
|
|
|
_velocity += eventTimeDelta * (_acceleration + _velocity);
|
2010-05-26 01:02:22 +08:00
|
|
|
return true;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
bool FlightManipulator::performMovementMiddleMouseButton( const double /*eventTimeDelta*/, const double /*dx*/, const double /*dy*/ )
|
2010-05-25 20:05:13 +08:00
|
|
|
{
|
2010-05-26 01:02:22 +08:00
|
|
|
_velocity = 0.0f;
|
|
|
|
return true;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
bool FlightManipulator::performMovementRightMouseButton( const double eventTimeDelta, const double /*dx*/, const double /*dy*/ )
|
2010-05-25 20:05:13 +08:00
|
|
|
{
|
2010-05-27 21:59:34 +08:00
|
|
|
_velocity -= eventTimeDelta * (_acceleration + _velocity);
|
2010-05-26 01:02:22 +08:00
|
|
|
return true;
|
2010-05-25 20:05:13 +08:00
|
|
|
}
|