forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyTeapots.py
380 lines (293 loc) · 8.93 KB
/
PyTeapots.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# teapots.py
# by Allen Downey
# The following program demonstrates features from the
# first few chapters of the Red Book. I have lifted
# lines from lots of different examples, with the intention
# of getting it all in one place.
# The documentation for PyOpenGL, GLU and GLUT is at:
#http://pyopengl.sourceforge.net/documentation/manual/reference-GL.xml
#http://pyopengl.sourceforge.net/documentation/manual/reference-GLU.xml
#http://pyopengl.sourceforge.net/documentation/manual/reference-GLUT.xml
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy import *
import sys
from time import sleep
class Object:
def step(self): pass
class Sphere(Object):
def __init__(self, world, size=1, rows=32, cols=32):
self.size = size
self.rows = rows
self.cols = cols
world.add_opaque(self)
def display(self):
glutSolidSphere(self.size, self.rows, self.cols)
class Teapot(Object):
def __init__(self, world, size=1, orbit=5, orbit_angle=0, rot_angle=0,
rev_speed=1, rot_speed=2):
self.size = size
self.orbit = orbit
self.orbit_angle = orbit_angle
self.rot_angle = rot_angle
self.rev_speed = rev_speed
self.rot_speed = rot_speed
self.rotation = 0
self.revolution = 0
world.add_opaque(self)
def step(self):
# update the state of the object
self.rotation = (self.rotation + self.rot_speed) % 360
self.revolution = (self.revolution + self.rev_speed) % 360
def display(self):
# precondition: matrix mode is modelview
# invariant: restores the current matrix
glPushMatrix()
axis = (0, 0, 1)
rev_axis=(0, 1, 0)
rot_axis=(0, 1, 0)
glRotate(self.orbit_angle, *axis)
glRotate(self.revolution, *rev_axis)
glTranslate(self.orbit, 0, 0)
glRotate(self.rot_angle, *axis)
glRotate(self.rotation, *rot_axis)
glutSolidTeapot(self.size)
self.frame = get_frame()
glPopMatrix()
class Triangle(Object):
def __init__(self, world):
world.add_transparent(self)
def display(self):
# most basic use of Begin, End and Vertex to
# draw a polygon
glBegin(GL_TRIANGLES)
glColor(1.0, 1.0, 1.0)
glVertex(0, 0, 0)
glVertex(5, 5, 0)
glVertex(-5, 5, 0)
glEnd()
class Orbit(Object):
def __init__(self, world, orbit=5, orbit_angle=0, n=40):
self.orbit = orbit
self.orbit_angle = orbit_angle
self.n = n
world.add_transparent(self)
def display(self):
# precondition: matrix mode is modelview
# invariant: restores the current matrix
glPushMatrix()
axis = (0, 0, 1)
glRotate(self.orbit_angle, *axis)
vertices = self.compute_vertices()
glBegin(GL_TRIANGLE_FAN)
for vertex in vertices:
glColor(0, 0, 1, 0.1)
glVertex(vertex)
glEnd()
glPopMatrix()
def compute_vertices(self):
glPushMatrix()
axis = (0, 1, 0)
vertices = [(0, 0, 0)]
for i in range(self.n+1):
glLoadIdentity()
angle = i * 360.0/self.n
glRotate(angle, *axis)
glTranslate(0, 0, self.orbit)
vertices.append(whereami())
glPopMatrix()
return vertices
def whereami():
# get the translation part of the model-view matrix
frame = glGetFloat(GL_MODELVIEW_MATRIX)
return frame[3]
def get_frame():
# get the model-view matrix
return glGetFloat(GL_MODELVIEW_MATRIX)
def print_frame(frame):
print transpose(array(frame))
def print_current_frame():
print_frame(get_frame())
class Vlist(list):
def __init__(self, type=GL_TRIANGLE_FAN):
self.type = type
world.add_opaque(self)
def display(self):
glBegin(GL_TRIANGLE_FAN)
for vertex in self:
glVertex(vertex)
glEnd()
class World:
def __init__(self, args, size=(500, 500), pos=(100, 100), bg=(1, 1, 1, 0)):
self.args = args
self.size = size
self.pos = pos
self.bg = bg
self.opaques = []
self.transparents = []
self.fog = Fog()
self.set_up_gl()
self.camera = Camera(position=(0, 3, 10))
self.lights = []
self.add_lights()
def add_opaque(self, object):
self.opaques.append(object)
def add_transparent(self, object):
self.transparents.append(object)
def set_up_gl(self):
# process command-line arguments
glutInit(self.args)
# turn on double-buffering and rgb color
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
# create the window
glutInitWindowSize(*self.size)
glutInitWindowPosition(*self.pos)
glutCreateWindow(self.args[0])
# clear the background
glClearColor(*self.bg)
glShadeModel(GL_SMOOTH)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
# glLightModel(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
# glLightModel(GL_LIGHT_MODEL_TWO_SIDE, 1)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# set up the callbacks
glutDisplayFunc(self.display)
glutReshapeFunc(self.reshape)
glutKeyboardFunc(self.keyboard)
glutMouseFunc(self.mouse)
glutIdleFunc(self.idle)
def add_lights(self):
# put a red light on the camera
position = self.camera.position + (0,)
color = (1, 0, 0, 0)
light = Light(GL_LIGHT0, position, color)
self.lights.append(light)
# put a blue light on the right
position = (10, 0, 0, 0)
color = (0, 0, 1, 0)
light = Light(GL_LIGHT1, position, color)
self.lights.append(light)
# put a green light at high noon
position = (0, 10, 0, 0)
color = (0, 1, 0, 0)
light = Light(GL_LIGHT2, position, color)
self.lights.append(light)
def display(self):
# precondition: matrix mode is modelview
# invariant: restores the current matrix
glMaterial(GL_FRONT, GL_SPECULAR, (1.0, 1.0, 1.0, 0.15))
glMaterial(GL_FRONT, GL_SHININESS, (100.0, ))
# clear out the colors and the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# you have to draw the fog every time
if hasattr(self, 'fog'):
self.fog.setup()
# display all the objects
glMaterial(GL_FRONT, GL_EMISSION, ( 0.0, 0.0, 0.0, 1.0))
glMaterial(GL_FRONT, GL_DIFFUSE, (0.75, 0.75, 0.0, 1.0))
for object in self.opaques:
try:
object.display()
except:
(type, value, traceback) = sys.exc_info()
print type, value
sys.exit()
glMaterial(GL_FRONT, GL_EMISSION, ( 0.0, 0.3, 0.3, 0.6))
glMaterial(GL_FRONT, GL_DIFFUSE, ( 0.0, 0.8, 0.8, 0.8))
glDepthMask(GL_FALSE)
for object in self.transparents:
object.display()
glDepthMask(GL_TRUE)
# reveal the finished picture
glutSwapBuffers()
def reshape (self, w, h):
glViewport (0, 0, w, h)
self.camera.point()
def keyboard(self, key, x, y):
# quit if the user presses Control-C
if key == chr(3):
sys.exit(0)
def mouse(self, button, state, x, y):
if button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
glutIdleFunc(None)
elif state == GLUT_UP:
glutIdleFunc(self.idle)
def idle(self):
for object in self.opaques:
object.step()
for object in self.transparents:
object.step()
# mark the scene for redisplay during the next iteration
# of mainLoop
glutPostRedisplay()
sleep(0.005)
def mainloop(self):
glutMainLoop()
class Camera:
def __init__(self, position=(0, 0, 10), target=(0, 0, 0), up=(0, 1, 0),
fovy=60, aspect=1, near=2, far=20):
self.position = position
self.target = target
self.up = up
self.fovy = fovy
self.aspect = aspect
self.near = near
self.far = far
self.point()
def point(self):
# postcondition: matrix mode is modelview
# set up the projection matrix
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.fovy, self.aspect, self.near, self.far)
# set up the modelview matrix
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(*self.position + self.target + self.up)
class Fog:
def __init__(self, color=(1, 1, 1, 1), mode=GL_LINEAR,
density=0.1, hint=GL_DONT_CARE, start=2, end=20):
self.color = color
self.mode = mode
self.density = density
self.hint = hint
self.start = start
self.end = end
self.setup()
def setup(self):
glEnable(GL_FOG)
glFog(GL_FOG_MODE, self.mode)
glFog(GL_FOG_COLOR, self.color)
glFog(GL_FOG_DENSITY, self.density)
glFog(GL_FOG_START, self.start)
glFog(GL_FOG_END, self.end)
glHint(GL_FOG_HINT, self.hint)
class Light:
def __init__(self, name, position=(0, 0, 1, 0), color=(1, 1, 1, 1)):
self.name = name
self.position = position
self.acolor = color
self.dcolor = color
self.scolor = color
self.setup()
def setup(self):
glLightfv(self.name, GL_AMBIENT, self.acolor)
glLightfv(self.name, GL_DIFFUSE, self.dcolor)
glLightfv(self.name, GL_SPECULAR, self.scolor)
glLightfv(self.name, GL_POSITION, self.position)
glEnable(self.name)
def main(*args):
world = World(args)
sphere = Sphere(world, 2)
for i in range(4):
teapot = Teapot(world, orbit = 7.2, orbit_angle = i*90)
teapot.revolution = i*90
plane = Orbit(world, orbit = 7.2)
tri = Triangle(world)
world.mainloop()
if __name__ == "__main__":
main(*sys.argv)