License Public Domain
Lines 96
Keywords
gui (8) OpenGL (15) PyQt (6) Qt (4)
Included in this Library
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

Using OpenGL with PyQt Atom Feed 0

In Brief A quick example of using OpenGL with PyQt4. When the script is run it will display two "spirals". The red spiral is drawn in immediate mode (glBegin/glEnd). The green spiral is drawn using array immediate mode (Vertex Arrays).... more
# 's
  1'''
2Created on Jul 7, 2009
3
4@author: Stou Sandalski (stou@icapsid.net)
5@license: Public Domain
6'''
7
8import math
9
10from OpenGL.GL import *
11from OpenGL.GLU import *
12from PyQt4 import QtGui
13from PyQt4.QtOpenGL import *
14
15class SpiralWidget(QGLWidget):
16 '''
17 Widget for drawing two spirals.
18 '''
19
20 def __init__(self, parent):
21 QGLWidget.__init__(self, parent)
22 self.setMinimumSize(500, 500)
23
24 def paintGL(self):
25 '''
26 Drawing routine
27 '''
28
29 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
30 glLoadIdentity()
31
32 # Draw the spiral in 'immediate mode'
33 # WARNING: You should not be doing the spiral calculation inside the loop
34 # even if you are using glBegin/glEnd, sin/cos are fairly expensive functions
35 # I've left it here as is to make the code simpler.
36 radius = 1.0
37 x = radius*math.sin(0)
38 y = radius*math.cos(0)
39 glColor(0.0, 1.0, 0.0)
40 glBegin(GL_LINE_STRIP)
41 for deg in xrange(1000):
42 glVertex(x, y, 0.0)
43 rad = math.radians(deg)
44 radius -= 0.001
45 x = radius*math.sin(rad)
46 y = radius*math.cos(rad)
47 glEnd()
48
49 glEnableClientState(GL_VERTEX_ARRAY)
50
51 spiral_array = []
52
53 # Second Spiral using "array immediate mode" (i.e. Vertex Arrays)
54 radius = 0.8
55 x = radius*math.sin(0)
56 y = radius*math.cos(0)
57 glColor(1.0, 0.0, 0.0)
58 for deg in xrange(820):
59 spiral_array.append([x, y])
60 rad = math.radians(deg)
61 radius -= 0.001
62 x = radius*math.sin(rad)
63 y = radius*math.cos(rad)
64
65 glVertexPointerf(spiral_array)
66 glDrawArrays(GL_LINE_STRIP, 0, len(spiral_array))
67 glFlush()
68
69 def resizeGL(self, w, h):
70 '''
71 Resize the GL window
72 '''
73
74 glViewport(0, 0, w, h)
75 glMatrixMode(GL_PROJECTION)
76 glLoadIdentity()
77 gluPerspective(40.0, 1.0, 1.0, 30.0)
78
79 def initializeGL(self):
80 '''
81 Initialize GL
82 '''
83
84 # set viewing projection
85 glClearColor(0.0, 0.0, 0.0, 1.0)
86 glClearDepth(1.0)
87
88 glMatrixMode(GL_PROJECTION)
89 glLoadIdentity()
90 gluPerspective(40.0, 1.0, 1.0, 30.0)
91
92
93# You don't need anything below this
94class SpiralWidgetDemo(QtGui.QMainWindow):
95 ''' Example class for using SpiralWidget'''
96
97 def __init__(self):
98 QtGui.QMainWindow.__init__(self)
99 widget = SpiralWidget(self)
100 self.setCentralWidget(widget)
101
102if __name__ == '__main__':
103 app = QtGui.QApplication(['Spiral Widget Demo'])
104 window = SpiralWidgetDemo()
105 window.show()
106 app.exec_()

A quick example of using OpenGL with PyQt4. When the script is run it will display two "spirals". The red spiral is drawn in immediate mode (glBegin/glEnd). The green spiral is drawn using array immediate mode (Vertex Arrays).

Note that the glVertex and glColor calls do not require the parameter type specification (2f,3f, etc). Also note that the call specifying the vertex array pointer (line 65) in Python is simpler than the equivalent call in C/C++ as it only requires one argument (not 4), a list of lists. The reason for these simplifications is that the data types and parameter structure can be determined from the list objects being passed to the OpenGL calls.

This is a PyQt port of Using OpenGL with wxPython

When you run the program it should look like this:

http://www.siafoo.net/image/173