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 4 of 4

Thread: Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget outside of paintGL() function

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    15

    Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget outside of paintGL() function

    Hi all. The problem seems to be special for Qt5 class QopenGLWidget. Similar code works properly under MFC implementation using GLEW.
    I try to use glReadPixels with GL_DEPTH_COMPONENT to read the z depth value of the point under the mouse cursor. The z value is simply not read and the consequent glGetError() call returns GL_INVALID_OPERATION code which in turn means there is no depth buffer. I set glEnable(GL_DEPTH_TEST) in initializeGL() of my GLWidget derived from QOpenGLWidget. I also call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); in painGL().
    If I check glIsEnabled(GL_DEPTH_TEST) it returns GL_FALSE in my picking function (outside of paintGL()) and GL_TRUE in paintGL() where the rendering code actually is. How can it be? I would assume that GL_DEPTH_TEST is either always enabled or always disabled. Anyway I do not disable it by myself. Does anyone know does QOpenGLWidget do some strange things with depth buffer between paintGL() calls?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,569
    Have you tried calling grabFramebuffer()?

    The docs say that QOpenGLWidget internally renders to an off-screen FBO. So my guess is that where you're calling glReadPixels(), that FBO is just not bound and instead some other framebuffer is. If so, you either need to:

    • use a Qt API to query the contents for you (e.g. grabFramebuffer()), or
    • coax Qt to bind that FBO for you first before glReadPixels() on your side is going to work (e.g. makeCurrent()), or
    • coax Qt into giving you the FBO framebuffer handle so you can bind it yourself before glReadPixels() on your side is going to work.

    Keep in mind that there are other reasons that glReadPixels() might return GL_INVALID_OPERATION, though I suspect your guess is the most likely reason here.
    Some related threads:

    Last edited by Dark Photon; 03-03-2018 at 11:30 AM.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    15
    Quote Originally Posted by Dark Photon View Post
    Have you tried calling grabFramebuffer()?

    The docs say that QOpenGLWidget internally renders to an off-screen FBO. So my guess is that where you're calling glReadPixels(), that FBO is just not bound and instead some other framebuffer is. If so, you either need to:

    • use a Qt API to query the contents for you (e.g. grabFramebuffer()), or
    • coax Qt to bind that FBO for you first before glReadPixels() on your side is going to work (e.g. makeCurrent()), or
    • coax Qt into giving you the FBO framebuffer handle so you can bind it yourself before glReadPixels() on your side is going to work.

    Keep in mind that there are other reasons that glReadPixels() might return GL_INVALID_OPERATION, though I suspect your guess is the most likely reason here.
    Some related threads:

    Many thanks! Yes I have tried grabFramebuffer(). It has worked. But it is only able to obtain a 32-bit QImage which I can e.g. redirect into external file. What I need is the depth buffer.
    Anyway you are right the FBO is not bound at the position of code where I need it. I need to find out the way to bind the FBO within QOpenGLWidget.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    15
    The solution is calling makeCurrent() prior to the glReadPixels call. makeCurrent() ensures the FBO binding and then the depth buffer is available for reading. Thanks for good idea!

Posting Permissions

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