接上篇,这是篇水数量的博客。
bool TrackballRotate::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) { viewer = dynamic_cast<osgViewer::Viewer*>(&aa); if (!viewer) return false; switch (ea.getEventType()) { case osgGA::GUIEventAdapter::PUSH: { int button = ea.getButton(); if (button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) { lbuttonDown = true; pick(ea.getX(), ea.getY()); if (PickObject) { old_world_point = screen2World(ea.getX(), ea.getY()); //old_world_point = { ea.getX(), ea.getY() ,0}; std::cout << "old_world_point.x(): " << old_world_point.x() << " old_world_point.y(): " << old_world_point.z() << std::endl; center = picked->getBound().center(); std::cout << "center: " << center.x() << " " << center.y() << " " << center.z() << std::endl; originPos = picked->getMatrix(); sphere_radius = picked->getBound().radius(); std::cout << "sphere_radius: " << sphere_radius << std::endl; std::cout << "radius" << sphere_radius << std::endl; } } else { lbuttonDown = false; } return false; } case osgGA::GUIEventAdapter::DRAG: { if (PickObject&&lbuttonDown) { osg::Vec3 new_world_point = screen2World(ea.getX(), ea.getY()); //osg::Vec3 new_world_point = { ea.getX(), ea.getY(), 0 }; std::cout << "new_world_point.x(): " << new_world_point.x() << " new_world_point.y(): " << new_world_point.z() << std::endl; osg::Quat rotateParameter = GetRotateParameter(new_world_point, old_world_point); std::cout << "rotateParameter: " << rotateParameter.x() <<" "<< rotateParameter.y()<<" " << rotateParameter.z() << std::endl; picked->setMatrix(originPos*osg::Matrix::translate(-center) *osg::Matrix::rotate(rotateParameter)*osg::Matrix::translate(center)); originPos = picked->getMatrix(); center=picked->getBound().center(); old_world_point = new_world_point; } return false; } case osgGA::GUIEventAdapter::RELEASE: { PickObject = false; lbuttonDown = false; return false; } default: return false; } }