-
Notifications
You must be signed in to change notification settings - Fork 2
/
futcubes.py
executable file
·256 lines (219 loc) · 9.73 KB
/
futcubes.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
#!/usr/bin/env python3
import sys
import os.path
import math
import random
import argparse
import time
import pygame
import futracer
class FutCubes:
def __init__(self, size=None, n_cubes=None, just_colors=False,
render_approach=None):
if size is None:
size = (800, 600)
self.size = size
if n_cubes is None:
n_cubes = 2000
self.n_cubes = n_cubes
self.just_colors = just_colors
if render_approach is None:
render_approach = 'segmented'
self.render_approach = render_approach
self.view_dist = 600.0
self.draw_dist = 8000.0
if render_approach != 'segmented':
self.draw_dist /= 10 # The others are really that much slower.
self.n_draw_rects = [1, 1]
def run(self):
# Setup pygame.
pygame.init()
pygame.display.set_caption('futcubes')
self.screen = pygame.display.set_mode(self.size)
self.surface = pygame.Surface(self.size, depth=32)
self.font = pygame.font.Font(None, 36)
self.clock = pygame.time.Clock()
# Load the library.
self.racer = futracer.FutRacer()
# Actually run!
return self.loop()
def message(self, what, where):
text = self.font.render(what, 1, (255, 255, 255))
self.screen.blit(text, where)
def random_cubes(self, square_texture=None):
t0 = [(200.0, 100.0, 200.0),
(200.0, 300.0, 200.0),
(400.0, 100.0, 200.0)]
t1 = [(400.0, 300.0, 200.0),
(400.0, 100.0, 200.0),
(200.0, 300.0, 200.0)]
origo = (300.0, 200.0, 300.0)
s0 = [t0, t1]
s1 = [[self.racer.rotate_point((0.0, math.pi / 2, 0.0), origo, p) for p in t]
for t in s0]
s2 = [[self.racer.rotate_point((0.0, -math.pi / 2, 0.0), origo, p) for p in t]
for t in s0]
s3 = [[self.racer.rotate_point((0.0, math.pi, 0.0), origo, p) for p in t]
for t in s0]
s4 = [[self.racer.rotate_point((math.pi / 2, 0.0, 0.0), origo, p) for p in t]
for t in s0]
s5 = [[self.racer.rotate_point((-math.pi / 2, 0.0, 0.0), origo, p) for p in t]
for t in s0]
half_cube_0 = s0 + s1 + s2 + s3 + s4 + s5
if square_texture is not None:
textures = [square_texture]
else:
textures = []
half_cubes = []
xr = 30000.0
yr = 1000.0
zr = 30000.0
for i in range(self.n_cubes):
xm = random.random() * xr - xr / 2
ym = random.random() * yr - yr / 2
zm = random.random() * zr - zr / 2
ax = random.random() * math.pi
ay = random.random() * math.pi
az = random.random() * math.pi
if square_texture is not None:
def make_surf(i):
surf = (2, (0.0, 0.0, 0.0), i % 2)
return surf
else:
# Use a random color.
hsv = (random.random() * 360.0,
random.random(),
random.random())
surf = (1, hsv, -1)
make_surf = lambda i: surf
def move_point(p):
return self.racer.rotate_point(
(ax, ay, az),
self.racer.translate_point((xm, ym, zm), origo),
self.racer.translate_point((xm, ym, zm), p))
half_cube = [[move_point(p) for p in t] + [make_surf(i)]
for t, i in
zip(half_cube_0, range(len(half_cube_0)))]
half_cubes.extend(half_cube)
return half_cubes, textures
def loop(self):
if self.just_colors:
triangles, textures = self.random_cubes()
else:
base_dir = os.path.dirname(__file__)
sonic_texture = self.racer.load_double_texture(
os.path.join(base_dir, 'data/futcubes/sonic-texture.png'))
textures_size = (100, 100)
assert (textures_size[0], textures_size[1], 3) == sonic_texture.shape
triangles, textures = self.random_cubes(sonic_texture)
# The objects will not change (only the camera changes), so we
# preprocess them to save important loop time.
triangles_pre = self.racer.preprocess_triangles(triangles)
textures_pre = self.racer.preprocess_textures(textures)
camera = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
keys_holding = {}
for x in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT,
pygame.K_PAGEUP, pygame.K_PAGEDOWN, pygame.K_1, pygame.K_2]:
keys_holding[x] = False
def inf_range():
i = 0
while True:
yield i
i += 1
for i in inf_range():
fps = self.clock.get_fps()
time_start = time.time()
dynamic_triangle = [[-300, -300, 500],
[300, -300, 500],
[0, 300, 400],
[1, [240, 1, 1], 0]]
dynamic_origo = [0, 0, 450]
dynamic_angles = [i / 60.0, i / 80.0, i / 100.0]
dynamic_triangle = self.racer.rotate_triangle(
dynamic_angles, dynamic_origo, dynamic_triangle)
frame = self.racer.render_triangles(
self.size, self.view_dist, self.draw_dist, camera,
[dynamic_triangle], triangles_pre,
None, textures_pre, self.render_approach, self.n_draw_rects)
time_end = time.time()
frame = frame.get()
futhark_dur_ms = (time_end - time_start) * 1000
pygame.surfarray.blit_array(self.surface, frame)
self.screen.blit(self.surface, (0, 0))
self.message('FPS: {:.02f}'.format(fps), (10, 10))
self.message('Futhark: {:.02f} ms'.format(futhark_dur_ms), (10, 40))
self.message('Draw distance: {:.02f}'.format(self.draw_dist), (10, 70))
self.message('Rendering approach: {}'.format(self.render_approach), (10, 100))
if self.render_approach == 'chunked':
self.message('# draw rects: x: {}, y: {}'.format(
*self.n_draw_rects), (10, 130))
pygame.display.flip()
# Check events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
return 0
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
return 0
if event.key in keys_holding.keys():
keys_holding[event.key] = True
if event.key == pygame.K_r:
self.render_approach = futracer.next_elem(futracer.render_approaches,
self.render_approach)
if event.key == pygame.K_a:
self.n_draw_rects[0] = max(1, self.n_draw_rects[0] - 1)
if event.key == pygame.K_d:
self.n_draw_rects[0] = self.n_draw_rects[0] + 1
if event.key == pygame.K_w:
self.n_draw_rects[1] = max(1, self.n_draw_rects[1] - 1)
if event.key == pygame.K_s:
self.n_draw_rects[1] = self.n_draw_rects[1] + 1
elif event.type == pygame.KEYUP:
if event.key in keys_holding.keys():
keys_holding[event.key] = False
if keys_holding[pygame.K_UP]:
p1 = camera[0][:]
p1[2] += 15
p2 = self.racer.rotate_point(camera[1], camera[0], p1)
camera[0] = list(p2)
if keys_holding[pygame.K_DOWN]:
p1 = camera[0][:]
p1[2] -= 15
p2 = self.racer.rotate_point(camera[1], camera[0], p1)
camera[0] = list(p2)
if keys_holding[pygame.K_LEFT]:
camera[1][1] -= 0.04
if keys_holding[pygame.K_RIGHT]:
camera[1][1] += 0.04
if keys_holding[pygame.K_PAGEUP]:
self.view_dist += 10.0
if keys_holding[pygame.K_PAGEDOWN]:
self.view_dist -= 10.0
if self.view_dist < 1.0:
self.view_dist = 1.0
if keys_holding[pygame.K_1]:
self.draw_dist -= 10.0
if keys_holding[pygame.K_2]:
self.draw_dist += 10.0
self.clock.tick()
def main(args):
def size(s):
return tuple(map(int, s.split('x')))
arg_parser = argparse.ArgumentParser(description='Use the arrow keys to move around. Use Page Up and Page Down to adjust the view distance.')
arg_parser.add_argument('--size', type=size, metavar='WIDTHxHEIGHT',
help='set the size of the racing game window')
arg_parser.add_argument('--cubes', type=int, metavar='N',
help='set the number of cubes in the world (defaults to 2000)')
arg_parser.add_argument('--just-colors', action='store_true',
help='use random colors instead of the pretty texture')
arg_parser.add_argument('--render-approach',
choices=futracer.render_approaches,
default='segmented',
help='choose how to render a frame')
args = arg_parser.parse_args(args)
cubes = FutCubes(size=args.size, n_cubes=args.cubes,
just_colors=args.just_colors,
render_approach=args.render_approach)
return cubes.run()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))