#include "GliderManipulator.h" #include using namespace osg; using namespace osgGA; GliderManipulator::GliderManipulator() { _modelScale = 0.01f; _velocity = 0.0f; _yawMode = YAW_AUTOMATICALLY_WHEN_BANKED; } GliderManipulator::~GliderManipulator() { } void GliderManipulator::setNode(osg::Node* node) { _node = node; if (_node.get()) { const osg::BoundingSphere& boundingSphere=_node->getBound(); _modelScale = boundingSphere._radius; } } const osg::Node* GliderManipulator::getNode() const { return _node.get(); } void GliderManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter& us) { if(_node.get() && _camera.get()) { const osg::BoundingSphere& boundingSphere=_node->getBound(); osg::Vec3 eye = boundingSphere._center+osg::Vec3(-boundingSphere._radius*0.25f,-boundingSphere._radius*0.25f,-boundingSphere._radius*0.03f); _camera->setView(eye, eye+osg::Vec3(1.0f,1.0f,-0.1f), osg::Vec3(0.0f,0.0f,1.0f)); _velocity = boundingSphere._radius*0.01f; us.requestRedraw(); us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2,(ea.getYmin()+ea.getYmax())/2); flushMouseEventStack(); } } void GliderManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us) { flushMouseEventStack(); us.requestContinuousUpdate(false); const osg::BoundingSphere& boundingSphere=_node->getBound(); _velocity = boundingSphere._radius*0.01f; us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2,(ea.getYmin()+ea.getYmax())/2); } bool GliderManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us) { if(!_camera.get()) return false; 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; } case(GUIEventAdapter::KEYDOWN): if (ea.getKey()==' ') { flushMouseEventStack(); home(ea,us); us.requestRedraw(); us.requestContinuousUpdate(false); return true; } else if (ea.getKey()=='+') { _camera->setFusionDistanceRatio(_camera->getFusionDistanceRatio()*1.25f); return true; } else if (ea.getKey()=='-') { _camera->setFusionDistanceRatio(_camera->getFusionDistanceRatio()/1.25f); return true; } return false; case(GUIEventAdapter::FRAME): addMouseEvent(ea); if (calcMovement()) us.requestRedraw(); return true; case(GUIEventAdapter::RESIZE): init(ea,us); us.requestRedraw(); return true; default: return false; } } void GliderManipulator::flushMouseEventStack() { _ga_t1 = NULL; _ga_t0 = NULL; } void GliderManipulator::addMouseEvent(const GUIEventAdapter& ea) { _ga_t1 = _ga_t0; _ga_t0 = &ea; } bool GliderManipulator::calcMovement() { _camera->setFusionDistanceMode(osg::Camera::PROPORTIONAL_TO_SCREEN_DISTANCE); // return if less then two events have been added. if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false; float dt = _ga_t0->time()-_ga_t1->time(); if (dt<0.0f) { notify(WARN) << "warning dt = "<getButtonMask(); if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON) { // pan model. _velocity += dt*_modelScale*0.05f; } else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON || buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON)) { _velocity = 0.0f; } else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON) { _velocity -= dt*_modelScale*0.05f; } float mx = (_ga_t0->getXmin()+_ga_t0->getXmax())/2.0f; float my = (_ga_t0->getYmin()+_ga_t0->getYmax())/2.0f; float dx = _ga_t0->getX()-mx; float dy = _ga_t0->getY()-my; osg::Vec3 center = _camera->getEyePoint(); osg::Vec3 sv = _camera->getSideVector(); osg::Vec3 lv = _camera->getLookVector(); float pitch = inDegrees(dy*0.15f*dt); float roll = inDegrees(dx*0.1f*dt); osg::Matrix mat; mat.makeTranslate(-center); mat *= Matrix::rotate(pitch,sv.x(),sv.y(),sv.z()); mat *= Matrix::rotate(roll,lv.x(),lv.y(),lv.z()); if (_yawMode==YAW_AUTOMATICALLY_WHEN_BANKED) { float bank = asinf(sv.z()); float yaw = inRadians(bank)*dt; mat *= Matrix::rotate(yaw,0.0f,0.0f,1.0f); } lv *= (_velocity*dt); mat *= Matrix::translate(center+lv); _camera->transformLookAt(mat); return true; }