Own gluUnProject with QMatrix4x4 and Camera
-
Hello,
I found example to create own gluUnProject, because gluUnProject not working for me, returning NaN values.
So I'm trying to get terrain coords from camera QMatrix4x4, but it returns incorrets values.Im using in camera these properties:
viewMatrix, projectionMatrix and viewProjectionMatrix.I tried to figure it out with this code:
@ QMatrix4x4 inverted = m_camera->projectionMatrix() * m_camera->viewMatrix();
inverted = inverted.inverted();float posZ; // m_funcs = QOpenGLFuncions_4_0_Core m_funcs->glReadPixels(mouse_position.x(), mouse_position.y(), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &posZ); QVector4D clickedPointOnScreen(mouse_position.x(), mouse_position.y(), posZ, 1.0f); QVector4D clickedPointIn3DOrgn = inverted * clickedPointOnScreen; terrain_pos = clickedPointIn3DOrgn.toVector3D(); qDebug() << terrain_pos;@
I'm with camera on position: 250.0f, 10.0f, 250.0f
And I'm getting: QVector3D(415.715, 99.3959, 6.45992) (cursor is in center of QGLWidget screen, i calculated coords). It should be QVector3D(250.0f, 10.0f, 260f+-)