-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticubes.py
More file actions
124 lines (107 loc) · 3.07 KB
/
multicubes.py
File metadata and controls
124 lines (107 loc) · 3.07 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
# bounce 15 cubes around in 3d
import pygame, random
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = [
[ 1, 1, 1],
[-1, 1, 1],
[-1, -1, 1],
[ 1, -1, 1],
[ 1, 1, -1],
[-1, 1, -1],
[-1, -1, -1],
[ 1, -1, -1]]
# cube faces
# (triangles)
faces = [
[0, 1, 2],
[0, 2, 3],
[4, 0, 3],
[4, 3, 7],
[5, 4, 7],
[5, 7, 6],
[1, 5, 6],
[1, 6, 2],
[4, 5, 1],
[4, 1, 0],
[2, 6, 7],
[2, 7, 3]]
facecolors = [
[15,0,0], # red
[15,0,0],
[0,15,0], # green
[0,15,0],
[0,0,15], # blue
[0,0,15],
[15,15,0], # yellow
[15,15,0],
[0,15,15], # cyan
[0,15,15],
[15,0,15], # magenta
[15,0,15]
]
class shapeobj:
x = y = z = 0
dx = dy = dz = 0
angle = spin = 0
def drawCube():
glBegin(GL_TRIANGLES)
for i in range(len(faces)):
r,g,b = facecolors[i]
glColor3f(r,g,b)
face = faces[i]
for vertex in face:
glVertex3fv(verticies[vertex])
glEnd()
def init():
global cubes
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glMatrixMode(GL_PROJECTION) # operate on projection (world)
glLoadIdentity()
gluPerspective(50.0, display[0]/display[1], 0.1, 70.0)
glEnable(GL_CULL_FACE) # doesn't need normals if winding order is correct
glEnable(GL_DEPTH_TEST) # so we can't see through objects
cubes = []
# make some cubes with random spin and movement
for i in range(15) :
c = shapeobj()
c.x = random.uniform(-5,5)
c.y = random.uniform(-5,5)
c.z = random.uniform(-50,-6)
c.spin = random.uniform(-1.0,1.0)
c.dx = random.uniform(-0.01,0.01)
c.dy = random.uniform(-0.01,0.01)
c.dz = random.uniform(-0.1,0.1)
cubes.append(c)
def render():
global cubes
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW) # operate on model
for c in cubes :
glLoadIdentity()
glTranslatef(c.x, c.y, c.z)
glRotatef(c.angle, 1.0, 1.0, 0.0)
drawCube()
pygame.display.flip()
def gameloop():
global cubes
# bounce some cubes around
for c in cubes:
c.angle += c.spin
c.x += c.dx
if abs(c.x / c.z) > 1 : c.dx *= -1
c.y += c.dy
if abs(c.y / c.z) > 1 : c.dy *= -1
c.z += c.dz
if c.z < -70 or c.z > 0 : c.dz *= -1
init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameloop()
render()