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?