How to use Qt's QVector3D::unproject() function?

1.2k views Asked by At

I'm doing an opengl project in Qt with the new QOpenGLWidget class. I'm just trying to get the opengl world coordinates of my mouse when I click. I've found a lot of ways to do it but some were obsolete and the others don't work. Then I found the QVector3D::unproject() function but I don't get the right coordinates. This is a part of my code so far :

QMatrix4x4 modelView;
modelView.setToIdentity();
modelView.lookAt(m_camera.getPosition(), m_camera.getTarget(), m_camera.getUp());

QMatrix4x4 projection;
projection.perspective(70.0, width() / height(), 0.1, 120.0);

GLint view[4];
glGetIntegerv(GL_VIEWPORT, &view[0]);

QVector3D worldPosition = QVector3D(event->pos().x(), event->pos().y(), 0).unproject(modelView, projection, QRect(view[0], view[1], view[2], view[3]));
qDebug() << worldPosition;

Do you know why I get (1.96532, -3.93444, 4.93216) where I should have (-0.5, -0.5, 0.0) ?

0

There are 0 answers