2001-09-20 05:08:56 +08:00
|
|
|
#include <osgUtil/TrackballManipulator>
|
|
|
|
#include <osg/Types>
|
|
|
|
#include <osg/Notify>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgUtil;
|
|
|
|
|
|
|
|
TrackballManipulator::TrackballManipulator()
|
|
|
|
{
|
|
|
|
_modelScale = 0.01f;
|
|
|
|
_minimumZoomScale = 0.05f;
|
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
TrackballManipulator::~TrackballManipulator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void TrackballManipulator::setNode(osg::Node* node)
|
|
|
|
{
|
|
|
|
_node = node;
|
|
|
|
if (_node.get())
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
2001-01-11 00:32:10 +08:00
|
|
|
_modelScale = boundingSphere._radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
const osg::Node* TrackballManipulator::getNode() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
return _node.get();
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
/*ea*/
|
|
|
|
void TrackballManipulator::home(const GUIEventAdapter& ,GUIActionAdapter& us)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if(_node.get() && _camera.get())
|
|
|
|
{
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
|
|
|
|
|
|
|
_camera->setView(boundingSphere._center+osg::Vec3( 0.0,-2.0f * boundingSphere._radius,0.0f),
|
|
|
|
boundingSphere._center,
|
|
|
|
osg::Vec3(0.0f,0.0f,1.0f));
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
us.requestRedraw();
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
void TrackballManipulator::init(const GUIEventAdapter& ,GUIActionAdapter& )
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
bool TrackballManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if(!_camera.get()) return false;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
case(GUIEventAdapter::PUSH):
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
2001-09-20 05:08:56 +08:00
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
2001-01-11 00:32:10 +08:00
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
return true;
|
2001-09-20 05:08:56 +08:00
|
|
|
case(GUIEventAdapter::RELEASE):
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (ea.getButtonMask()==0)
|
|
|
|
{
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
if (isMouseMoving())
|
|
|
|
{
|
|
|
|
if (calcMovement())
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(true);
|
2001-01-11 00:32:10 +08:00
|
|
|
_thrown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
2001-09-20 05:08:56 +08:00
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
2001-01-11 00:32:10 +08:00
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
2001-09-20 05:08:56 +08:00
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
2001-01-11 00:32:10 +08:00
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2001-09-20 05:08:56 +08:00
|
|
|
case(GUIEventAdapter::DRAG):
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
addMouseEvent(ea);
|
2001-09-20 05:08:56 +08:00
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
2001-01-11 00:32:10 +08:00
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
return true;
|
2001-09-20 05:08:56 +08:00
|
|
|
case(GUIEventAdapter::MOVE):
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return false;
|
2001-09-20 05:08:56 +08:00
|
|
|
case(GUIEventAdapter::KEYBOARD):
|
|
|
|
if (ea.getKey()==' ')
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
_thrown = false;
|
|
|
|
home(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case(GUIEventAdapter::FRAME):
|
|
|
|
if (_thrown)
|
|
|
|
{
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return false;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
bool TrackballManipulator::isMouseMoving()
|
|
|
|
{
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
|
|
|
static const float velocity = 100.0f;
|
|
|
|
|
|
|
|
float dx = _ga_t0->getX()-_ga_t1->getX();
|
|
|
|
float dy = _ga_t0->getY()-_ga_t1->getY();
|
2001-09-20 05:08:56 +08:00
|
|
|
float len = sqrtf(dx*dx+dy*dy);
|
2001-01-11 00:32:10 +08:00
|
|
|
float dt = _ga_t0->time()-_ga_t1->time();
|
|
|
|
|
|
|
|
return (len>dt*velocity);
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void TrackballManipulator::flushMouseEventStack()
|
|
|
|
{
|
|
|
|
_ga_t1 = NULL;
|
|
|
|
_ga_t0 = NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
void TrackballManipulator::addMouseEvent(const GUIEventAdapter& ea)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_ga_t1 = _ga_t0;
|
|
|
|
_ga_t0 = &ea;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
bool TrackballManipulator::calcMovement()
|
|
|
|
{
|
2001-12-21 21:07:35 +08:00
|
|
|
_camera->setFusionDistanceFunction(osg::Camera::PROPORTIONAL_TO_LOOK_DISTANCE,1.0f);
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
// return if less then two events have been added.
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
2001-10-14 13:14:37 +08:00
|
|
|
float dx = _ga_t0->getX()-_ga_t1->getX();
|
2001-01-11 00:32:10 +08:00
|
|
|
float dy = _ga_t0->getY()-_ga_t1->getY();
|
|
|
|
|
2001-12-21 21:07:35 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
// return if there is no movement.
|
|
|
|
if (dx==0 && dy==0) return false;
|
|
|
|
|
2001-12-21 00:35:38 +08:00
|
|
|
float focalLength = (_camera->getCenterPoint()-_camera->getEyePoint()).length();
|
2001-01-11 00:32:10 +08:00
|
|
|
unsigned int buttonMask = _ga_t1->getButtonMask();
|
|
|
|
if (buttonMask==GUIEventAdapter::LEFT_BUTTON)
|
|
|
|
{
|
|
|
|
|
|
|
|
// rotate camera.
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
osg::Vec3 center = _camera->getCenterPoint();
|
2001-01-11 00:32:10 +08:00
|
|
|
osg::Vec3 axis;
|
|
|
|
float angle;
|
|
|
|
|
|
|
|
float mx0 = (_ga_t0->getXmin()+_ga_t0->getXmax())/2.0f;
|
|
|
|
float rx0 = (_ga_t0->getXmax()-_ga_t0->getXmin())/2.0f;
|
|
|
|
|
|
|
|
float my0 = (_ga_t0->getYmin()+_ga_t0->getYmax())/2.0f;
|
|
|
|
float ry0 = (_ga_t0->getYmax()-_ga_t0->getYmin())/2.0f;
|
|
|
|
|
|
|
|
float mx1 = (_ga_t0->getXmin()+_ga_t1->getXmax())/2.0f;
|
|
|
|
float rx1 = (_ga_t0->getXmax()-_ga_t1->getXmin())/2.0f;
|
|
|
|
|
|
|
|
float my1 = (_ga_t1->getYmin()+_ga_t1->getYmax())/2.0f;
|
|
|
|
float ry1 = (_ga_t1->getYmax()-_ga_t1->getYmin())/2.0f;
|
|
|
|
|
|
|
|
float px0 = (_ga_t0->getX()-mx0)/rx0;
|
|
|
|
float py0 = (my0-_ga_t0->getY())/ry0;
|
|
|
|
|
|
|
|
float px1 = (_ga_t1->getX()-mx1)/rx1;
|
|
|
|
float py1 = (my1-_ga_t1->getY())/ry1;
|
|
|
|
|
|
|
|
trackball(axis,angle,px1,py1,px0,py0);
|
|
|
|
|
|
|
|
osg::Matrix mat;
|
2001-12-13 04:29:10 +08:00
|
|
|
mat.makeTranslate(-center.x(),-center.y(),-center.z());
|
2001-09-27 17:44:55 +08:00
|
|
|
mat *= Matrix::rotate(angle,axis.x(),axis.y(),axis.z());
|
2001-12-13 04:29:10 +08:00
|
|
|
mat *= Matrix::translate(center.x(),center.y(),center.z());
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_camera->transformLookAt(mat);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
else if (buttonMask==GUIEventAdapter::MIDDLE_BUTTON ||
|
|
|
|
buttonMask==(GUIEventAdapter::LEFT_BUTTON|GUIEventAdapter::RIGHT_BUTTON))
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// pan model.
|
2001-12-21 00:35:38 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-21 00:35:38 +08:00
|
|
|
float scale = 0.0015f*focalLength;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
osg::Vec3 uv = _camera->getUpVector();
|
|
|
|
osg::Vec3 sv = _camera->getSideVector();
|
|
|
|
osg::Vec3 dv = uv*(dy*scale)-sv*(dx*scale);
|
|
|
|
|
|
|
|
osg::Matrix mat;
|
2001-12-13 04:29:10 +08:00
|
|
|
mat.makeTranslate(dv.x(),dv.y(),dv.z());
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_camera->transformLookAt(mat);
|
2001-12-21 00:35:38 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::RIGHT_BUTTON)
|
|
|
|
{
|
|
|
|
|
|
|
|
// zoom model.
|
|
|
|
|
2001-12-21 00:35:38 +08:00
|
|
|
float fd = focalLength;
|
2001-01-11 00:32:10 +08:00
|
|
|
float scale = 1.0f-dy*0.001f;
|
|
|
|
if (fd*scale>_modelScale*_minimumZoomScale)
|
|
|
|
{
|
|
|
|
// zoom camera in.
|
2001-09-20 05:08:56 +08:00
|
|
|
osg::Vec3 center = _camera->getCenterPoint();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
osg::Matrix mat;
|
2001-12-13 04:29:10 +08:00
|
|
|
mat.makeTranslate(-center.x(),-center.y(),-center.z());
|
2001-09-27 17:44:55 +08:00
|
|
|
mat *= Matrix::scale(scale,scale,scale);
|
2001-12-13 04:29:10 +08:00
|
|
|
mat *= Matrix::translate(center.x(),center.y(),center.z());
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_camera->transformLookAt(mat);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
else
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// notify(DEBUG_INFO) << "Pushing forward"<<endl;
|
2001-01-11 00:32:10 +08:00
|
|
|
// push the camera forward.
|
|
|
|
float scale = 0.0015f*fd;
|
|
|
|
osg::Vec3 dv = _camera->getLookVector()*(dy*scale);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
osg::Matrix mat;
|
2001-12-13 04:29:10 +08:00
|
|
|
mat.makeTranslate(dv.x(),dv.y(),dv.z());
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_camera->transformLookAt(mat);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/*
|
|
|
|
* This size should really be based on the distance from the center of
|
|
|
|
* rotation to the point on the object underneath the mouse. That
|
|
|
|
* point would then track the mouse as closely as possible. This is a
|
|
|
|
* simple example, though, so that is left as an Exercise for the
|
|
|
|
* Programmer.
|
|
|
|
*/
|
|
|
|
const float TRACKBALLSIZE = 0.8f;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, simulate a track-ball. Project the points onto the virtual
|
|
|
|
* trackball, then figure out the axis of rotation, which is the cross
|
|
|
|
* product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
|
|
|
|
* Note: This is a deformed trackball-- is a trackball in the center,
|
|
|
|
* but is deformed into a hyperbolic sheet of rotation away from the
|
|
|
|
* center. This particular function was chosen after trying out
|
|
|
|
* several variations.
|
|
|
|
*
|
|
|
|
* It is assumed that the arguments to this routine are in the range
|
|
|
|
* (-1.0 ... 1.0)
|
|
|
|
*/
|
|
|
|
void TrackballManipulator::trackball(osg::Vec3& axis,float& angle, float p1x, float p1y, float p2x, float p2y)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* First, figure out z-coordinates for projection of P1 and P2 to
|
|
|
|
* deformed sphere
|
|
|
|
*/
|
|
|
|
|
|
|
|
osg::Vec3 uv = _camera->getUpVector();
|
|
|
|
osg::Vec3 sv = _camera->getSideVector();
|
|
|
|
osg::Vec3 lv = _camera->getLookVector();
|
|
|
|
|
|
|
|
osg::Vec3 p1 = sv*p1x+uv*p1y-lv*tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y);
|
|
|
|
osg::Vec3 p2 = sv*p2x+uv*p2y-lv*tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, we want the cross product of P1 and P2
|
|
|
|
*/
|
2001-10-14 13:14:37 +08:00
|
|
|
|
|
|
|
// Robert,
|
|
|
|
//
|
|
|
|
// This was the quick 'n' dirty fix to get the trackball doing the right
|
|
|
|
// thing after fixing the Quat rotations to be right-handed. You may want
|
|
|
|
// to do something more elegant.
|
|
|
|
// axis = p1^p2;
|
|
|
|
axis = p2^p1;
|
2001-01-11 00:32:10 +08:00
|
|
|
axis.normalize();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out how much to rotate around that axis.
|
|
|
|
*/
|
|
|
|
float t = (p2-p1).length() / (2.0*TRACKBALLSIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid problems with out-of-control values...
|
|
|
|
*/
|
|
|
|
if (t > 1.0) t = 1.0;
|
|
|
|
if (t < -1.0) t = -1.0;
|
2001-10-02 23:59:49 +08:00
|
|
|
angle = inRadians(asin(t));
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
/*
|
|
|
|
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
|
|
|
|
* if we are away from the center of the sphere.
|
|
|
|
*/
|
|
|
|
float TrackballManipulator::tb_project_to_sphere(float r, float x, float y)
|
|
|
|
{
|
|
|
|
float d, t, z;
|
|
|
|
|
|
|
|
d = sqrt(x*x + y*y);
|
2001-09-20 05:08:56 +08:00
|
|
|
/* Inside sphere */
|
|
|
|
if (d < r * 0.70710678118654752440)
|
|
|
|
{
|
2001-01-11 00:32:10 +08:00
|
|
|
z = sqrt(r*r - d*d);
|
2001-09-20 05:08:56 +08:00
|
|
|
} /* On hyperbola */
|
|
|
|
else
|
|
|
|
{
|
2001-01-11 00:32:10 +08:00
|
|
|
t = r / 1.41421356237309504880;
|
|
|
|
z = t*t / d;
|
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|