Jump to content
  • Advertisement
Sign in to follow this  
glararan

OpenGL Qt5 - QOpenGL terrain, trying get terrain coords by mouse

This topic is 1897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I looking for something what will return xyz position on my terrain.

I used gluUnProject, but it still returning “nan” values

gluUnProject:

  1. GLint viewport[4];
  2.         GLdouble modelview[16], projection[16], posX, posY, posZ;
  3.         GLfloat winX, winY, winZ;
  4.  
  5.        // m_funcs = QOpenGLFunctions_4_1_Core
  6.        m_funcs->glGetIntegerv(GL_VIEWPORT, viewport);
  7.        m_funcs->glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  8.        m_funcs->glGetDoublev(GL_PROJECTION_MATRIX, projection);
  9.  
  10.         winX = mouse_position.x();
  11.         winY = static_cast<float>(viewport[3]) - mouse_position.y();
  12.  
  13.         m_funcs->glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT,GL_FLOAT, &winZ);
  14.         gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX,&posY, &posZ);
  15.  
  16.         terrain_pos = QVector3D(posX, posY, posZ);

 

gluUnProject v2:

  1. GLint viewport[4];
  2.         GLdouble modelview[16], projection[16], posX, posY, posZ;
  3.         GLfloat winX, winY, winZ;
  4.  
  5.         int indexing = 0;
  6.  
  7.         // passing from own Camera class QMatrix4x4 modelView, projection and viewport
  8.         for(int i = 0; i < 4; i++)
  9.         {
  10.             viewport[i] = viewMatrix(0, i);
  11.  
  12.             for(int j = 0; j < 4; j++)
  13.             {
  14.                 modelview[indexing] = modelViewMatrix(i, j);
  15.                 projection[indexing] = mvp(i, j);
  16.  
  17.                 indexing++;
  18.             }
  19.         }
  20.  
  21.         winX = mouse_position.x();
  22.         winY = /*static_cast<float>(viewport[3]) -*/ mouse_position.y();
  23.  
  24.         m_funcs->glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT,GL_FLOAT, &winZ);
  25.         gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX,&posY, &posZ);
  26.  
  27.         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

  1. QMatrix4x4 mProjection = m_camera->viewMatrix() * m_camera->projectionMatrix();
  2.         QMatrix4x4 mInverse = mProjection.inverted();
  3.  
  4.         float in[4];
  5.         float winZ = 1.0f;
  6.  
  7.         in[0] = (2.0f * ((float)(mouse_position.x() - 0) / (this->width() - 0))) - 1.0f,
  8.         in[1] = 1.0f - (2.0f * ((float)(mouse_position.y() - 0) / (this->height() - 0)));
  9.         in[2] = 2.0f * winZ - 1.0f;
  10.         in[3] = 1.0f;
  11.  
  12.         QVector4D vIn(in[0], in[1], in[2], in[3]);
  13.  
  14.         QVector4D Vpos = vIn * mInverse;
  15.  
  16.         Vpos.setW(1.0f / Vpos.w());
  17.         Vpos.setX(Vpos.x() * Vpos.w());
  18.         Vpos.setY(Vpos.y() * Vpos.w());
  19.         Vpos.setZ(Vpos.z() * Vpos.z());
  20.  
  21.         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? Or why it doesnt work?

Using QGLWidget with QOpenGL headers and GL version 4.1

 

Pic from app: (I have debug mode, so I see QVector3D posX, posY, posZ returning nan)

34df6076f7cd758f8ba10f6ca928e875.png

 

Share this post


Link to post
Share on other sites
Advertisement

BUMP

 

I’m trying to get terrain coords from camera QMatrix4×4, but it returns incorrets values.

Im using in camera these properties:
viewMatrix, projectionMatrix and viewProjectionMatrix.

I tried to figure it out with this code:

  1.         QMatrix4x4 inverted = m_camera->projectionMatrix() * m_camera->viewMatrix();
  2.         inverted = inverted.inverted();
  3.  
  4.         float posZ;
  5.  
  6.         // m_funcs = QOpenGLFuncions_4_0_Core
  7.         m_funcs->glReadPixels(mouse_position.x(), mouse_position.y(), 1, 1,GL_DEPTH_COMPONENT, GL_FLOAT, &posZ);
  8.  
  9.         QVector4D clickedPointOnScreen(mouse_position.x(),mouse_position.y(), posZ, 1.0f);
  10.         QVector4D clickedPointIn3DOrgn = inverted * clickedPointOnScreen;
  11.  
  12.         terrain_pos = clickedPointIn3DOrgn.toVector3D();
  13.  
  14.         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 ofQGLWidget screen, i calculated coords). It should be QVector3D(250.0f, 10.0f, 260f+-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!