PyQT5 的 OpenGL FrameBuffer 离屏、同屏渲染问题

OpenGL FrameBuffer off-screen, on-screen rendering problem with PyQT5

我正在尝试渲染到 fbo 以使用 glReadPixels。我可以渲染到 fbo 并读取像素值,但我无法在屏幕上渲染它,我收到这样的错误:

err = 1286, description = b'invalid framebuffer operation',

这是我的步骤:

    class MyGLWidget(QOpenGLWidget):
        ...
        ...
        def paintGl():
           ### offscreen rendering ###

           glBindFramebuffer(GL_FRAMEBUFFER, self.fbo);
           glClearColor(0.0, 0.0, 0.0, 1.0);
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
           # ... bind texture ...
           # ... shader program...
           # ... VAO
           # ... EBO
           # ... uniforms
           # ... draw

           glBindFramebuffer(GL_FRAMEBUFFER, 0) #<--- The problem is here. i don't know why
    
           ### read pixel ###
           glBindFramebuffer(GL_READ_FRAMEBUFFER, self.fbo)
           glReadBuffer(GL_COLOR_ATTACHMENT0)
           print(glReadPixels(0, 0, _width, _height, _format, _type)) #<-- I got correct value here.

           ### on screen render ###
           glClearColor(0.0, 0.0, 0.0, 1.0) #<---I got an error here
           glClear(GL_COLOR_BUFFER_BIT)
    
           # ... bind texture (color attachment texture)
           # ... shader program...
           # ... VAO
           # ... EBO
           glDrawElements(mode, count, _type, indices)  #<--- If i don't use glClear, I get an error here.
           

问题是行 glBindFramebuffer(GL_FRAMEBUFFER, 0) 如果我删除该行,我不会收到错误消息,但会出现黑屏。 我不知道我做错了什么,或者可能是我的显卡错误或 QOpenGLWidget 中的错误。 希望你能帮忙。 提前致谢。

glBindFramebuffer(GL_FRAMEBUFFER, 0) #<--- The problem is here. i don't know why

来自 documentation for QOpenGLWidget(强调我的):

All rendering happens into an OpenGL framebuffer object. makeCurrent() ensure that it is bound in the context. Keep this in mind when creating and binding additional framebuffer objects in the rendering code in paintGL(). Never re-bind the framebuffer with ID 0. Instead, call defaultFramebufferObject() to get the ID that should be bound.