I'm trying to figure out how to use renderdoc to debug some of my python opengl apps. At this moment some of my python opengl tests are using either pyopengl(glut) or pyqt. Here's a couple of minimal tests showing up how to initalize either glut or pyqt:
GLUT
from ctypes import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
if __name__ == "__main__":
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutSwapBuffers()
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutInitWindowPosition(800, 100)
glutCreateWindow(b'TestGLUT')
glutDisplayFunc(display)
glClearColor(1.0, 0.0, 0.0, 0.0)
glutMainLoop()
If I launch renderdoc with the above snippet using Executable Path (python path) and Command-Line arguments (-u path_to_the_glut_example.py) i'll get this output:
PYQT
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PyQt5 import QtWidgets
from PyQt5.QtOpenGL import QGLWidget
class Foo(QGLWidget):
def __init__(self, *args, **kwargs):
super(Foo, self).__init__(*args, **kwargs)
def initializeGL(self):
glClearColor(1.0, 0.0, 0.0, 0.0)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
if __name__ == "__main__":
global app
app = QtWidgets.QApplication(sys.argv)
ex = Foo()
ex.move(800,100)
ex.resize(800, 600)
ex.show()
sys.exit(app.exec_())
If I launch renderdoc with the above snippet using Executable Path (python path) and Command-Line arguments (-u path_to_the_pyqt_example.py) i'll get this output:
So, thing is I don't know how neither glutCreateWindow (glut) nor initializeGL (pyqt) are initializing the opengl context behind the curtains but it seems renderdoc doesn't like the way they are creating the opengl context. I'd like to know how I could make working renderdoc with both glut&pyqt tests.
As an aside note:
glGetIntegerv(GL_MAJOR_VERSION) ---> 4
glGetIntegerv(GL_MINOR_VERSION) ---> 5
So, this issue is not about the context opengl version (which is good enough) but the way it's been created.
Official GLUT does not support any modern OpenGL contexts. However, freeglut has some extensions on that, and PyOpenGL does support those extensions. Since freeglut is the most actively developped GLUT implementation today, and is also available for all major desktop platforms, it is quite safe to rely on freeglut. All you need to do is adding
glutInitContextVersion(3,2) # at least 3.2 is required, you can use a higer version when needed
glutInitContextProfile(GLUT_CORE_PROFILE)
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE)
before creating the window.
But not that your code will now generate a GL Error on the glLoadIdentity
, since this deprecated function is not available in core profile OpenGL at all.
If you want to go the Qt route, things are conceptually the same. Qt has the QGLFormat
class to deal with such properties, so your example becomes:
from sys import *
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtWidgets
from PyQt5.QtOpenGL import QGL
from PyQt5.QtOpenGL import QGLWidget
from PyQt5.QtOpenGL import QGLFormat
class Foo(QGLWidget):
def __init__(self, *args, **kwargs):
fmt = QGLFormat()
fmt.setVersion(3,2)
fmt.setProfile(QGLFormat.CoreProfile)
fmt.setOption(QGL.NoDeprecatedFunctions) # a.k.a. forward compatible
super(Foo, self).__init__(fmt, *args, **kwargs)
def initializeGL(self):
glClearColor(1.0, 0.0, 0.0, 0.0)
print(glGetString(GL_VERSION))
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#glMatrixMode(GL_MODELVIEW) !!! not supported in core profile
#glLoadIdentity() !!! not supported in core profile
if __name__ == "__main__":
global app
app = QtWidgets.QApplication(argv)
ex = Foo()
ex.move(800,100)
ex.resize(800, 600)
ex.show()
exit(app.exec_())
I basically only extended the __init__
method, but I had to import a couple of more stuff from PyQt5
. Note that I also removed the GLUT
import, since it is not needed at all when you use Qt.
Both of these examples create a core profile on my Linux system, using python3. I've no idea if renderdoc will now be happy with the contexts you give it. These changes will at least address both error messages which it presented for your original examples, and in principle, it should work.