Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Terrain 3D position from mouse coordinates

  1. #1
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    54

    Terrain 3D position from mouse coordinates

    Hello,

    I have big problem, I don't know why gluUnProject returning NaN values, so I trying to build own gluUnProject, but is failing.

    Target platform: Crossplatform
    Framework: Qt5
    Library: QOpenGL, not QGL!
    OpenGL ver: 4.0+
    Current OS: Windows 8

    So I’m trying to get terrain coords from my own camera class with QMatrix44, but it returns incorrets 3D 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() * m_modelMatrix);
    2. inverted = inverted.inverted();
    3. float posZ;
    4. // m_funcs = QOpenGLFuncions_4_0_Core
    5. m_funcs->glReadPixels(mouse_position.x(), mouse_position.y(), 1, 1,GL_DEPTH_COMPONENT, GL_FLOAT, &posZ);
    6. QVector4D clickedPointOnScreen(mouse_position.x(),mouse_position.y(), posZ, 1.0f);
    7. QVector4D clickedPointIn3DOrgn = inverted * clickedPointOnScreen;
    8. terrain_pos = clickedPointIn3DOrgn.toVector3D();
    9. qDebug() << terrain_pos;


    I’m with camera on position: 250.0f, 10.0f, 250.0f
    And I’m getting in result: QVector3D(414.76, 105.6, 6.37669) (cursor is in center of QGLWidget screen [935, 475], i calculated coords). It should be QVector3D(250.0f, 10.0f, 260f+-)

    To calculated coords: I have fullHD = 1920x1080, but QGLWidget is only 1870x950
    To QGLWidget: I'm using it because QtGui cannot have controls which I needed (making editor)

    Edit: Camera is perspective mode!
    Edit2: posZ is often 0.9xxx, maximum 1
    Last edited by glararan; 08-09-2013 at 01:43 PM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by glararan View Post
    I have big problem, I don't know why gluUnProject returning NaN values,
    It's probably dividing zero by zero, which probably indicates that the arrays being passed are incorrect.
    Quote Originally Posted by glararan View Post
    so I trying to build own gluUnProject, but is failing.
    You need to concatenate the viewport, projection and model-view transformations (in that order: V*P*M), invert the result, transform the window coordinates by the resulting matrix, then divide by w to convert from homogeneous to Euclidean coordinates.

    Note that the Z coordinate needs to be converted from the 0..1 range returned by glReadPixels() to the -1..+1 range, either explicitly or as part of the viewport transformation (gluUnProject treats this as part of the viewport transformation). Also note that OpenGL window coordinates have (0,0) in the lower left while GUI toolkits typically report mouse coordinates with (0,0) at the upper left.

    If gluUnProject() isn't working because you're feeding it incorrect data, feeding the same incorrect data to your own version won't work any better.

  3. #3
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    54
    Z coordinate now returning 5.67406e-39 That's bad right? Or not? (Changing every time u run app ex to: 1.28372e-38)
    Yes viewport lower left is 0, 0

    Currently coords at (935, 475) QVector3D(250, 9.99991, 249.8)
    But if you will see screen it's still not correct. Link to image. On screen its alot away! It should be QVector3D(250, 10, 230)


    Final code: (Forum remove whitespaces :/ pastebin: http://pastebin.com/rReETsJC )

    boolinvert=false;


    QMatrix4x4viewMatrix=m_camera->viewMatrix();
    QMatrix4x4modelViewMatrix=viewMatrix*m_modelMatrix;
    QMatrix4x4modelViewProject=m_camera->projectionMatrix()*modelViewMatrix;
    QMatrix4x4inverted=m_viewportMatrix*modelViewProject; // m_viewportMatrix is from resizeGL from calculated glViewport


    inverted=inverted.inverted(&invert);


    floatposZ;
    floatposY=(float)this->width()-mouse_position.y();


    m_funcs->glReadPixels(mouse_position.x(),posY,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&posZ);


    QVector4DclickedPointOnScreen(mouse_position.x(),mouse_position.y(),posZ,1.0f);
    QVector4DclickedPointIn3DOrgn=inverted*clickedPointOnScreen;


    clickedPointIn3DOrgn=clickedPointIn3DOrgn/clickedPointIn3DOrgn.w();


    terrain_pos=clickedPointIn3DOrgn.toVector3D();


    EDIT: when I remove in glReadPixels posY to mouse_position.y() it gives right coords when I arrive to position on nearClip, but from farClip it's still wrong :/ I think, here is problem with posZ
    Last edited by glararan; 08-10-2013 at 04:23 AM.

  4. #4
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    54
    Hi, BUMPING

    I have solved why posZ returning wrong value, it was because posY was width() - mouse height!

    But I have still trouble with final Vector3D result! Still Vector3D.Z returning wrong value -20f

    Current source code: http://pastebin.com/TX4RYmNM

    Currently QVector3D on [935, 475] = QVector3D(249.997, 10.0002, 216.095)
    Should be QVector3D(250, 10, 230), I don't care about X, Y +- 0.0003


  5. #5
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    54
    New discovering...

    When I rotate camera in axis X to left by 90 degrees, my cursor Z axis is now correct, but cursor X have incorrect values, same with axis Y, when I rotate camera on axis Y to -90 degrees I get -values.

    Any help to this? What's wrong? Some calculation with Matrix? How it can be fixed? Same code as preview post.

    This is my last problem, please let comment if you have any idea!

    Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •