0

I am trying to move a 3D object ,using the mouse, in a 3D space.

So,I retrieve the actual matrixes and use the gluUnProject (that map window coordinates to object coordinates) to obtain the actual x,y,z from the mouse position dx and dy.

By design my project has 3 functions to translate objects,one for each axis.So everywhen happens an event like 'mouseMoveEvent' it emits a signal 'mouseHasMoved(x,y,z)' (x,y,z obtained from gluunproject and scaled down).

This signal (QT based) is catched by the controller that invokes the three methods I spoke above.

As it is it works but there is a lot of lag,and the movement is not precise.The select object tend to go farther from the parallel plan it should stay.

Here is the sample code of 'mouseMoveEvent':

if (event->buttons() & Qt::RightButton) {

    gluUnProject(event->x(), -event->y(), 0, model_matrix, proj_matrix, view_matrix, &newx, &newy, &newz);

    if (event->x()>lastPos.x()){
        emit SignalPreviewMouseXChanged((float)(newx/1000));
        emit SignalPreviewMouseZChanged((float)(-newz/1000));
    }
    else {
        emit SignalPreviewMouseXChanged((float)(-newx/1000));
        emit SignalPreviewMouseZChanged((float)(newz/1000));
    }


    if (event->y()<lastPos.y())
        emit SignalPreviewMouseYChanged((float)(newy/500));
    else {
        emit SignalPreviewMouseYChanged((float)(-newy/500));
    }

}
lastPos = event->pos();
updateGL();

Where objCamera->mPos is the camera Position.

This sets up matrixes for gluUnProject()

void setMatrixes()
{
  glGetDoublev(GL_MODELVIEW_MATRIX, model_matrix);
  glGetDoublev(GL_PROJECTION_MATRIX, proj_matrix);
  glGetIntegerv(GL_VIEWPORT, view_matrix);
}

Same translation functions are linked with 3 QSliders (x,y,z) and they are accurate and fast.So that's not a function problem.Here's an example of one the methods that is called when signal is catched

void SlotZRotationValueChanged(float val){
  sceneRef->translate(0,0,tValue*10);
  facade->window->getPreview()->RebuildScene();
}

The question is: what I am doing is logically correct?

4

2 回答 2

1

部分解决(请阅读本文底部)将上下移动与左右分离:将上述 mousemove 函数更改为

if (event->buttons() & Qt::RightButton) {
    setMatrixes();


    gluUnProject(event->x(), -event->y(), 0, model_matrix, proj_matrix, view_matrix, &newx, &newy, &newz);

    float acc=sqrt((int)dx^2+(int)dy^2);
    acc*=acc;


    qDebug()<< acc;

    if (dy<dx){
        if (event->x()>lastPos.x()){
            emit SignalPreviewMouseChanged((float)(newx/2500)*acc,0,(float)(-newz/2500)*acc);
        }
        else {
            emit SignalPreviewMouseChanged((float)(-newx/2500)*acc,0,(float)(newz/2500)*acc);
        }
    }

    else{
        if (event->y()<lastPos.y())
            emit SignalPreviewMouseChanged(0,(float)(newy/1750)*acc,0);
        else {
            emit SignalPreviewMouseChanged(0,(float)(-newy/1750)*acc,0);
        }
    }
    isPressed=true;
}
lastPos = event->pos();
updateGL();

在控制器中:

void Controller::SlotMouseTranslationValueChanged(float val1, float val2, float val3){

if (val1!=0) sceneRef->translate(val1,0,0);
if (val2!=0) sceneRef->translate(0,val2,0);
if (val3!=0) sceneRef->translate(0,0,val3);
facade->window->getPreview()->RebuildScene();
}

现在它没有滞后,一切正常。

考虑:

现在我没有拖动对象,而是使用 gluUnproject 来检索我的对象可以移动的计划(然后调用将添加\sub 值添加到对象位置的翻译函数)。所以我的鼠标指向的位置与对象的位置不同在我的屏幕上。

如何达到这个结果?有人可以解释我的逻辑步骤吗?

于 2013-07-01T23:19:46.850 回答
1

您可以为此使用“挑选”技术。检查本教程 http://www.lighthouse3d.com/opengl/picking/

于 2013-07-20T08:16:00.310 回答