-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
555 lines (425 loc) · 14.1 KB
/
base.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from PIL import Image
from math import sin, cos, degrees, radians, sqrt, pow, atan2
from functools import wraps
def push_pop(f):
@wraps(f)
def _wrapper(*args, **kwargs):
glPushMatrix()
result = f(*args, **kwargs)
glPopMatrix()
return result
return _wrapper
# print(GL_VERSION)
# print(GL_MINOR_VERSION)
# print(GL_MAJOR_VERSION)
width = 640
height = 480
primary = None
primary_radius = 1.5
primary_rot_angle = 0
primary_rot_period_ms = 4000
secondary = None
secondary_rot_angle = 0
secondary_rev_angle = 90
secondary_rot_period_ms = 4000
secondary_rev_period_ms = 8000
sky_dome = None
tex_primary_planet = None
tex_secondary_planet = None
tex_background = None
tex_tree = None
tex_rose = None
tex_little_prince = None
tex_primary_planet_path = "./img/mars.jpg"
tex_secondary_planet_path = "./img/mercury.jpg"
tex_background_path = "./img/space.jpg"
tex_tree_path = "./img/tree.png"
tex_baobab_path = "./img/baobab.png"
tex_rose_path = "./img/rose.png"
tex_little_prince_path = "./img/little_prince.png"
eye_x = 0.
eye_y = 0.
eye_z = 0.
eye_rho = 10.
eye_phi = 180.
eye_theta = 90.
def cart2sphe(x, y, z):
rho_xy = sqrt(pow(x, 2)+pow(y, 2))
rho = sqrt(pow(rho_xy, 2)+pow(z, 2))
theta = atan2(z, rho_xy)
phi = atan2(y, x)
return rho, degrees(phi), degrees(theta)
def sphe2cart(rho, phi, theta):
x = rho*sin(radians(theta))*cos(radians(phi))
y = rho*sin(radians(theta))*sin(radians(phi))
z = rho*cos(radians(theta))
return x, y, z
def move_forward():
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
dx, dy, dz = sphe2cart(1., eye_phi, eye_theta)
eye_x += dy
eye_y += dz
eye_z += dx
def move_backwards():
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
dx, dy, dz = sphe2cart(1., eye_phi, eye_theta)
eye_x -= dy
eye_y -= dz
eye_z -= dx
def move_left():
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
dx, dy, dz, = sphe2cart(1., eye_phi+90, eye_theta)
eye_x += dy
eye_y += dz
eye_z += dx
def move_right():
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
dx, dy, dz, = sphe2cart(1., eye_phi+90, eye_theta)
eye_x -= dy
eye_y -= dz
eye_z -= dx
def get_look_at_args():
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
x, y, z = sphe2cart(eye_rho, eye_phi, eye_theta)
target_x = eye_x + y
target_y = eye_y + z
target_z = eye_z + x
# up_z, up_x, up_y = sphe2cart(1, 0, eye_theta-90)
up_x = 0.
up_y = 1.
up_z = 0.
return eye_x, eye_y, eye_z, target_x, target_y, target_z, up_x, up_y, up_z
def load_texture(file_name, channels="RGBX"):
image = Image.open(file_name)
image_bytes = image.tobytes("raw", channels, 0, -1)
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
# glPixelStorei(GL_UNPACK_ALIGNMENT,1)
# http://pyopengl.sourceforge.net/documentation/manual-3.0/glTexImage2D.html
# 0 ... specifies the level-of-detail number
# 3 ... n of color
# w ... width
# h ... height
# 0 ... border ... This value must be 0
# image ... Specifies a pointer to the image data in memory
glTexImage2D(
GL_TEXTURE_2D,
0, GL_RGBA, image.size[0], image.size[1], 0,
GL_RGBA, GL_UNSIGNED_BYTE, image_bytes
)
# set the texture's stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
return texture_id
def load_texture_x(file_name):
return load_texture(file_name, channels="RGBA")
def print_light_info(light_name):
print(light_name.__repr__().split(" ")[0])
print("\t->", GL_AMBIENT.__repr__().split(" ")[0], glGetLightfv(GL_LIGHT0, GL_AMBIENT))
print("\t->", GL_DIFFUSE.__repr__().split(" ")[0], glGetLightfv(light_name, GL_DIFFUSE))
print("\t->", GL_SPECULAR.__repr__().split(" ")[0], glGetLightfv(light_name, GL_SPECULAR))
print("\t->", GL_POSITION.__repr__().split(" ")[0], glGetLightfv(light_name, GL_POSITION))
print("\t->", GL_SPOT_DIRECTION.__repr__().split(" ")[0], glGetLightfv(light_name, GL_SPOT_DIRECTION))
print("\t->", GL_SPOT_EXPONENT.__repr__().split(" ")[0], glGetLightfv(light_name, GL_SPOT_EXPONENT))
print("\t->", GL_CONSTANT_ATTENUATION.__repr__().split(" ")[0], glGetLightfv(light_name, GL_CONSTANT_ATTENUATION))
print("\t->", GL_LINEAR_ATTENUATION.__repr__().split(" ")[0], glGetLightfv(light_name, GL_LINEAR_ATTENUATION))
print("\t->", GL_QUADRATIC_ATTENUATION.__repr__().split(" ")[0], glGetLightfv(light_name, GL_QUADRATIC_ATTENUATION))
def init():
global primary, secondary
global tex_primary_planet, tex_secondary_planet, tex_tree, tex_background, tex_rose, tex_little_prince
global sky_dome
glClearColor(0.0, 0.0, 0.0, 1.0)
glShadeModel(GL_SMOOTH)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0) # Enable light #1
# glEnable(GL_LIGHT1) # Enable light #1
# glLightfv(GL_LIGHT0, GL_POSITION, [-50.0, 0.0, +50.0])
# glLightfv(GL_LIGHT1, GL_POSITION, [-50.0, 0.0, +50.0])
# glLightfv(GL_LIGHT1, GL_AMBIENT, [1., 1., 1., 1.])
# reference for glLightModelfv parameters
# https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLightModel.xml
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1])
# GL_LIGHT_MODEL_AMBIENT
# params contains four integer or floating-point values that specify the ambient RGBA intensity
# of the entire scene.
# Integer values are mapped linearly such that the most positive representable value maps to 1.0,
# and the most negative representable value maps to -1.0 . Floating-point values are mapped directly.
# Neither integer nor floating-point values are clamped.
# The initial ambient scene intensity is (0.2, 0.2, 0.2, 1.0).
# ....
# print_light_info(GL_LIGHT0)
# print_light_info(GL_LIGHT1)
glEnable(GL_NORMALIZE)
tex_primary_planet = load_texture(tex_primary_planet_path)
tex_secondary_planet = load_texture(tex_secondary_planet_path)
tex_tree = load_texture_x(tex_baobab_path)
tex_background = load_texture(tex_background_path)
tex_rose = load_texture_x(tex_rose_path)
tex_little_prince = load_texture_x(tex_little_prince_path)
primary = gluNewQuadric()
gluQuadricNormals(primary, GLU_SMOOTH)
gluQuadricTexture(primary, GL_TRUE)
gluQuadricDrawStyle(primary, GLU_FILL)
gluQuadricOrientation(primary, GLU_OUTSIDE)
secondary = gluNewQuadric()
gluQuadricNormals(secondary, GLU_SMOOTH)
gluQuadricTexture(secondary, GL_TRUE)
gluQuadricDrawStyle(secondary, GLU_FILL)
gluQuadricOrientation(secondary, GLU_OUTSIDE)
sky_dome = gluNewQuadric()
gluQuadricNormals(sky_dome, GLU_SMOOTH)
gluQuadricTexture(sky_dome, GL_TRUE)
gluQuadricDrawStyle(sky_dome, GLU_FILL)
gluQuadricOrientation(sky_dome, GLU_INSIDE)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
@push_pop
def texture_on_plane(tex_id, sx, sy, sz, alpha):
# glPushMatrix()
glScale(sx, sy, sz)
if alpha:
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_ALPHA_TEST)
glAlphaFunc(GL_GREATER, 0.0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_id)
glBegin(GL_QUADS)
glTexCoord2f(0., 0.)
glVertex3f(-0.5, 0., 0.)
glTexCoord2f(0., 1.)
glVertex3f(-0.5, 0., -1.)
glTexCoord2f(1., 1.)
glVertex3f(+0.5, 0., -1.)
glTexCoord2f(1., 0.)
glVertex3f(+0.5, 0., 0.)
glEnd()
if alpha:
glDisable(GL_ALPHA_TEST)
glDisable(GL_BLEND)
# glPopMatrix()
@push_pop
def draw_tree():
# glPushMatrix()
glRotatef(100, 0., 1., 0.)
glRotatef(60, 1., 0., 0.)
glTranslatef(0., 0., -primary_radius*9/10)
texture_on_plane(tex_tree, 3., 3., 3., True)
glRotatef(90, 0., 0., 1.)
texture_on_plane(tex_tree, 3., 3., 3., True)
# glPopMatrix()
@push_pop
def draw_rose():
# glPushMatrix()
glRotatef(-100, 0., 1., 0.)
glRotatef(-60, 1., 0., 0.)
glTranslatef(0., 0., -primary_radius*9/10)
texture_on_plane(tex_rose, 0.75, 0.75, 0.75, True)
# glPopMatrix()
@push_pop
def draw_little_prince_and_fox():
# glPushMatrix()
glRotatef(-70, 0., 1., 0.)
glRotatef(-30, 1., 0., 0.)
glTranslatef(0., 0., -primary_radius*9/10)
texture_on_plane(tex_little_prince, 0.75, 0.75, 0.75, True)
# glPopMatrix()
@push_pop
def draw_background():
global sky_dome, tex_background
# glPushMatrix()
glRotatef(90., 1., 0., 0.)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_background)
gluSphere(sky_dome, 200, 512, 512)
glDisable(GL_TEXTURE_2D)
# glPopMatrix()
def display():
global primary_rot_angle, secondary_rot_angle, secondary_rev_angle
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(
*get_look_at_args()
)
draw_background()
glPushMatrix()
glTranslatef(0., 50, -50.)
glColor3f(1, 0, 0)
glPopMatrix()
glPushMatrix()
glTranslatef(0.0, 0.0, -12.)
glPushMatrix()
glRotatef(90., 1., 0., 0.)
glPushMatrix()
glRotatef(-20., 1., 0., 0.)
glRotatef(-10., 0., 1., 0.)
glRotatef(primary_rot_angle, 0.0, 0., 1.0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_primary_planet)
gluSphere(primary, primary_radius, 32, 32)
glDisable(GL_TEXTURE_2D)
draw_tree()
draw_rose()
draw_little_prince_and_fox()
glPopMatrix()
glPopMatrix()
glRotatef(secondary_rev_angle, 0., 1., 0.)
glTranslatef(0.0, 0.0, -6.)
glRotatef(90., 1., 0., 0.)
glRotatef(secondary_rot_angle, 0.0, 0., 1.0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_secondary_planet)
gluSphere(secondary, 0.5, 32, 32)
glDisable(GL_TEXTURE_2D)
glPopMatrix()
glFlush()
def keyboard(key, x, y):
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
if key == b'\x1b':
# print("key ESC: exit")
sys.exit()
if key == b'w':
move_forward()
glutPostRedisplay()
return
if key == b's':
move_backwards()
glutPostRedisplay()
return
if key == b'a':
move_left()
glutPostRedisplay()
return
if key == b'd':
move_right()
glutPostRedisplay()
return
if key == b'0':
eye_x = 0.
eye_y = 0.
eye_z = 0.
eye_rho = 10.
eye_phi = 180.
eye_theta = 90.
glutPostRedisplay()
return
def special_keyboard(key, x, y):
global eye_x, eye_y, eye_z
global eye_rho, eye_phi, eye_theta
if key == GLUT_KEY_LEFT:
eye_phi += 1
glutPostRedisplay()
return
if key == GLUT_KEY_RIGHT:
eye_phi -= 1
glutPostRedisplay()
return
if key == GLUT_KEY_DOWN:
if eye_theta > 90+45:
return
eye_theta += 1
glutPostRedisplay()
return
if key == GLUT_KEY_UP:
if eye_theta < 90-45:
return
eye_theta -= 1
glutPostRedisplay()
return
def reshape(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60, w / float(h), 1.0, 402.0)
def update_scene(val):
global primary_rot_angle, primary_rot_period_ms
global secondary_rot_angle, secondary_rev_angle, secondary_rot_period_ms, secondary_rev_period_ms
primary_rot_angle += float(val)*360/primary_rot_period_ms
secondary_rot_angle += float(val)*360/secondary_rot_period_ms
secondary_rev_angle += float(val)*360/secondary_rev_period_ms
glutTimerFunc(val, update_scene, val)
glutPostRedisplay()
return
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height)
glutInitWindowPosition(0, 0)
glutCreateWindow("Lil")
init()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glutSpecialFunc(special_keyboard)
# glutIdleFunc(display)
glutTimerFunc(10, update_scene, 10)
glutMainLoop()
#########################################################################
# arrow_x_q = {"cyl": None, "con": None}
# arrow_y_q = {"cyl": None, "con": None}
# arrow_z_q = {"cyl": None, "con": None}
# arrow_x_q["cyl"] = gluNewQuadric()
# gluQuadricNormals(arrow_x_q["cyl"], GLU_SMOOTH)
# gluQuadricTexture(arrow_x_q["cyl"], GL_TRUE)
#
# arrow_y_q["cyl"] = gluNewQuadric()
# gluQuadricNormals(arrow_y_q["cyl"], GLU_SMOOTH)
# gluQuadricTexture(arrow_y_q["cyl"], GL_TRUE)
#
# arrow_z_q["cyl"] = gluNewQuadric()
# gluQuadricNormals(arrow_z_q["cyl"], GLU_SMOOTH)
# gluQuadricTexture(arrow_z_q["cyl"], GL_TRUE)
# def draw_axis_3d():
#
# global arrow_x_q, arrow_y_q, arrow_z_q
#
# glPushMatrix()
# # glTranslatef(eye_x, eye_y, eye_z)
# # glRotatef(eye_phi-180, 0., 1., 0.)
# # glRotatef(eye_theta-90, -1., 0., 0.)
# # glTranslatef(3.3, -2.5, 0)
# #
# # glDisable(GL_LIGHTING)
# # # three arrows are colored using glColor
# # # to do this light mus be disabled
# # # in this way arrows have theirs own color
# # # and doesn't have any effects on the rest of the scene
# #
# # glPushMatrix()
# # glTranslatef(0., 0., -5.)
# # glRotatef(90, 0., 1., 0.)
# # glColor3f(1., 0., 0.)
# # gluCylinder(arrow_x_q["cyl"], 0.1, 0.1, 0.3, 32, 32)
# # glTranslatef(0., 0., 0.3)
# # glutSolidCone(0.2, 0.5, 32, 32)
# # glPopMatrix()
# #
# # glPushMatrix()
# # glTranslatef(0., 0., -5.)
# # glRotatef(-90, 1., 0., 0.)
# # glColor3f(0., 1., 0.)
# # gluCylinder(arrow_y_q["cyl"], 0.1, 0.1, 0.3, 32, 32)
# # glTranslatef(0., 0., 0.3)
# # glutSolidCone(0.2, 0.5, 32, 32)
# # glPopMatrix()
# #
# # glPushMatrix()
# # glTranslatef(0., 0., -5.)
# # glColor3f(0., 0., 1.)
# # gluCylinder(arrow_z_q["cyl"], 0.1, 0.1, 0.3, 32, 32)
# # glTranslatef(0., 0., 0.3)
# # glutSolidCone(0.2, 0.5, 32, 32)
# # glPopMatrix()
# #
# # glEnable(GL_LIGHTING)
#
# glPopMatrix()