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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
#!/ufs/guido/bin/sgi/python
# Simulate the artwork in the hall.
# Jack Jansen, Feb 91.
#
# Please please please don't try to read this code.
# It is the first GL program I ever wrote, and used to do
# very different things before it's current function:-)
from gl import *
from GL import *
from math import *
from DEVICE import *
import sys
import __main__
main_dict = __main__.__dict__
SPOTDIRECTION = 103
SPOTLIGHT = 104
#
# Make a cylinder paralel with the Z axis with center (X,Y,0)
# and radius 1
def mkcyl(nslice, nparts, docircle):
cyl = []
step = 2.0 / float(nslice)
z = -1.0
for i in range(nslice):
cyl.append(mkslice(z, z+step, nparts, docircle))
z = z + step
return drawcylinder(cyl)
#
# Make one part of a cylinder
#
def mkslice(z1, z2, nparts, docircle):
if docircle:
w1 = z1
w2 = z2
w1 = sqrt(1.0-w1*w1)
w2 = sqrt(1.0-w2*w2)
normalz = 1.0
else:
w1 = 1.0
w2 = 1.0
normalz = 0.0
slice = []
step = (2.0*pi)/float(nparts)
angle = 0.0
for i in range(nparts+1):
vx = cos(angle)
vy = sin(angle)
slice.append( ((vx*w1,vy*w1,z1), (vx*w1, vy*w1, z1*normalz)) )
slice.append( ((vx*w2,vy*w2,z2), (vx*w2, vy*w2, z2*normalz)) )
angle = angle + step
return slice
#
# Drawcylinder : draw the cylinder
#
class struct(): pass
curobj = struct()
curobj.curobj = 1
def drawcylinder(cyl):
obj = curobj.curobj
curobj.curobj = curobj.curobj+1
makeobj(obj)
for slice in cyl:
bgntmesh()
vnarray(slice)
endtmesh()
closeobj()
return obj
#
def drawnormals(cyl):
for slice in cyl:
for triang in slice:
bgnline()
v3f(triang[0])
v3f(triang[0][0] + triang[1][0], triang[0][1] + triang[1][1], triang[0][2] + triang[1][2])
endline()
def drawfloors():
obj = curobj.curobj
curobj.curobj = curobj.curobj+1
makeobj(obj)
bgnpolygon()
v3i(4,6,-6)
v3i(-6,6,-6)
v3i(-6,-6,-6)
v3i(4,-6,-6)
endpolygon()
for floor in range(3):
pos = -1 + 5*floor
bgnpolygon()
v3i(4,4,pos)
v3i(-6,4,pos)
v3i(-6,6,pos)
v3i(4,6,pos)
endpolygon()
bgnpolygon()
v3i(-4,4,pos)
v3i(-4,-4,pos)
v3i(-6,-4,pos)
v3i(-6,4,pos)
endpolygon()
bgnpolygon()
v3i(-6,-4,pos)
v3i(-6,-6,pos)
v3i(4,-6,pos)
v3i(4,-4,pos)
endpolygon()
closeobj()
return obj
def drawdoors():
obj = curobj.curobj
curobj.curobj = curobj.curobj+1
makeobj(obj)
for floor in range(3):
pos = -1+5*floor
bgnpolygon()
v3i(-2,6,pos)
v3i(-2,6,pos+3)
v3i(0,6,pos+3)
v3i(0,6,pos)
endpolygon()
closeobj()
return obj
def drawrailing():
obj = curobj.curobj
curobj.curobj = curobj.curobj+1
makeobj(obj)
for floor in range(3):
pos = -1 + 5*floor
bgnpolygon()
v3i(4,4,pos)
v3i(4,4,pos-1)
v3i(-4,4,pos-1)
v3i(-4,4,pos)
endpolygon()
bgnpolygon()
v3i(-4,4,pos)
v3i(-4,4,pos-1)
v3i(-4,-4,pos-1)
v3i(-4,-4,pos)
endpolygon()
bgnpolygon()
v3i(-4,-4,pos)
v3i(-4,-4,pos-1)
v3i(4,-4,pos-1)
v3i(4,-4,pos)
endpolygon()
closeobj()
return obj
def drawwalls():
obj = curobj.curobj
curobj.curobj = curobj.curobj+1
makeobj(obj)
bgnpolygon()
v3i(4,6,-6)
v3i(4,6,18)
v3i(-6,6,18)
v3i(-6,6,-6)
endpolygon()
bgnpolygon()
v3i(-6,6,-6)
v3i(-6,6,18)
v3i(-6,-6,18)
v3i(-6,-6,-6)
endpolygon()
bgnpolygon()
v3i(-6,-6,-6)
v3i(-6,-6,18)
v3i(4,-6,18)
v3i(4,-6,-6)
endpolygon()
bgnpolygon()
v3i(4,-6,-6)
v3i(4,-6,18)
v3i(4,4,18)
v3i(4,4,-6)
endpolygon()
closeobj()
return obj
def axis():
bgnline()
cpack(0xff0000)
v3i(-1,0,0)
v3i(1,0,0)
v3f(1.0, 0.1, 0.1)
endline()
bgnline()
cpack(0xff00)
v3i(0,-1,0)
v3i(0,1,0)
v3f(0.1, 1.0, 0.1)
endline()
bgnline()
cpack(0xff)
v3i(0,0,-1)
v3i(0,0,1)
v3f(0.1,0.1,1.0)
endline()
#
silver = [ DIFFUSE, 0.3, 0.3, 0.3, SPECULAR, 0.9, 0.9, 0.95, \
SHININESS, 40.0, LMNULL]
floormat = [ AMBIENT, 0.5, 0.25, 0.15, DIFFUSE, 0.5, 0.25, 0.15, SPECULAR, 0.6, 0.3, 0.2, SHININESS, 20.0, LMNULL]
wallmat = [ DIFFUSE, 0.4, 0.2, 0.1, AMBIENT, 0.4, 0.20, 0.10, SPECULAR, 0.0, 0.0, 0.0, SHININESS, 20.0, LMNULL]
offwhite = [ DIFFUSE, 0.8, 0.8, 0.6, AMBIENT, 0.8, 0.8, 0.6, SPECULAR, 0.9, 0.9, 0.9, SHININESS, 30.0, LMNULL]
doormat = [ DIFFUSE, 0.1, 0.2, 0.5, AMBIENT, 0.2, 0.4, 1.0, SPECULAR, 0.2, 0.4, 1.0, SHININESS, 60.0, LMNULL]
toplight = [ LCOLOR, 1.0, 1.0, 0.5, \
POSITION, 0.0, 0.0, 11.0, 1.0, LMNULL]
floor1light = [ LCOLOR, 1.0, 1.0, 1.0, POSITION, 3.9, -3.9, 0.0, 1.0, \
SPOTDIRECTION, 1.0, 1.0, 0.0, SPOTLIGHT, 10.0, 90.0, LMNULL]
lmodel = [ AMBIENT, 0.92, 0.8, 0.5, LOCALVIEWER, 1.0, LMNULL]
#
def lighting():
INDIGO=1 # XXXX Seems indigo only has one light.
lmdef(DEFMATERIAL, 2, silver)
lmdef(DEFMATERIAL, 3, floormat)
lmdef(DEFMATERIAL, 4, wallmat)
lmdef(DEFMATERIAL, 5, offwhite)
lmdef(DEFMATERIAL, 6, doormat)
lmdef(DEFLIGHT, 1, toplight)
if not INDIGO:
lmdef(DEFLIGHT, 2, floor1light)
lmdef(DEFLMODEL, 1, lmodel)
lmbind(LIGHT0, 1)
if not INDIGO:
lmbind(LIGHT1, 2)
lmbind(LMODEL, 1)
IdMat=[1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0]
#
wrongrange='Wrong Range'
def defun(axis):
done = 0
res = 0.0 # Hack around exec(...)
while not done:
print 'F'+axis+'(t) = ',
s = sys.stdin.readline(100)
print
try:
s = 'def f'+axis+'(t): return '+s
exec(s, main_dict)
exec('res = f'+axis+'(0.0)\n')
if res < -10.0 or res > 10.0:
raise wrongrange
exec('res = f'+axis+'(100.0)\n')
if res < -10.0 or res > 10.0:
raise wrongrange
done = 1
except RuntimeError:
print 'Sorry, there is a syntax error in your expression'
except TypeError:
print 'Please remember to use floating point numbers'
except wrongrange:
print 'Sorry, function values out of range (non-periodic function?)'
def getfunctions():
print 'Welcome to the CWI art simulator. You can now enter X, Y and Z'
print 'coordinates as a function of t.'
print 'Alternatively, you can specify the name of a python module'
print 'defining functions fx(t), fy(t) and fz(t) on the command line'
print 'Normal trig functions are available. Please use floating point'
print 'values only (so 0.0 for 0). Comments to jack@cwi.nl'
defun('x')
defun('y')
defun('z')
print 'Ok, here you go. Use mouse+right button to move up/down,'
print 'mouse+middle to speed up/slow down time. type ESC to quit simulation'
def main():
if len(sys.argv) > 1:
exec('from '+sys.argv[1]+' import *\n')
else:
getfunctions()
foreground()
prefposition(100,600,100,600)
void = winopen('cyl')
qdevice(ESCKEY)
qdevice(MOUSE1)
qdevice(MOUSE2)
qdevice(PKEY)
RGBmode()
doublebuffer()
gconfig()
zbuffer(1)
mmode(MVIEWING)
perspective(600, 1.0, 0.01, 20.0)
loadmatrix(IdMat)
vx = 0.0
vy = -3.9
vz = 0.0
lookat(0.0, -3.9, 0.0, 0.0, 0.0, 0.0, 0)
lighting()
t = -1.0
step = 0.2
bol = mkcyl(12,24, 1)
cable = mkcyl(1, 6, 0)
floors = drawfloors()
walls = drawwalls()
pillar = mkcyl(1,4,0)
railing = drawrailing()
doors = drawdoors()
shademodel(GOURAUD)
mousing = -1
pausing = 0
while 1:
#
# Check for some user input
#
if qtest():
dev, value = qread()
if dev == PKEY and value == 1:
pausing = 1
if dev == ESCKEY:
break
elif (dev==MOUSE1 or dev==MOUSE2) and value == 1:
if mousing > 0:
vx = 0.0
vy = -3.9
vz = 0.0
mousing = dev
oldx = getvaluator(MOUSEX)
oldy = getvaluator(MOUSEY)
elif (dev==MOUSE1 or dev==MOUSE2):
mousing = -1
if mousing >= 0:
newx = getvaluator(MOUSEX)
newy = getvaluator(MOUSEY)
if newy <> oldy and mousing==MOUSE1:
vz = vz + float(newy - oldy)/100.0
if vz < -5.99:
vz = -5.99
dist = sqrt(vx*vx + vy*vy + vz*vz)
perspective(600, 1.0, 0.01, dist+16.0)
loadmatrix(IdMat)
if vz < 0.0:
lookat(vx, vy, vz, 0.0, 0.0, 0.0, 1800)
else:
lookat(vx, vy, vz, 0.0, 0.0, 0.0, 0)
if newy <> oldy and mousing==MOUSE2:
step = step * exp(float(newy-oldy)/400.0)
if getbutton(CTRLKEY) == 0:
t = t + step
else:
t = t - step
if getbutton(LEFTSHIFTKEY) == 0:
shademodel(GOURAUD)
else:
shademodel(FLAT)
#
# Draw background and axis
czclear(0x802020,getgdesc(GD_ZMAX))
#axis()
#
# draw the floors
#
lmbind(MATERIAL, 3)
callobj(floors)
lmbind(MATERIAL, 4)
callobj(walls)
lmbind(MATERIAL, 5)
pushmatrix()
translate(-4.5,4.5,3.0)
scale(0.2,0.2,9.0)
rotate(450,'z')
callobj(pillar)
popmatrix()
callobj(railing)
lmbind(MATERIAL, 6)
pushmatrix()
translate(0.0, -0.01, 0.0)
callobj(doors)
popmatrix()
#
# Draw object
#
bolx = fx(t)
boly = fy(t)
bolz = fz(t)
err = ''
if bolx < -4.0 or bolx > 4.0:
err = 'X('+`bolx`+') out of range [-4,4]'
if boly < -4.0 or boly > 4.0:
err = 'Y('+`boly`+') out of range [-4,4]'
if bolz < -4.0 or bolz > 8.0:
err = 'Z('+`bolz`+') out of range [-4,8]'
if not err:
pushmatrix()
translate(bolx, boly, bolz)
scale(0.3, 0.3, 0.3)
lmbind(MATERIAL, 2)
blendfunction(BF_ONE, BF_ONE)
callobj(bol)
blendfunction(BF_ONE, BF_ZERO)
popmatrix()
#
# Draw the cables
#
bolz = bolz + 0.3
pushmatrix()
#linesmooth(SML_ON)
bgnline()
v3i(-4,-4,9)
v3f(bolx, boly, bolz)
endline()
bgnline()
v3i(-4,4,9)
v3f(bolx, boly, bolz)
endline()
bgnline()
v3i(4,-4,9)
v3f(bolx, boly, bolz)
endline()
bgnline()
v3i(4,4,9)
v3f(bolx, boly, bolz)
endline()
popmatrix()
if mousing == MOUSE2 or err:
cpack(0xff0000)
cmov(0.0, 0.0, 0.4)
charstr('t='+`t`)
if mousing == MOUSE2:
cpack(0xff0000)
cmov(0.0, 0.0, 0.2)
charstr('delta-t='+`step`)
if err:
cpack(0xff00)
cmov(0.0, 0.0, 0.2)
charstr(err)
pausing = 1
if pausing:
cpack(0xff00)
cmov(0.0, 0.0, 0.0)
charstr('Pausing, type P to continue')
swapbuffers()
if pausing:
while 1:
dv=qread()
if dv==(PKEY,1):
break
if dv==(ESCKEY,1):
sys.exit(0)
pausing = 0
#
try:
main()
except KeyboardInterrupt:
sys.exit(1)
|