2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2004-08-02 17:11:31 +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.
|
|
|
|
*/
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( disable : 4786 )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
#include <osgUtil/IntersectVisitor>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgGA;
|
|
|
|
|
2003-12-11 00:50:26 +08:00
|
|
|
#define DRIVER_HEIGHT 15
|
|
|
|
|
2005-02-23 03:19:50 +08:00
|
|
|
|
|
|
|
// #define ABOSULTE_PITCH 1
|
|
|
|
// #define INCREMENTAL_PITCH 1
|
|
|
|
#define KEYBOARD_PITCH 1
|
|
|
|
|
2003-12-11 00:50:26 +08:00
|
|
|
static float getHeightOfDriver()
|
|
|
|
{
|
|
|
|
float height = 1.5f;
|
|
|
|
if (getenv("OSG_DRIVE_MANIPULATOR_HEIGHT"))
|
|
|
|
{
|
|
|
|
height = atof(getenv("OSG_DRIVE_MANIPULATOR_HEIGHT"));
|
|
|
|
}
|
|
|
|
osg::notify(osg::INFO)<<"DriveManipulator::_height set to =="<<height<<std::endl;
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
DriveManipulator::DriveManipulator()
|
|
|
|
{
|
|
|
|
_modelScale = 0.01f;
|
|
|
|
_velocity = 0.0f;
|
2003-12-11 00:50:26 +08:00
|
|
|
_height = getHeightOfDriver();
|
|
|
|
_buffer = _height*1.3;
|
2005-02-23 03:19:50 +08:00
|
|
|
_pitch = 0.0;
|
2002-05-09 18:31:03 +08:00
|
|
|
//_speedMode = USE_MOUSE_Y_FOR_SPEED;
|
|
|
|
_speedMode = USE_MOUSE_BUTTONS_FOR_SPEED;
|
2005-02-23 03:19:50 +08:00
|
|
|
|
|
|
|
_pitchUpKeyPressed = false;
|
|
|
|
_pitchDownKeyPressed = false;
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DriveManipulator::~DriveManipulator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DriveManipulator::setNode(osg::Node* node)
|
|
|
|
{
|
|
|
|
_node = node;
|
|
|
|
if (_node.get())
|
|
|
|
{
|
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
|
|
|
_modelScale = boundingSphere._radius;
|
2003-04-05 15:11:07 +08:00
|
|
|
//_height = sqrtf(_modelScale)*0.03f;
|
|
|
|
//_buffer = sqrtf(_modelScale)*0.05f;
|
|
|
|
|
2003-12-11 00:50:26 +08:00
|
|
|
_height = getHeightOfDriver();
|
|
|
|
_buffer = _height*1.3;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
2004-07-13 03:54:54 +08:00
|
|
|
if (getAutoComputeHomePosition()) computeHomePosition();
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const osg::Node* DriveManipulator::getNode() const
|
|
|
|
{
|
|
|
|
return _node.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* DriveManipulator::getNode()
|
|
|
|
{
|
|
|
|
return _node.get();
|
|
|
|
}
|
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
void DriveManipulator::computeHomePosition()
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2003-05-19 23:15:17 +08:00
|
|
|
if(_node.get())
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ep = boundingSphere._center;
|
|
|
|
osg::Vec3d bp = ep;
|
2004-05-06 19:01:16 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::CoordinateFrame cf=getCoordinateFrame(ep);
|
2004-05-06 19:01:16 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
ep -= getUpVector(cf)* _modelScale*0.0001;
|
2004-05-06 19:01:16 +08:00
|
|
|
bp -= getUpVector(cf)* _modelScale;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
// check to see if any obstruction in front.
|
|
|
|
osgUtil::IntersectVisitor iv;
|
2005-03-23 05:06:06 +08:00
|
|
|
iv.setTraversalMask(_intersectTraversalMask);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
bool positionSet = false;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segDown->set(ep,bp);
|
|
|
|
iv.addLineSegment(segDown.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit terrain ok"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3d np = hitList.front().getWorldIntersectNormal();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d uv;
|
|
|
|
if (np * getUpVector(cf)>0.0) uv = np;
|
2002-05-09 18:31:03 +08:00
|
|
|
else uv = -np;
|
|
|
|
|
|
|
|
ep = ip;
|
2004-05-06 19:01:16 +08:00
|
|
|
ep += getUpVector(cf)*_height;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3 lv = uv^osg::Vec3d(1.0,0.0,0.0);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
setHomePosition(ep,ep+lv,uv);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
positionSet = true;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
if (!positionSet)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
bp = ep;
|
2004-05-06 19:01:16 +08:00
|
|
|
bp += getUpVector(cf)*_modelScale;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segUp = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segUp->set(ep,bp);
|
|
|
|
iv.addLineSegment(segUp.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segUp.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit terrain ok"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3d np = hitList.front().getWorldIntersectNormal();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d uv;
|
|
|
|
if (np*getUpVector(cf)>0.0) uv = np;
|
2002-05-09 18:31:03 +08:00
|
|
|
else uv = -np;
|
|
|
|
|
|
|
|
ep = ip;
|
2004-05-06 19:01:16 +08:00
|
|
|
ep += getUpVector(cf)*_height;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3 lv = uv^osg::Vec3d(1.0,0.0,0.0);
|
2004-07-13 03:54:54 +08:00
|
|
|
setHomePosition(ep,ep+lv,uv);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
positionSet = true;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
if (!positionSet)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2004-07-13 03:54:54 +08:00
|
|
|
setHomePosition(
|
2004-05-20 18:15:48 +08:00
|
|
|
boundingSphere._center+osg::Vec3d( 0.0,-2.0 * boundingSphere._radius,0.0),
|
2004-07-13 03:54:54 +08:00
|
|
|
boundingSphere._center+osg::Vec3d( 0.0,-2.0 * boundingSphere._radius,0.0)+osg::Vec3d(0.0,1.0,0.0),
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d(0.0,0.0,1.0));
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2004-07-13 03:54:54 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
void DriveManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter& us)
|
|
|
|
{
|
|
|
|
if (getAutoComputeHomePosition()) computeHomePosition();
|
|
|
|
|
|
|
|
computePosition(_homeEye, _homeCenter, _homeUp);
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
_velocity = 0.0;
|
2005-02-23 03:19:50 +08:00
|
|
|
|
|
|
|
_pitch = 0.0;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
us.requestRedraw();
|
|
|
|
|
2003-04-05 03:10:37 +08:00
|
|
|
us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2.0f,(ea.getYmin()+ea.getYmax())/2.0f);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
flushMouseEventStack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DriveManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us)
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
|
|
|
|
_velocity = 0.0f;
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ep = _eye;
|
2003-05-19 23:15:17 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::CoordinateFrame cf=getCoordinateFrame(ep);
|
2004-05-06 19:01:16 +08:00
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
Matrixd rotation_matrix;
|
2003-09-29 22:42:15 +08:00
|
|
|
rotation_matrix.set(_rotation);
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d sv = osg::Vec3d(1.0,0.0,0.0) * rotation_matrix;
|
|
|
|
osg::Vec3d bp = ep;
|
2004-05-06 19:01:16 +08:00
|
|
|
bp -= getUpVector(cf)*_modelScale;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
// check to see if any obstruction in front.
|
|
|
|
osgUtil::IntersectVisitor iv;
|
2005-03-23 05:06:06 +08:00
|
|
|
iv.setTraversalMask(_intersectTraversalMask);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
bool positionSet = false;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segDown->set(ep,bp);
|
|
|
|
iv.addLineSegment(segDown.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit terrain ok"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3d np = hitList.front().getWorldIntersectNormal();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d uv;
|
|
|
|
if (np*getUpVector(cf)>0.0) uv = np;
|
2002-05-09 18:31:03 +08:00
|
|
|
else uv = -np;
|
|
|
|
|
|
|
|
ep = ip+uv*_height;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d lv = uv^sv;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
computePosition(ep,ep+lv,uv);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
positionSet = true;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
if (!positionSet)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
bp = ep;
|
2004-05-06 19:01:16 +08:00
|
|
|
bp += getUpVector(cf)*_modelScale;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segUp = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segUp->set(ep,bp);
|
|
|
|
iv.addLineSegment(segUp.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segUp.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit terrain ok"<< std::endl;
|
|
|
|
osg::Vec3 ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3 np = hitList.front().getWorldIntersectNormal();
|
|
|
|
|
|
|
|
osg::Vec3 uv;
|
2004-05-06 19:01:16 +08:00
|
|
|
if (np*getUpVector(cf)>0.0f) uv = np;
|
2002-05-09 18:31:03 +08:00
|
|
|
else uv = -np;
|
|
|
|
|
|
|
|
ep = ip+uv*_height;
|
|
|
|
osg::Vec3 lv = uv^sv;
|
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
computePosition(ep,ep+lv,uv);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
positionSet = true;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-20 19:52:34 +08:00
|
|
|
if (ea.getEventType()!=GUIEventAdapter::RESIZE)
|
|
|
|
{
|
2003-04-05 03:10:37 +08:00
|
|
|
us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2.0f,(ea.getYmin()+ea.getYmax())/2.0f);
|
2003-01-20 19:52:34 +08:00
|
|
|
}
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DriveManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
|
|
|
|
{
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(GUIEventAdapter::PUSH):
|
|
|
|
{
|
|
|
|
|
|
|
|
addMouseEvent(ea);
|
|
|
|
us.requestContinuousUpdate(true);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::RELEASE):
|
|
|
|
{
|
|
|
|
|
|
|
|
addMouseEvent(ea);
|
|
|
|
us.requestContinuousUpdate(true);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::DRAG):
|
|
|
|
{
|
|
|
|
|
|
|
|
addMouseEvent(ea);
|
|
|
|
us.requestContinuousUpdate(true);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::MOVE):
|
|
|
|
{
|
|
|
|
|
|
|
|
addMouseEvent(ea);
|
|
|
|
us.requestContinuousUpdate(true);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-01-14 22:25:56 +08:00
|
|
|
case(GUIEventAdapter::KEYDOWN):
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
if (ea.getKey()==' ')
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
home(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()=='q')
|
|
|
|
{
|
|
|
|
_speedMode = USE_MOUSE_Y_FOR_SPEED;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()=='a')
|
|
|
|
{
|
|
|
|
_speedMode = USE_MOUSE_BUTTONS_FOR_SPEED;
|
|
|
|
return true;
|
|
|
|
}
|
2005-02-23 03:19:50 +08:00
|
|
|
#ifdef KEYBOARD_PITCH
|
|
|
|
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
|
|
|
|
{
|
|
|
|
_pitchUpKeyPressed = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
|
|
|
|
{
|
|
|
|
_pitchDownKeyPressed = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2002-05-09 18:31:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-23 03:19:50 +08:00
|
|
|
case(GUIEventAdapter::KEYUP):
|
|
|
|
{
|
|
|
|
#ifdef KEYBOARD_PITCH
|
|
|
|
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
|
|
|
|
{
|
|
|
|
_pitchUpKeyPressed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
|
|
|
|
{
|
|
|
|
_pitchDownKeyPressed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_pitchDownKeyPressed = false;
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
case(GUIEventAdapter::FRAME):
|
|
|
|
{
|
|
|
|
addMouseEvent(ea);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
2004-02-22 00:57:57 +08:00
|
|
|
return false;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::RESIZE):
|
|
|
|
{
|
|
|
|
init(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-19 18:43:02 +08:00
|
|
|
void DriveManipulator::getUsage(osg::ApplicationUsage& usage) const
|
|
|
|
{
|
|
|
|
usage.addKeyboardMouseBinding("Drive: Space","Reset the viewing position to home");
|
|
|
|
usage.addKeyboardMouseBinding("Drive: q","Use mouse y for controlling speed");
|
|
|
|
usage.addKeyboardMouseBinding("Drive: a","Use mouse middle,right mouse buttons for speed");
|
2005-02-23 03:19:50 +08:00
|
|
|
usage.addKeyboardMouseBinding("Drive: Down","Cursor down key to look downwards");
|
|
|
|
usage.addKeyboardMouseBinding("Drive: Up","Cursor up key to look upwards");
|
2003-02-19 18:43:02 +08:00
|
|
|
}
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
void DriveManipulator::flushMouseEventStack()
|
|
|
|
{
|
|
|
|
_ga_t1 = NULL;
|
|
|
|
_ga_t0 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DriveManipulator::addMouseEvent(const GUIEventAdapter& ea)
|
|
|
|
{
|
|
|
|
_ga_t1 = _ga_t0;
|
|
|
|
_ga_t0 = &ea;
|
|
|
|
}
|
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
void DriveManipulator::setByMatrix(const osg::Matrixd& matrix)
|
2002-08-30 04:41:19 +08:00
|
|
|
{
|
2003-05-19 23:15:17 +08:00
|
|
|
_eye = matrix.getTrans();
|
2003-09-06 04:48:42 +08:00
|
|
|
matrix.get(_rotation);
|
2002-08-30 04:41:19 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd DriveManipulator::getMatrix() const
|
2002-08-30 04:41:19 +08:00
|
|
|
{
|
2005-02-23 03:19:50 +08:00
|
|
|
return osg::Matrixd::rotate(_pitch,1.0,0.0,0.0)*osg::Matrixd::rotate(_rotation)*osg::Matrixd::translate(_eye);
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
2002-08-30 04:41:19 +08:00
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd DriveManipulator::getInverseMatrix() const
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
2005-02-23 03:19:50 +08:00
|
|
|
return osg::Matrixd::translate(-_eye)*osg::Matrixd::rotate(_rotation.inverse())*osg::Matrixd::rotate(-_pitch,1.0,0.0,0.0);
|
2002-08-30 04:41:19 +08:00
|
|
|
}
|
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
void DriveManipulator::computePosition(const osg::Vec3d& eye,const osg::Vec3d& center,const osg::Vec3d& up)
|
2002-08-30 04:41:19 +08:00
|
|
|
{
|
2004-07-13 03:54:54 +08:00
|
|
|
osg::Vec3d lv = center-eye;
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d f(lv);
|
2002-08-30 04:41:19 +08:00
|
|
|
f.normalize();
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d s(f^up);
|
2002-08-30 04:41:19 +08:00
|
|
|
s.normalize();
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d u(s^f);
|
2002-08-30 04:41:19 +08:00
|
|
|
u.normalize();
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Matrix rotation_matrix(s[0], u[0], -f[0], 0.0,
|
|
|
|
s[1], u[1], -f[1], 0.0,
|
|
|
|
s[2], u[2], -f[2], 0.0,
|
|
|
|
0.0, 0.0, 0.0, 1.0);
|
2002-08-30 04:41:19 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
_eye = eye;
|
2003-09-06 04:48:42 +08:00
|
|
|
rotation_matrix.get(_rotation);
|
2002-08-30 04:41:19 +08:00
|
|
|
_rotation = _rotation.inverse();
|
|
|
|
}
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
bool DriveManipulator::calcMovement()
|
|
|
|
{
|
|
|
|
// return if less then two events have been added.
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
2002-06-09 21:10:09 +08:00
|
|
|
double dt = _ga_t0->time()-_ga_t1->time();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
if (dt<0.0f)
|
|
|
|
{
|
|
|
|
notify(WARN) << "warning dt = "<<dt<< std::endl;
|
|
|
|
dt = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(_speedMode)
|
|
|
|
{
|
|
|
|
case(USE_MOUSE_Y_FOR_SPEED):
|
|
|
|
{
|
2004-05-20 18:15:48 +08:00
|
|
|
double dy = _ga_t0->getYnormalized();
|
2003-04-14 21:23:12 +08:00
|
|
|
_velocity = _modelScale*0.2f*dy;
|
2002-05-09 18:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(USE_MOUSE_BUTTONS_FOR_SPEED):
|
|
|
|
{
|
|
|
|
unsigned int buttonMask = _ga_t1->getButtonMask();
|
|
|
|
if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON)
|
|
|
|
{
|
|
|
|
// pan model.
|
|
|
|
|
2004-05-26 08:26:43 +08:00
|
|
|
_velocity += dt*_modelScale*0.01;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
|
|
|
|
buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON))
|
|
|
|
{
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
_velocity = 0.0;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
|
|
|
|
{
|
|
|
|
|
2004-05-26 08:26:43 +08:00
|
|
|
_velocity -= dt*_modelScale*0.01;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::CoordinateFrame cf=getCoordinateFrame(_eye);
|
2004-05-06 19:01:16 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
osg::Matrix rotation_matrix;
|
|
|
|
rotation_matrix.makeRotate(_rotation);
|
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d up = osg::Vec3d(0.0,1.0,0.0) * rotation_matrix;
|
|
|
|
osg::Vec3d lv = osg::Vec3d(0.0,0.0,-1.0) * rotation_matrix;
|
2005-02-23 03:19:50 +08:00
|
|
|
osg::Vec3d sv = osg::Vec3d(1.0,0.0,0.0) * rotation_matrix;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
// rotate the camera.
|
2004-05-20 18:15:48 +08:00
|
|
|
double dx = _ga_t0->getXnormalized();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
double yaw = -inDegrees(dx*50.0f*dt);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2005-02-23 03:19:50 +08:00
|
|
|
|
|
|
|
#ifdef KEYBOARD_PITCH
|
|
|
|
double pitch_delta = 0.5;
|
|
|
|
if (_pitchUpKeyPressed) _pitch += pitch_delta*dt;
|
|
|
|
if (_pitchDownKeyPressed) _pitch -= pitch_delta*dt;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ABOSULTE_PITCH)
|
|
|
|
// abosolute pitch
|
|
|
|
double dy = _ga_t0->getYnormalized();
|
|
|
|
_pitch = -dy*0.5;
|
|
|
|
#elif defined(INCREMENTAL_PITCH)
|
|
|
|
// incremental pitch
|
|
|
|
double dy = _ga_t0->getYnormalized();
|
|
|
|
_pitch += dy*dt;
|
|
|
|
#endif
|
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
osg::Quat yaw_rotation;
|
|
|
|
yaw_rotation.makeRotate(yaw,up);
|
2005-02-23 03:19:50 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
_rotation *= yaw_rotation;
|
2005-02-23 03:19:50 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
rotation_matrix.makeRotate(_rotation);
|
2005-02-23 03:19:50 +08:00
|
|
|
|
|
|
|
sv = osg::Vec3d(1.0,0.0,0.0) * rotation_matrix;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
// movement is big enough the move the eye point along the look vector.
|
2004-05-20 18:15:48 +08:00
|
|
|
if (fabs(_velocity*dt)>1e-8)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2004-05-20 18:15:48 +08:00
|
|
|
double distanceToMove = _velocity*dt;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
double signedBuffer;
|
|
|
|
if (distanceToMove>=0.0) signedBuffer=_buffer;
|
2002-05-09 18:31:03 +08:00
|
|
|
else signedBuffer=-_buffer;
|
|
|
|
|
|
|
|
// check to see if any obstruction in front.
|
|
|
|
osgUtil::IntersectVisitor iv;
|
2005-03-23 05:06:06 +08:00
|
|
|
iv.setTraversalMask(_intersectTraversalMask);
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segForward = new osg::LineSegment;
|
2002-08-30 04:41:19 +08:00
|
|
|
segForward->set(_eye,_eye+lv*(signedBuffer+distanceToMove));
|
2002-05-09 18:31:03 +08:00
|
|
|
iv.addLineSegment(segForward.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segForward.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit obstruction"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
2002-08-30 04:41:19 +08:00
|
|
|
distanceToMove = (ip-_eye).length()-_buffer;
|
2004-05-20 18:15:48 +08:00
|
|
|
_velocity = 0.0;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if forward point is correct height above terrain.
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d fp = _eye+lv*distanceToMove;
|
|
|
|
osg::Vec3d lfp = fp-up*_height*5;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
iv.reset();
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segNormal = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segNormal->set(fp,lfp);
|
|
|
|
iv.addLineSegment(segNormal.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segNormal.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
// notify(INFO) << "Hit terrain ok"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3d np = hitList.front().getWorldIntersectNormal();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
if (up*np>0.0) up = np;
|
2002-08-30 04:41:19 +08:00
|
|
|
else up = -np;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
_eye = ip+up*_height;
|
2003-12-11 00:50:26 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
lv = up^sv;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
computePosition(_eye,_eye+lv,up);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2003-12-11 00:50:26 +08:00
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
// no hit on the terrain found therefore resort to a fall under
|
|
|
|
// under the influence of gravity.
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d dp = lfp;
|
2004-05-06 19:01:16 +08:00
|
|
|
dp -= getUpVector(cf)* (2*_modelScale);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
iv.reset();
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
osg::ref_ptr<osg::LineSegment> segFall = new osg::LineSegment;
|
2002-05-09 18:31:03 +08:00
|
|
|
segFall->set(lfp,dp);
|
|
|
|
iv.addLineSegment(segFall.get());
|
|
|
|
|
|
|
|
_node->accept(iv);
|
|
|
|
|
|
|
|
if (iv.hits())
|
|
|
|
{
|
|
|
|
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segFall.get());
|
|
|
|
if (!hitList.empty())
|
|
|
|
{
|
|
|
|
|
|
|
|
notify(INFO) << "Hit terrain on decent ok"<< std::endl;
|
2004-05-20 18:15:48 +08:00
|
|
|
osg::Vec3d ip = hitList.front().getWorldIntersectPoint();
|
|
|
|
osg::Vec3d np = hitList.front().getWorldIntersectNormal();
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-05-20 18:15:48 +08:00
|
|
|
if (up*np>0.0) up = np;
|
2002-08-30 04:41:19 +08:00
|
|
|
else up = -np;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
_eye = ip+up*_height;
|
2003-12-11 00:50:26 +08:00
|
|
|
|
2002-08-30 04:41:19 +08:00
|
|
|
lv = up^sv;
|
2002-05-09 18:31:03 +08:00
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
computePosition(_eye,_eye+lv,up);
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no collision with terrain has been found therefore track horizontally.
|
|
|
|
|
|
|
|
lv *= (_velocity*dt);
|
2002-08-30 04:41:19 +08:00
|
|
|
|
|
|
|
_eye += lv;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|