Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget
Forum Update on Monday, May 27th 2025

Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget

Scheduled Pinned Locked Moved General and Desktop
openglqopenglwidget
3 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vagabond
    wrote on 9 Feb 2016, 22:37 last edited by Vagabond 2 Sept 2016, 22:59
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vagabond
      wrote on 10 Feb 2016, 20:32 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Ponzifex
        wrote on 27 Oct 2017, 11:23 last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved