|
如题,最近在改下EarthManipulator,遇到了这个问题,很诡异啊,osg版本3.0.1,osgEarth2.4
在键盘按下后会就会有KEYUP,而且key为0,求大神指导。
头文件
class MyEarthManipulator : public EarthManipulator
{
public:
MyEarthManipulator(EarthManipulator& earthMip,double step = 1.0,double rotateStep = 1.0);
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);
void changePosition(osg::View* view);
private:
double m_step,m_rotateStep;
double m_Up,m_Down,m_Left,m_Right;
double pre_Up,pre_Down,pre_Left,pre_Right;
double pre_move,now_move,pre_rotate,now_rotate;
};
源文件
MyEarthManipulator::MyEarthManipulator(EarthManipulator& earthMip,double step,double rotateStep)
: m_step(step),
m_rotateStep(rotateStep),
m_Up(0),m_Down(0),m_Left(0),m_Right(0),
pre_Up(0),pre_Down(0),pre_Left(0),pre_Right(0),
pre_move(0),now_move(0),pre_rotate(0),now_rotate(0)
{
this->setByMatrix(earthMip.getMatrix());
/*osg::Vec3d eye,center,up;
earthMip.getHomePosition(eye,center,up);
this->setHomePosition(eye,center,up);*/
this->setAutoComputeHomePosition(false);
}
void MyEarthManipulator::changePosition(osg::View* view)
{
/*if (m_bLeft != m_bRight)
{*/
double rotateValue,moveValue;
rotateValue = now_rotate - pre_rotate;
moveValue = now_move - pre_move;
pre_rotate = now_rotate;
pre_move = now_move;
osg::Vec3d eye,center,up;
view->getCamera()->getViewMatrixAsLookAt(eye,center,up);
osg::Vec3d nUp = up;
osg::Vec3d nEye = eye;
osg::Vec3d nCenter = center;
osg:uat q,qRotateUp;
nUp.normalize();
nEye.normalize();
nCenter.normalize();
qRotateUp.makeRotate(nUp,nEye);
osg::Vec3d upRotated = qRotateUp*up;
q.makeRotate(osg:egreesToRadians(rotateValue),upRotated);
center = q * center; //通过改变camera看向的点迫使他旋转
this->setByLookAt(eye,center,upRotated);
//return true;
/*}*/
}
bool MyEarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
{
Viewpoint vp = this->getViewpoint();
osg::View* view = us.asView();
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
{
now_rotate += m_rotateStep;
changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
{
now_rotate -= m_rotateStep;
changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
{
now_move += m_step;
changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
{
now_move -= m_step;
changePosition(view);
//return false;
}
}
break;
case osgGA::GUIEventAdapter::KEYUP:
{
int key = ea.getKey();
if (key == 0)
{
break;
}
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
{
now_rotate += m_rotateStep;
//changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
{
now_rotate -= m_rotateStep;
//changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
{
now_move += m_step;
//changePosition(view);
//return false;
}
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
{
now_move -= m_step;
//changePosition(view);
//return false;
}
}
break;
default:
{
return EarthManipulator::handle(ea,us);
}
}
return false;
}
|
|