forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTrails.py
More file actions
300 lines (255 loc) · 6.19 KB
/
Copy pathPyTrails.py
File metadata and controls
300 lines (255 loc) · 6.19 KB
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
#PyLorenz.py
#This program illustrates the Lorenz attractor
#Feb 24, 2005
#import important GL stuff
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
#define some globals
global aff
global wd
global ht
global MouseX
global MouseY
#Lorenz globals
global dt
global a
global b
global c
global numspheres
global vertx
global colors
global trails
global trailflag
global flag
#For lighting
global whiteLight
global sourceLight
global lightPos
global attract
dt = .004
numspheres = 3000
trailflag = -1
flag = 1
attract = 1
vertx = []
colors = []
trails = []
whiteLight = (0.5, 0.5, 0.5, 1.0)
sourceLight = (0.3, 0.3, 0.3, 1.0)
lightPos = ( 20.0, 20.0, 20.0, 1.0 )
#define the affine identity matrix
#for mouse motion
aff = (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)
#initial window and mouse settings
wd = 800
ht = 800
MouseX = wd/2
MouseY = ht/2
#The usual display routine
def display():
global trails
global trailflag
global flag
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
#glPushMatrix()
glLoadIdentity()
#glTranslatef(0,0,-60)
# Uncomment the next line for mouse motion
#glMultMatrixf(aff)
#draw the Lorenz attractor
for n in range(0,numspheres/3):
glColor3ub(colors[n][0],colors[n][1],colors[n][2])
if attract == 1:
a = (10*vertx[n][1] - 10*vertx[n][0])*dt + vertx[n][0]
b = (28*vertx[n][0] - vertx[n][1] - vertx[n][0]*vertx[n][2])*dt + vertx[n][1]
c = (-2.66667*vertx[n][2] + vertx[n][0]*vertx[n][1])*dt + vertx[n][2]
else:
a = vertx[n][0] - (vertx[n][1] + vertx[n][2])*dt
b = vertx[n][1] + (vertx[n][0] + 0.2*vertx[n][1])*dt
c = vertx[n][2] + (vertx[n][0]*vertx[n][2] - 6.7*vertx[n][2])*dt
vertx[n] = [a,b,c]
#glRotatef(15, 0,0,1)
glPushMatrix()
glTranslatef(a,b,c-60)
glutSolidSphere(1.0,10,10)
glPopMatrix()
trails = trails + [[a,b,c]]
if trailflag == -1:
for n in range(2,flag):
trails[n] = trails[n-1]
if trailflag == 1:
for n in range(flag-1,2,-1):
trails[n] = trails[n-1]
glBegin(GL_POINTS)
for n in range(0,flag):
glColor3ub(100,100,100)
glVertex3f(trails[n][0],trails[n][1], trails[n][2] - 60)
glEnd()
#glPopMatrix()
flag +=1
if flag > 599:
flag = 599
glutSwapBuffers()
#keyboard stuff
def keyboard(key, x, y):
global trailflag
global attract
if key == chr(27) or key == 'q':
sys.exit(0)
if key == "t":
trailflag = -1*trailflag
if key == "r":
attract = -1*attract
glutPostRedisplay()
#if we change the screen dimensions
def reshape(width, height):
global wd
global ht
glClearColor(0.0, 0.0, 0.0, 0.0)
if height == 0:
height = 1
wd = width
ht = height
glViewport(0,0,wd,ht)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.,1.,1.,180.)
gluLookAt(0,0,40,
0,0,0,
0,1,0)
#if wd<=ht:
# glOrtho(-2.0,2.0,-2.0*ht/wd,2.0*ht/wd,-2.0,2.0)
#else:
# glOrtho(-2.0*wd/ht,2.0*wd/ht,-2.0,2.0,-2.0,2.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glLightfv(GL_LIGHT0, GL_POSITION, lightPos)
#does nothing at this point
def motion(x, y):
return 0
#chaptrack, python style.
#Note that we must declare the globals again
def chaptrack():
global MouseX
global MouseY
global wd
global ht
global aff
dx = (MouseX-wd/2)/96
dy = (MouseY-ht/2)/96
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glRotatef(dx,0,1.0,0.0)
glRotatef(dy,1.0,0.0,0.0)
glMultMatrixf(aff)
#this line is different from the C
#version. Python handles it a bit
#differently... was a pain to figure out!
aff = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
#traditional idle
def idle():
chaptrack()
glutPostRedisplay()
#ditto traditional mousemotion
#Note globals
def mousemotion(x,y):
global MouseX
global MouseY
MouseX = x
MouseY = y
def setup():
#setup the spheres
global vertx
global colors
# z position of first sphere
zpos = 23.990
#Places the spheres NEARLY on top of each other
for n in range(0,numspheres):
if (n+1)%3==0:
zpos+=.0001
vertx = vertx + [[8.0,8.0,zpos]]
#Give the spheres a range of colors
for n in range(0,numspheres,3):
if n <= numspheres/10.:
red = 255
green = 0
blue = 0
elif n > numspheres/10. and n <= numspheres/5.:
red = 255
green = 128
blue = 0
elif n > numspheres/5. and n <= numspheres/3.3333:
red = 255
green = 255
blue = 0
elif n > numspheres/3.3333 and n <= numspheres/2.5:
red = 128
green = 255
blue = 0
elif n > numspheres/2.5 and n <= numspheres/2.:
red = 0
green = 255
blue =0
elif n > numspheres/2. and n <= numspheres/1.66667:
red = 0
green = 255
blue = 128
elif n > numspheres/1.66667 and n <= numspheres/1.428571:
red = 0
green = 128
blue = 255
elif n > numspheres/1.428571 and n <= numspheres/1.25:
red = 0
green = 0
blue = 255
elif n > numspheres/1.25 and n <= numspheres/1.11111:
red = 128
green = 0
blue = 255
else:
red = 255
green = 0
blue = 255
colors = colors + [(red,green,blue)]
#setup for lighting... doesn't work well :-(
glShadeModel(GL_SMOOTH)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight)
glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
#Traditional main subroutine
def main(argv) :
global wd
global ht
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE)
glutInitWindowPosition(50, 50)
glutInitWindowSize(wd, ht)
glutInit(argv)
glutCreateWindow("Lorenz Attractor")
glutKeyboardFunc(keyboard)
glutReshapeFunc(reshape)
glutDisplayFunc(display)
glutMotionFunc(motion)
##glutMouseFunc(mouse)
glutIdleFunc(idle)
glutPassiveMotionFunc(mousemotion)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#Could add additional function calls
##init()
##lists()
setup()
glutMainLoop()
#Necessary if we want to this program to run
main(sys.argv)