How to transform model with Mouse in modern OpenGL

It’s difficult to control model with mouse to me. I define the 4-dimension matrix of projection, camera, and model, and the mvp matrix is :

$$mvp = projection * camera * model$$

In the mouse movement event loop, I rotate the camera matrix like:

$$camera.rotate(dy, glm::vec3(1.0f, 0.0f, 0.0f))$$
$$camera.rotate(dx, glm::vec3(0.0f, 1.0f, 0.0f))$$

and then I re-calculate the mvp. But the model does not rotate in the direction of the mouse.

This is my code:

void GLWidget::mouseMoveEvent(QMouseEvent *event)
    float dx = static_cast<int>(event->localPos().x() - m_lastPos.x());
    float dy = static_cast<int>(event->localPos().y() - m_lastPos.y());
//    m_camera.setToIdentity();
//    m_camera.translate(0.0f, 0.0f, -500.0f);
//    m_camera.rotate(45, QVector3D(1.0f, 0.0f, 0.0f));
//    m_camera.rotate(-45, QVector3D(0.0f, 1.0f, 0.0f));
    if (event->buttons() & Qt::LeftButton)
        m_camera.rotate(dy, QVector3D(1.0f, 0.0f, 0.0f));//m_camera rotate δθ
        m_camera.rotate(dx, QVector3D(0.0f, 1.0f, 0.0f));
        mvp = m_proj * m_camera * m_model;


I’m guessing that you want a trackball (aka arcball) control.

Thank you for your reply. I’v solved this question.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.