Sunday, February 05, 2012

Proof of concept OpenGL program in python and Qt/PySide

Here is the output of my initial test program:

I am planning to test the performance of various ways of rendering lots of spheres using OpenGL. I will use python and Qt to run the tests. As a first step, I have created a very light hello program that renders the classic Utah teapot.


# File
# "hello world" type rendering of the classic Utah teapot
# Requires python modules PySide and PyOpenGL

from PySide.QtGui import QMainWindow, QApplication
from PySide.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys

def override(interface_class):
Method to implement Java-like derived class method override annotation.
Courtesy of mkorpela's answer at
def override(method):
assert(method.__name__ in dir(interface_class))
return method
return override

class SphereTestGLWidget(QGLWidget):
"GUI rectangle that displays a teapot"
def initializeGL(self):
"runs once, after OpenGL context is created"
glClearColor(1,1,1,0) # white background
glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
glMaterialfv(GL_FRONT, GL_SHININESS, [50.0])
glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glLightfv(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [1.0, 1.0, 1.0, 0.0])
gluLookAt(0, 0, -10, # camera
0, 0, 0, # focus
0, 1, 0) # up vector

def paintGL(self):
"runs every time an image update is needed"

def resizeGL(self, w, h):
"runs every time the window changes size"
glViewport(0, 0, w, h)

def orientCamera(self):
"update projection matrix, especially when aspect ratio changes"
glPushAttrib(GL_TRANSFORM_BIT) # remember current GL_MATRIX_MODE
gluPerspective (60.0, self.width()/float(self.height()), 1.0, 10.0)
glPopAttrib() # restore GL_MATRIX_MODE

def paintTeapot(self):
glPushAttrib(GL_POLYGON_BIT) # remember current GL_FRONT_FACE indictor
glFrontFace(GL_CW) # teapot polygon vertex order is opposite to modern convention
glColor3f(0.2,0.2,0.5) # paint it blue
glutSolidTeapot(3.0) # thank you GLUT tool kit
glPopAttrib() # restore GL_FRONT_FACE

class SphereTestApp(QApplication):
"Simple application for testing OpenGL rendering"
def __init__(self):
QApplication.__init__(self, sys.argv)
self.mainWindow = QMainWindow()
self.gl_widget = SphereTestGLWidget()
self.mainWindow.resize(1024, 768)
sys.exit(self.exec_()) # Start Qt main loop

if __name__ == "__main__":


Catalin George Festila said...

I got this errors ... I think is something with override or decorators :
Traceback (most recent call last):
File "", line 53, in paintGL
File "", line 73, in paintTeapot
glutSolidTeapot(3.0) # thank you GLUT tool kit
File "C:\Python34\lib\site-packages\OpenGL\platform\", line 407
, in __call__
self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutSolidT
eapot, check for bool(glutSolidTeapot) before calling

Biospud said...

@Catalan George Festila: I think you might need to install GLUT on your computer to get around a problem like that. There is something called FreeGLUT that might do the trick.