catubc
catubc

Reputation: 508

PyOpenGL glClearColor error in Windows, ok in linux

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) # 
    ...

Upvotes: 2

Views: 1287

Answers (1)

CodeSurgeon
CodeSurgeon

Reputation: 2465

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!

Upvotes: 0

Related Questions