Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget

  • I am implementing a simple click selection for 3D objects inside a QOpenGLWidget. To do this I need to transform 2D mouse coordinates into 3D worldspace. I had previously implemented the whole thing using QGLWidget. Using QOpenGLWidget, I am not able to read the GL_DEPTH_COMPONENT of a pixel:

    float z;
    glReadPixels(pixel.x, height - pixel.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);

    'z' is always 0.
    To make sure my pixel coordinates were correct I tried receiving GL_RGBA values:

    float rgba[4];
    glReadPixels((int)p_temp.x(), (int) (viewport[3] - p_temp.y()), 1, 1, GL_RGBA, GL_FLOAT, rgba);

    , which returns the correct pixel Colors.
    In order for this to work I had to change the domain of the pixel coordinates from local to parent coordinates. This probably results from the fact that the GL_VIEWPORT is set corresponding to the parent widgets size:

    int viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);

    for QGLWidget this returns: {0, 0, this->width(), this->height()}
    for QOpenGLWidget this returns: {0, 0, this->parent()->width(), this->parent()->height()}

    Now I am kind of clueless as to what I might be missing. Any ideas?
    Is there maybe an in-built Qt OpenGL convenience function that I could rely on to get the depth component of a pixel?

    EDIT: my OpenGL version is 4.5 and I do not get any OpenGL error using glReadPixels with GL_DEPTH_COMPONENT

  • Is there maybe an additional render pass using the QOpenGLWidget, which clears the depth buffer before I read from it? I am certain that I am not clearing it myself before the glReadPixels call. As I mentioned, it worked with QGLWidget.

  • QOpenGLWidget is works into an FBO. Probably the surface format was set to multisampled therefore the underlying FBO inherited that and reading GL_DEPTH_COMPONENT from a multisampled FBO is not possible. To check this set samlpes to zero in QSurfaceFormat and pass it to QOpenGLWidget.

Log in to reply

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