Search code examples
python-3.xopenglqt5pyqt5pyopengl

Qt / openGL textures not working correctly


I am encountering weird behaviour with python3 / qt5 / openGL, when using openGL textures.

Opening two QOpenGLWidgets in two separate windows, results in openGL texture mixing between the two Widgets - changing texture in the first widget, results in texture change in the second widget. Although they should have different openGL contexts..!

A code demonstrating this problem is provided (see below) - please try it first before suggesting a solution. The code demonstrates compactly the two cases:

1) Creating two QOpenGLWidgets that share the same parent Widget

  • Works OK - no "texture mixing"

2) Creating two QOpenGLWidgets with no parents

  • Widgets open as individual windows
  • The Qt/OpenGL system messes up OpenGL textures : changing texture in the first window, changes unexpectedly the second window's texture ! (Remember to resize / click the second window to see this)

In order to fix case (2), I have also tried to force the two widgets to share the same OpenGLContext .. but with no success.

http://doc.qt.io/qt-5/qopenglwidget.html

Says the following:

Creating extra QOpenGLContext instances that share resources like textures with the QOpenGLWidget's context is also possible. 'Simply' pass the pointer returned from context() to QOpenGLContext::setShareContext() before calling QOpenGLContext::create()

Yes, I can instantiate a custom QOpenGLContext for sharing, but there is no way to force the QOpenGLWidget to use that custom context: QOpenGLWidget instantiates automatically QOpenGLContext somewhere (where?) .. I can access it only in initializeGL (with "self.context()"), but at that point the context's ".create()" method has already been called .. ! .. and thus there is no way to stuff my custom QOpenGLContext anywhere.

I also tried the following: we create a class variable

glcontext=None

At the widget's (please see first the complete example below) initializeGL method I'm trying to do this:

if (VideoImageWidget.glcontext==None):
    print("VideoImageWidget: initializeGL: creating context for sharing")
    VideoImageWidget.glcontext=QtGui.QOpenGLContext()
    ok=VideoImageWidget.glcontext.create()
    print("VideoImageWidget: initializeGL: created context for sharing",VideoImageWidget.glcontext,ok)

context=self.context()
print("VideoImageWidget: initializeGL: automatically created context:",context)
context.setShareContext(VideoImageWidget.glcontext)
ok=context.create() # must call this ..
print("VideoImageWidget: initializeGL: recreated my context:",ok)

But that does not work .. there is no image appearing to VideoImageWidget anymore.

This is a mess! Help highly appreciated!

Related:

Is it possible to use the same OpenGL context between top-level windows in Qt?

How to share OpenGL context or data?

Demo program:

import sys
import time
from PyQt5 import QtWidgets, QtCore, QtGui # Qt5
from OpenGL.GL import *
from PIL import Image

"""
Demonstrating a bug (?) in QOpenGLWidget / Qt OpenGL insfrastructure :

You need:
  * to have two tiff images ("base.tif" and "2.tif") in the same directory
  * to remove/install some libraries:
  sudo apt-get install python3-pyqt5 pip3
  sudo apt-get remove python3-opengl                     # we want the most recent version of the opengl bindings
  sudo pip3 install PyOpenGL PyOpenGL_accelerate
  sudo pip3 install imutils

Usage:
  * look for the tag "TOGGLE HERE" below to switch between nested QWidgets / individual windows
  * run program with
  python3 context_test.py

What's going on here?
  * Press the button a few times : a new image appears to the first widget
  * The image should appear only to the first widget
  * .. but it appears in both widgets if we set "nested=False", i.e. when the widgets constitute individual windows
  * .. confirm this by clicking / resizing the second window after clicking the button a few times

Why this happens?
  * Qt creates some sort of "top-level" (?) opengl context that is referring to the same texture ids = bug ?


This code is licensed under the do-with-it-whatever-you-want license, written by Sampsa Riikonen, 2017
"""

def getImg(fname):
    im =QtGui.QImage(fname)
    im =im.convertToFormat(QtGui.QImage.Format_RGB888)
    ix =im.width()
    iy =im.height()
    ptr=im.bits()
    ptr.setsize(im.byteCount())
    return ptr.asstring(), ix, iy


