-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjConverter.py
53 lines (43 loc) · 1.47 KB
/
objConverter.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
inputFile = input("Input file: ")
vertexPositions = []
normals = []
faces = []
with open(inputFile, "r") as f:
for line in f.readlines():
tokens = line.rstrip().split(" ")
if(tokens[0] == "v"):
vertexPositions.append([tokens[1], tokens[2], tokens[3]])
if(tokens[0] == "vn"):
normals.append([tokens[1], tokens[2], tokens[3]])
elif(tokens[0] == "f"):
faces.append([tokens[1], tokens[2], tokens[3]])
vertexNormals = {}
faceVertices = []
for face in faces:
for faceVertex in face:
faceVertex = faceVertex.split("//")
vertexIndex = int(faceVertex[0]) - 1
normalIndex = int(faceVertex[1]) - 1
faceVertices.append(vertexIndex)
vertexNormals[vertexIndex] = normalIndex
print("// prettier-ignore")
print("static vertices = [")
print(" // position + normal")
for i in range(len(vertexPositions)):
vertexPosition = vertexPositions[i]
normalIndex = vertexNormals[i]
vertexNormal = normals[normalIndex]
print(" {0}, {1}, {2}, {3}, {4}, {5},".format(
float(vertexPosition[0]),
float(vertexPosition[1]),
float(vertexPosition[2]),
float(vertexNormal[0]),
float(vertexNormal[1]),
float(vertexNormal[2])))
print("];\n")
print("// prettier-ignore")
print("static indices = [")
for i in range(0, len(faceVertices), 3):
print(" {0}, {1}, {2},".format(
faceVertices[i], faceVertices[i + 1], faceVertices[i + 2]))
print("];\n")