-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj2h.py
108 lines (87 loc) · 2.25 KB
/
obj2h.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
import sys
if len(sys.argv) != 2:
print("Usage: obj2h.py <filename>")
sys.exit(1)
filename = sys.argv[1]
# Get file name
title = filename.split(".")[:-1]
print("[INFO] Reading file:", filename)
with open(filename, "r") as f:
data = f.read()
vs = [line for line in data.split("\n") if line.startswith("v ")]
vts = [line for line in data.split("\n") if line.startswith("vt ")]
fs = [line for line in data.split("\n") if line.startswith("f ")]
vertices = [
[float(component.strip()) for component in components.split(" ")[1:4]]
for components in vs
]
textures = [
[float(component.strip()) for component in components.split(" ")[1:3]]
for components in vts
]
faces = [
[
[int(component.split("/")[0]), int(component.split("/")[1])]
for component in components.split(" ")[1:4]
]
for components in fs
]
print("[DATA] Vertices:", len(vertices))
print("[DATA] Texture Coordinates:", len(textures))
print("[DATA] Faces:", len(faces))
stuff = f"""// This file was generated by obj2h.py
// OBJ file: {filename}
#include "renderer/mesh.h"
"""
# vertices
stuff += "vector3 vertices[] = {\n"
for vertex in vertices:
stuff += (
" { "
+ str(vertex[0])
+ ", "
+ str(vertex[1])
+ ", "
+ str(vertex[2])
+ "},\n"
)
stuff += "};\n"
# Texure coordinates
stuff += """
// Texture coordinates
point textureCoords[] = {
"""
for texture in textures:
stuff += " { " + str(texture[0]) + ", " + str(texture[1]) + "},\n"
stuff += "};\n"
# triangles
stuff += "vector2 faces[] = {\n"
for face in faces:
stuff += (
" { vertices["
+ str(face[0][0] - 1)
+ "] , vertices["
+ str(face[1][0] - 1)
+ "] , vertices["
+ str(face[2][0] - 1)
+ "], textureCoords["
+ str(face[0][1] - 1)
+ "], textureCoords["
+ str(face[1][1] - 1)
+ "], textureCoords["
+ str(face[2][1] - 1)
+ "]},\n"
)
stuff += "};\n"
stuff += f"""
mesh {title[0]} = {{
faces,
{len(faces)},
new vertex({{0, 0, 0}}), // Rotation
new vertex({{0, 0, 200}}), // Translation
10, // Scale
}};
"""
print("[INFO] Writing to file:", title[0] + ".h")
with open(title[0] + ".h", "w") as f:
f.write(stuff)