class VideoImageWidget(QtWidgets.QOpenGLWidget): # http://doc.qt.io/qt-5/qopenglwidget.html # Qt5
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        self.parent=parent

        self.baseimage, self.ix, self.iy =getImg("base.tif")

        self.gl_format=GL_RGB
        self.ratio     =1
        self.picratio  =1

    def changeTexture(self,image,ix,iy):
        glBindTexture(GL_TEXTURE_2D, self.tex) # this is the texture we will manipulate
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, self.gl_format, ix, iy, 0, self.gl_format, GL_UNSIGNED_BYTE, image) # load bitmap to texture
        self.picratio=self.iy/self.ix

    def resetTexture(self):
        self.changeTexture(self.baseimage,self.ix,self.iy)

    def initializeGL(self):
        # "This function should set up any required OpenGL resources and state"
        glEnable(GL_TEXTURE_2D)
        self.tex = glGenTextures(1) # create a new texture
        # https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGenTextures.xml
        # "it is guaranteed that none of the returned names was in use immediately before the call"
        print("VideoImageWidget: glGenTextures returned:",self.tex)
        self.resetTexture()

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  # Clear The Screen And The Depth Buffer
        glLoadIdentity() # Reset The View
        glBindTexture(GL_TEXTURE_2D, self.tex) # this is the texture we will manipulate
        glBegin(GL_QUADS)
        dz=0
        r=self.ratio/self.picratio # screen h/w  // picture h/w
        if (r<1):   # screen wider than image
            dy=1
            dx=r
        elif (r>1): # screen taller than image
            dx=1
            dy=1/r
        else:
            dx=1
            dy=1
        glTexCoord2f(0.0, 0.0); glVertex3f(-dx, dy, dz)
        glTexCoord2f(1.0, 0.0); glVertex3f( dx, dy, dz)
        glTexCoord2f(1.0, 1.0); glVertex3f( dx,-dy, dz)
        glTexCoord2f(0.0, 1.0); glVertex3f(-dx,-dy, dz)
        glEnd()

    def resizeGL(self, width, height):
        """Called upon window resizing: reinitialize the viewport.
        """
        glViewport(0, 0, width, height)
        glLoadIdentity()
        glOrtho(-1, 1, 1, -1, -1, 1)
        self.ratio=height/width

    @QtCore.pyqtSlot(object)
    def frameReceived(self,frame):
        buf   =frame[0]
        width =frame[1]
        height=frame[2]
        print("VideoImageWidget updating with frame",width,height)
        self.changeTexture(buf,width,height)
        self.update()


class MyGui(QtWidgets.QMainWindow):

    f1 = QtCore.pyqtSignal(object)
    f2 = QtCore.pyqtSignal(object)

    def __init__(self,parent=None):
        super().__init__(parent)
        self.cw=QtWidgets.QWidget(self)
        self.setCentralWidget(self.cw)

        self.lay = QtWidgets.QVBoxLayout(self.cw)

        self.b   = QtWidgets.QPushButton("Send frame",self.cw)

        # *** TOGGLE HERE ***
        # nested=True   # *** widgets sitting in the QMainWindow
        nested=False    # *** individual windows

        self.lay.addWidget(self.b)
        if (nested):
            self.v1  = VideoImageWidget(parent=self.cw)
            self.v2  = VideoImageWidget(parent=self.cw)
            self.lay.addWidget(self.v1)
            self.lay.addWidget(self.v2)
        else:
            self.v1  = VideoImageWidget(parent=None)
            self.v2  = VideoImageWidget(parent=None)
            self.v1.show()
            self.v2.show()

        self.b.clicked. connect(self.clicked)
        self.f1.        connect(self.v1.frameReceived)

        self.newimage, self.ix, self.iy =getImg("2.tif")

    @QtCore.pyqtSlot()
    def clicked(self):
        print("emitting frame")
        self.f1.emit([self.newimage, self.ix, self.iy])  # update _only_ the first VideoImageWidget

if (__name__=="__main__"):
    app=QtWidgets.QApplication([])

    # *** Set this to apply context sharing ***
    # app.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
    """
    .. but that does not work for textures

    See the last comment on this post:
    https://stackoverflow.com/questions/29838356/is-it-possible-to-use-the-same-opengl-context-between-top-level-windows-in-qt
    "I've realised that this does not solve the problem in the question, as it does not actually share the context, but enables sharing of a subset of resources .."

    That is said also in the qt docs, but in an extremely implicit way..
    http://doc.qt.io/qt-5/qopenglwidget.html
    "Creating extra QOpenGLContext instances that share resources like textures .. "
    """

    print("OpenGL context sharing status:",app.testAttribute(QtCore.Qt.AA_ShareOpenGLContexts))
    mg=MyGui()
    mg.show()
    app.exec_()

Solution

  • I found a way of fixing the example, but I have no idea whether it is the "correct" solution, because I know next to nothing about OpenGL (maybe that helped).

    Anyway, all I did was this:

    @QtCore.pyqtSlot(object)
    def frameReceived(self, frame):
        self.makeCurrent()
        ...
    

    According to the docs for makeCurrent:

    It is not necessary to call this function in most cases, because it is called automatically before invoking paintGL().

    So the question seems to be: what are the "other cases" alluded to here, and what makes them different...