-
Notifications
You must be signed in to change notification settings - Fork 25
/
bunobj.py
59 lines (55 loc) · 1.21 KB
/
bunobj.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
import sys
print("#include<stddef.h>")
verts=[]
faces=[]
normals=[]
for l in open(sys.argv[1],'r'):
lp=l.split()
if(len(lp)==0):
pass
elif(lp[0]=='v'):
verts.append(lp[1:])
elif(lp[0]=='vn'):
normals.append(lp[1:])
elif(lp[0]=='f'):
nfi=[]
for f in lp[1:]:
fp=f.split('//')
if(fp[0] != fp[1]):
raise "NonMatchingIndex!"
nfi.append(str(int(fp[0])-1))
faces.append(nfi)
print("const float interleavedverts[6*%d]={\n" % (len(verts)))
for vi in range(len(verts)):
print(','.join(verts[vi]+normals[vi])+(',' if (vi < len(verts)-1) else ''))
print("};")
print("const size_t indices[3*%d]={\n" % (len(faces)))
for fi in range(len(faces)):
print(','.join(faces[fi])+(',' if (fi < len(faces)-1) else ''))
print("};")
print("""
#include<string.h>
void gen_bunny_interleaved_array(
float* out_vertices,
size_t* out_indices,
size_t* num_vertices,
size_t* num_indices)
{
if(num_indices)
{
*num_indices=sizeof(indices)/sizeof(indices[0]);
}
if(num_vertices)
{
*num_vertices=(sizeof(interleavedverts)/sizeof(interleavedverts[0]));
}
if(out_vertices)
{
memcpy(out_vertices,interleavedverts,sizeof(interleavedverts));
}
if(out_indices)
{
memcpy(out_indices,indices,sizeof(indices));
}
}
""")