#!/usr/bin/env python """ This program draws a single red rectangle in a blue background. It is a demonstration of the structure needed to mix Python + PyQt (for the window control) + PyOpenGL. Code includes commented-out lines to use the more efficient OpenGL Displays lists. """ #import pydoc #Not used __author__= "Joseph O'Rourke" __version__ = "1.0" __date__= "31Aug06" import sys #import math #Not used from qt import * from qtgl import * from OpenGL.GL import * ############################################################################## def rect(): """ Draw a single rectangle in the first quadrant. Assumes [0,1]^3 coordinate system. """ glColor3f (1.0, 0.0, 0.0) #Red glBegin(GL_POLYGON) glVertex3f (0.25, 0.25, 0.0) glVertex3f (0.75, 0.25, 0.0) glVertex3f (0.75, 0.75, 0.0) glVertex3f (0.25, 0.75, 0.0) glEnd() ############################################################################## class MyWidget(QGLWidget): """ All the stuff necessary to make Qt + QtOpenGL work together """ def __init__(self,parent=None,name=None): QGLWidget.__init__(self,parent,name) self.startTimer(100) # milliseconds def timerEvent(self,event): self.updateGL() def paintGL(self): """ What to do when the screen is repainted: draw the rectangle. """ glClear( GL_COLOR_BUFFER_BIT ) rect() #How to do it instead via a CallList: #glCallList(self.rect1) def resizeGL(self,width,height): """ What to do when the window is resized: reset coord system etc. """ w = width / float(height) h = 1.0 glViewport( 0, 0, width, height ) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) # Use orthog proj, [0,1]^3 # Note: first four numbers are xmin, xmax, ymin, ymax. glMatrixMode(GL_MODELVIEW) glLoadIdentity() def initializeGL(self): """ Upon first invocation """ glClearColor(0.0, 0.0, 1.0, 0.0) #This is how to set up a display list, whose invocation by glCallList #would display the rectangle: #self.rect1=glGenLists(1) #glNewList(self.rect1,GL_COMPILE) #rect() #glEndList() ############################################################################## if __name__=='__main__': QApplication.setColorSpec(QApplication.CustomColor) app=QApplication(sys.argv) if not QGLFormat.hasOpenGL(): raise 'No Qt OpenGL support.' widget=MyWidget() app.setMainWidget(widget) widget.show() app.exec_loop()