ls6576837 发表于 2013-12-16 18:47:26

重写EarthManipulator遇到键盘KEYUP诡异问题

如题,最近在改下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::Quat q,qRotateUp;

                nUp.normalize();
                nEye.normalize();
                nCenter.normalize();


                qRotateUp.makeRotate(nUp,nEye);
                osg::Vec3d upRotated = qRotateUp*up;

                q.makeRotate(osg::DegreesToRadians(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;
       
}

ls6576837 发表于 2013-12-17 17:30:29

问题已解决,改用osg3.2就没有这个问题了

floor 发表于 2013-12-20 17:28:33

非常的不错的啊,谢谢楼主的分享啊

xiaol_deng 发表于 2014-3-13 14:50:30

l我也重写了一个操作器,但是实现的方式与楼主有些不同,我遇到的问题是我拖动地球时地球会出现闪烁的现象。我的类是继承CameraManipulator 通过改变getMatrix 和getMatrixInverse 的矩阵来改变相机的位置和方向等。请教楼主
页: [1]
查看完整版本: 重写EarthManipulator遇到键盘KEYUP诡异问题