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