-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_quads.py
106 lines (84 loc) · 3.03 KB
/
gen_quads.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
import numpy as np
front_quad = [[0, 1, 1],
[1, 1, 1],
[1, 0, 1],
[0, 0, 1]]
left_quad = [[1, 1, 0],
[1, 1, 1],
[1, 0, 1],
[1, 0, 0]]
back_quad = [[1, 1, 1],
[0, 1, 1],
[0, 0, 1],
[1, 0, 1]]
right_quad = [[1, 1, 1],
[1, 1, 0],
[1, 0, 0],
[1, 0, 1]]
top_quad = [[0, 1, 0],
[1, 1, 0],
[1, 1, 1],
[0, 1, 1]]
bottom_quad = [[0, 1, 1],
[1, 1, 1],
[1, 1, 0],
[0, 1, 0]]
tex_coords = [[0, 0],
[1, 0],
[1, 1],
[0, 1]]
front_shade = 166
left_shade = 223
back_shade = 166
right_shade = 223
top_shade = 255
bottom_shade = 143
scale = 64
tex_scale = 16
max_size = 8
code_template = f"""// Auto-generated
#include <nusys.h>
#define QUAD_ADDR(face, width, height) (quad_verts + ((face) * {max_size * max_size} + (width) * {max_size} + (height)) * 4)
static Vtx quad_verts[] = {{
%s
}};
"""
def write_quad(unit_quad, shade, sx, sy, sz, tx, ty):
scaled_quad = np.array(unit_quad) * [sx, sy, sz] * scale
scaled_tex = (np.array(tex_coords) * [tx, ty] * tex_scale) << 6
for pos, tex in zip(scaled_quad, scaled_tex):
line = " {"
line += ", ".join(f"{p:>3}" for p in pos)
line += ", 0, "
line += f"{tex[0]:>4}, {tex[1]:>4}"
line += ", "
line += ", ".join([f"{shade:>3}"] * 3)
line += ", 255}"
lines.append(line)
def generate_vertex_code(unit_quad, shade, max_sx, max_sy, max_sz, tex_x_axis, tex_y_axis, order='xyz'):
if order == 'xyz':
for sx in range(1, max_sx + 1):
for sy in range(1, max_sy + 1):
for sz in range(1, max_sz + 1):
tex_x = [sx, sy, sz][tex_x_axis]
tex_y = [sx, sy, sz][tex_y_axis]
write_quad(unit_quad, shade, sx, sy, sz, tex_x, tex_y)
elif order == 'zyx':
for sz in range(1, max_sz + 1):
for sy in range(1, max_sy + 1):
for sx in range(1, max_sx + 1):
tex_x = [sx, sy, sz][tex_x_axis]
tex_y = [sx, sy, sz][tex_y_axis]
write_quad(unit_quad, shade, sx, sy, sz, tex_x, tex_y)
else:
raise RuntimeError(f"Unsupported order: {order}")
lines = []
generate_vertex_code(front_quad, front_shade, max_size, max_size, 1, 0, 1, order='xyz')
generate_vertex_code(left_quad, left_shade, 1, max_size, max_size, 2, 1, order='zyx')
generate_vertex_code(back_quad, back_shade, max_size, max_size, 1, 0, 1, order='xyz')
generate_vertex_code(right_quad, right_shade, 1, max_size, max_size, 2, 1, order='zyx')
generate_vertex_code(top_quad, top_shade, max_size, 1, max_size, 0, 2)
generate_vertex_code(bottom_quad, bottom_shade, max_size, 1, max_size, 0, 2)
code = code_template % ",\n".join(lines)
with open('include/quads.h', 'w') as file:
file.write(code)