QGLWidget and point picking

  • Hi!

    I've discovered the "Panini Perspective Tool ":http://sourceforge.net/projects/pvqt/ and I'm using it to play around with some panoramic images. It's written in Qt using the QtOpenGL module.

    I'm trying to do some mouse picking in the panoramic images and I've managed to retrieve the Z coord from the mouse position (at least, I think so).

    The code I have for retrieving the Z-coord is:


    void pvQtView::initializeGL() {

    void pvQtView::mousePressEvent( QMouseEvent * pme ) {
    GLfloat z;
    glReadPixels( pme->x(), Height - pme->y(), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
    GLenum error = glGetError();
    if(GL_NO_ERROR != error) throw;
    qDebug() << "X: " << pme->x() << ", Y: " << pme->y() << ", RETRIEVED Z: " << QString::number(z, 'd', 6 );
    qDebug( "\tERROR: %s (Code: %u)\n", gluErrorString(error), error );

    That code, for a mouse click in [X: 388 , Y: 300] will give me Z = 0.912548.

    I've clicked in different parts of the image and the Z coord changes wheter I click on the forefront of the image or the background, so to speak. If I click a point near the camera position, I receive a lesser Z-value than If I click on a point which is farther from the camera position.

    [Check the attached image. When clicking in the zone labeled as 1, I receive a Z-value of 0.924145, whereas clicking in zone 2, the Z-value is 0.924145. I understand that the greater the Z-value is, the farther the object is from the observer.]

    !http://s11.postimg.org/4pj3xm0xv/panotest.jpg(Panoramic example)!

    Now, I've been reading some OpenGL docs in order to convert this coordinates in screen space to something meaningful. I've tried this:

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );
    double posX, posY, posZ;
    gluUnProject( pme->x(), Height - pme->y(), z, modelview, projection, viewport, &posX, &posY, &posZ);
    error = glGetError();
    if(GL_NO_ERROR != error) throw;
    qDebug() << "3D point with POS: " << QString::number( posX, 'd', 6 ) << QString::number( posY, 'd', 6 ) << QString::number( posZ, 'd', 6 );
    qDebug( "\tERROR: %s (Code: %u)\n", gluErrorString(error), error );

    But this will give me:

    3D point with POS: "-0.139124" "-0.609829" "0.781416"

    This seems to be normalized and I wanted the "real" coords, without normalizing.

    So, the questions, at last:

    Did I the Z-coord retrieval right? Did I messed up with the parameters of glReadPixels?

    If glReadPixels returned the right Z-coord, what am I missing in the conversion of the mouse coords to the OpenGL coords?


    • I'm using Qt 4.8, with the MinGW compiler in a Windows 8.1 64 bits environment.
    • Height is updated inside the resizeGL method. It simply stores the window height each time a resize occurs.
    • I tried to retrieve the RGBA color values from the panoramic pictures and it seems that this is working fine. I convert the RGBA values to hex and visualize the color in an online tool and it matches the color in the image (I'm doing this visually, so there might be slight changes. But if I click on a white car, the online tool shows a white result).

    Thanks and cheers!

  • Hello!

    Does anyone have any idea? I'm still struggling with this.

    I've reading a lot through Google and found "this":https://www.opengl.org/discussion_boards/showthread.php/170900-Getting-real-world-coordinates-from-screen

    If I'm not mistaken, they're doing the same as me.

    Could the problem be that when the Panoramic viewer calls paintGL, it is scaled, rotated, translated and glFrustrum is also called?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.