-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultitexcubes.py
More file actions
152 lines (129 loc) · 4.08 KB
/
multitexcubes.py
File metadata and controls
152 lines (129 loc) · 4.08 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
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_QUADS)
f2old = 0
for i in range(len(faces)):
r,g,b = facecolors[i]
face = faces[i]
f1,f2,f3 = face
if (i % 2) :
glTexCoord2f(0.0, 1.0)
glVertex3fv(verticies[f2])
glTexCoord2f(1.0, 1.0)
glVertex3fv(verticies[f2old])
glTexCoord2f(1.0, 0.0)
glVertex3fv(verticies[f1])
glTexCoord2f(0.0, 0.0)
glVertex3fv(verticies[f3])
f2old = f2
glEnd()
def loadTexture():
textureSurface = pygame.image.load('crate.bmp')
textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
width = textureSurface.get_width()
height = textureSurface.get_height()
glEnable(GL_TEXTURE_2D)
texid = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
return texid
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)
cubes = []
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)
loadTexture()
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 game():
global cubes
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()
game()
render()