V
V
Victor2013-12-09 08:37:29
Qt
Victor, 2013-12-09 08:37:29

OpenGl traversal issue (reverse matrix multiplication and review of results)

Actually, there is a code:

paintGL() {
    //Clear color and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    //Drawing constant objects
    matrixManipulation(0, 0);
    drawRoom(light0Position, light1Position, viewPosition, GL_TRUE);
}

And the move function:
matrixManipulation(int manipulationMode, int x) {
    glLoadIdentity();
    //For right translate on x axis
    glRotated(180.0f, 0.0f, 1.0f, 0.0f);
    //Manipulation translate
    glTranslatef(coorX, coorY, coorZ);
    if(manipulationMode != 0) {
        glGetFloatv(GL_MODELVIEW_MATRIX, currentMatrix);
        glLoadIdentity();
        qDebug() << "x" << xOld << x;
        glRotated((xOld - x)/5, 0.0f, 1.0f, 0.0f);
        glMultMatrixf(currentMatrix);
    }
}

When moving the mouse, I need to perform matrix multiplication on the left to get the desired rotation around the viewer's axis.
And the function to call move when controlling the mouse:
mousePressEvent(QMouseEvent *me) {
    if(me->button() == Qt::LeftButton) {
        xOld = me->x();
        yOld = me->y();
        mouseRotate = true;
    } else mouseRotate = false;
}

mouseMoveEvent(QMouseEvent *me) {
    if(mouseRotate) {
        qDebug() << " mouseRotate" << "\n";
        float x = me->x();
        matrixManipulation(1, x);
        xOld = x;
        updateGL();
    }
}

The issue is that glTranslate works fine, but nothing happens when a mouse event occurs, but if you change the code of the matrixManipulation() function a bit like this:
matrixManipulation(int manipulationMode, int x) {
    glGetFloatv(GL_MODELVIEW_MATRIX, currentMatrix);
    glLoadIdentity();
    //For right translate on x axis
    glRotated(180.0f, 0.0f, 1.0f, 0.0f);
    //Manipulation translate
    glTranslatef(coorX, coorY, coorZ);
    if(manipulationMode != 0) {
        glLoadIdentity();
        qDebug() << "x" << xOld << x;
        glRotated((xOld - x)/5, 0.0f, 1.0f, 0.0f);
    }
    glMultMatrixf(currentMatrix);
}

Then we get a beautiful mouse movement, but now the image flickers and the glTranslate command is executed as if with a delay, that is, for example, I execute glTranslate by pressing a key, triggering a draw event, then when using the mouse I see that in parallel with the mouse glTranslate is still running, although the parameters of this function have not been changed - such is the effect of delay, if observed from the screen. Therefore, I don’t understand two points:
1) Why the mouse is not rotated with this code:
glLoadIdentity();
    //For right translate on x axis
    glRotated(180.0f, 0.0f, 1.0f, 0.0f);
    //Manipulation translate
    glTranslatef(coorX, coorY, coorZ);
    if(manipulationMode != 0) {
        glGetFloatv(GL_MODELVIEW_MATRIX, currentMatrix);
        glLoadIdentity();
        qDebug() << "x" << xOld << x;
        glRotated((xOld - x)/5, 0.0f, 1.0f, 0.0f);
        glMultMatrixf(currentMatrix);
    }

2) And why at such actions are performed acc. mouse, but the artifacts described above occur:
matrixManipulation(int manipulationMode, int x) {
    glGetFloatv(GL_MODELVIEW_MATRIX, currentMatrix);
    glLoadIdentity();
    //For right translate on x axis
    glRotated(180.0f, 0.0f, 1.0f, 0.0f);
    //Manipulation translate
    glTranslatef(coorX, coorY, coorZ);
    if(manipulationMode != 0) {
        glLoadIdentity();
        qDebug() << "x" << xOld << x;
        glRotated((xOld - x)/5, 0.0f, 1.0f, 0.0f);
    }
    glMultMatrixf(currentMatrix);
}

There is no loading of the identity matrix in third-party drawing functions. If there are wishes when writing some moments in the code, then I will only be glad. little experience.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question