2

I've developed a jupyter notebook based api that seems to work well in linux, but in windows I'm getting this error that some have suggested has to do with no context being drawn, or there being no context by the time glClearColor() is called.

Not sure why the linux environment can handle it but jupyter in windows is not having it.

Any suggestions? Is this a bug in OpenGL?

C:\Python27\biovis\biovis\biovis\glwindow.py in initializeGL(self)
    132 
    133         if self.background=='black': GL.glClearColor(0.0, 0.0, 0.0, 1.0) # Black / White toggle switch
--> 134         if self.background=='white': GL.glClearColor(1.0, 1.0, 1.0, 1.0)
    135 
    136         GL.glClearDepth(10.0) # same as default

C:\Users\cm\Anaconda2\lib\site-packages\pyopengl-3.1.1a1-py2.7.egg\OpenGL\platform\baseplatform.pyc in __call__(self, *args, **named)
    400     def __call__( self, *args, **named ):
    401         if self.load():
--> 402             return self( *args, **named )
    403         else:
    404             from OpenGL import error

C:\Users\cm\Anaconda2\lib\site-packages\pyopengl-3.1.1a1-py2.7.egg\OpenGL\error.pyc in glCheckError(self, result, baseOperation, cArguments, *args)
    230                         result,
    231                         cArguments = cArguments,
--> 232                         baseOperation = baseOperation,
    233                     )
    234                 return result

GLError: GLError(
    err = 1282,
    description = 'invalid operation',
    baseOperation = glClearColor,
    cArguments = (1.0, 1.0, 1.0, 1.0)
)

EDIT:

To clarify added some code for the above, the chain of calls goes through GLWindow which calls the Widget, which in turn loads some data and explicitly calls initializeGL(), updateGL() and paintGL().

The error occurs when calling initializeGL() and none of the functions there work, perhaps the widget is not ready to plot yet? If I comment those 3 calls out the widget does load but doesn't display properly: the screen is black and only while rotating it I can see the figure flickering (the rotate code has it's own updateGL() call).

Am I missing a show() somewhere, or calling functions out of order? But why is the linux environment ok with that and not windows? (maybe the installed module versions are slightly different?)

class GLWindow(QtGui.QWidget):

     def __init__(self, fig, parent=None):
         QtGui.QWidget.__init__(self, parent)

         self.glWidget = GLWidget(fig, parent=self)

         mainLayout = QtGui.QHBoxLayout()
         mainLayout.addWidget(self.glWidget)

         self.setLayout(mainLayout)
         self.setWindowTitle(self.tr("Biovis"))

         self.show()

class GLWidget(QtOpenGL.QGLWidget):

    def __init__(self, fig, parent=None):
        QtOpenGL.QGLWidget.__init__(self, parent)

        self.setFocusPolicy(Qt.StrongFocus)
        self.lastPos = QtCore.QPoint()

        self.load_data_from_fig(fig)
        format = QtOpenGL.QGLFormat()

        format.setDoubleBuffer(True) # works fine
        self.setFormat(format)

    def load_data_from_fig(self, fig):

        self.background = fig.background
    ...

        self.initializeGL()  #Can comment these out and code works, but, the screen is black and the figure flickers in/out of existence while rotating
        self.updateGL()
        self.paintGL()

    def initializeGL(self):

        GL.glClearColor(1.0, 1.0, 1.0, 1.0)
        GL.glClearDepth(10.0) # same as default
        GL.glEnable(GL.GL_DEPTH_TEST) # 
    ...
catubc
  • 488
  • 3
  • 16
  • Could you post a short code sample that replicates this issue? GLError 1282 is pretty generic and doesn't provide much helpful information. I am guessing that you have already created the window for OpenGL to render in before you are calling glClearColor. How are you creating that window and setting up OpenGL? – CodeSurgeon Oct 19 '16 at 06:59
  • Ok, uploaded some chunks of code, hope that helps. – catubc Oct 19 '16 at 16:44
  • Sorry I am only looking at it now. Are you using qt4 or qt5? I am trying to get this to run in linux, but am struggling with how to correctly import from PyQt4 import QtOpenGL and the qglwidget. – CodeSurgeon Oct 22 '16 at 07:34
  • I'm using pyqt4, I assume it should work with qt5 as well. Here's the imports: `import sys from glwindow import GLWindow from PyQt4 import QtCore, QtGui, QtOpenGL from PyQt4.QtCore import Qt from OpenGL import GL, GLU` – catubc Oct 25 '16 at 19:40

1 Answers1

0

Below is an pyqt4 opengl demo that renders a simple triangle successfully on both windows and linux. Note that I do not call self.paintGL() or anything like that as these *GL commands are defined and called automatically pyqt library.

import sys
import math
from PyQt4 import QtCore, QtGui, QtOpenGL
from OpenGL.GL import *

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.glWidget = GLWidget()
        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.glWidget)
        mainLayout.setContentsMargins(0,0,0,0)
        self.setLayout(mainLayout)
        self.setWindowTitle("Biovis")

class GLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        super(GLWidget, self).__init__(parent)

    def minimumSizeHint(self):
        return QtCore.QSize(200, 200)

    def sizeHint(self):
        return QtCore.QSize(400, 400)

    def initializeGL(self):
        glClearColor(1, 0, 0, 1)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glBegin(GL_TRIANGLES)
        glVertex(-1, -1, 0)
        glVertex(0, 1, 0)
        glVertex(1, -1, 0)
        glEnd()

    def resizeGL(self, width, height):
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glViewport(0, 0, width, height)
        glOrtho(-1, 1, -1, 1, -1, 1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Let me know if this successfully runs for you!

CodeSurgeon
  • 2,435
  • 2
  • 15
  • 36
  • Hi thanks for the code. Unfortunately changing the constructors on both of those classes yields the same error as before. I really think it's something more complex having to do with pyqt version compatibility with windows or some of the opengl libraries... It seems like things are being called out of order in windows for some reason. – catubc Oct 26 '16 at 17:21
  • @Cat You are probably right. I tested the above code in a Windows XP 32bit Virtualbox VM. I ran the code using python 2.7.12 installed from the python.org website. PyOpenGL was installed via `python -m pip install pyopengl`. I installed QT 5.6/5.7 using the `Qt Online Installer for Windows` and followed the defaults, adding the needed PATH variables for both python and QT. I installed pyqt4 using the prebuilt `PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe`, which can be found [here](https://www.riverbankcomputing.com/software/pyqt/download). What does your setup look like? – CodeSurgeon Oct 26 '16 at 18:52
  • Hi. Well, I tested the code on Windows 8.1 pro with all installations done in windows. There could be so many different parameters to check and test for, but my colleague has the same problem on a windows 10 machine (I think). He is the one that allerted me to the errors - I had no idea the code didn't work otherwise. I do recall that MacOS also had some issues a year ago when I tried to run similar opengl routines. I probably need a macOS or windows opengl person who might know better. Thanks again. catubc – catubc Oct 27 '16 at 02:43
  • One last thing. Since an error like 1282 might indicate that the OpenGL context is not being set up properly before gl* calls are made, it might be worth trying to see if we can render anything using a different library other than pyqt to create the window. [This](http://stackoverflow.com/a/40077658/2588654) answer that I wrote for another question uses pygame to create the window instead. If you have pygame installed on your computer, try running this and post whatever error you get back. If there is no error, then it is a (py)QT issue, otherwise it looks like an OpenGL one. – CodeSurgeon Oct 27 '16 at 03:13