Get heightmap xyz by mouse
-
Hello,
I looking for something what will return xyz position on my terrain.
I used gluUnProject, but it still returning "nan" values
gluUnProject:
@GLint viewport[4];
GLdouble modelview[16], projection[16], posX, posY, posZ;
GLfloat winX, winY, winZ;// m_funcs = QOpenGLFunctions_4_1_Core m_funcs->glGetIntegerv(GL_VIEWPORT, viewport); m_funcs->glGetDoublev(GL_MODELVIEW_MATRIX, modelview); m_funcs->glGetDoublev(GL_PROJECTION_MATRIX, projection); winX = mouse_position.x(); winY = static_cast<float>(viewport[3]) - mouse_position.y(); m_funcs->glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); terrain_pos = QVector3D(posX, posY, posZ);@
gluUnProject v2:
@GLint viewport[4];
GLdouble modelview[16], projection[16], posX, posY, posZ;
GLfloat winX, winY, winZ;int indexing = 0; // passing from own Camera class QMatrix4x4 modelView, projection and viewport for(int i = 0; i < 4; i++) { viewport[i] = viewMatrix(0, i); for(int j = 0; j < 4; j++) { modelview[indexing] = modelViewMatrix(i, j); projection[indexing] = mvp(i, j); indexing++; } } winX = mouse_position.x(); winY = /*static_cast<float>(viewport[3]) -*/ mouse_position.y(); m_funcs->glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); terrain_pos = QVector3D(posX, posY, posZ);@
Every array and params have values in gluUnProject v2, so I don't know why it returns nan :(
Then I tried to use some code from google, but it returns out range positions
@QMatrix4x4 mProjection = m_camera->viewMatrix() * m_camera->projectionMatrix();
QMatrix4x4 mInverse = mProjection.inverted();float in[4]; float winZ = 1.0f; in[0] = (2.0f * ((float)(mouse_position.x() - 0) / (this->width() - 0))) - 1.0f, in[1] = 1.0f - (2.0f * ((float)(mouse_position.y() - 0) / (this->height() - 0))); in[2] = 2.0f * winZ - 1.0f; in[3] = 1.0f; QVector4D vIn(in[0], in[1], in[2], in[3]); QVector4D Vpos = vIn * mInverse; Vpos.setW(1.0f / Vpos.w()); Vpos.setX(Vpos.x() * Vpos.w()); Vpos.setY(Vpos.y() * Vpos.w()); Vpos.setZ(Vpos.z() * Vpos.z()); terrain_pos = QVector3D(Vpos.x(), Vpos.y(), Vpos.z());@
Is any better way to get heightmap xyz in Qt? Or its possible with Qt OpenGL and not with basic GL?
Using QGLWidget with QOpenGL headers and GL version 4.1