Files
SoftwareRenderer/parseObj.py
2026-04-03 12:34:06 +02:00

73 lines
1.6 KiB
Python

# Open the file in read mode
file = open("plane.obj", "r")
# Read the entire content of the file
content = file.read().split("\n")
file.close()
startVerts = 0
verts = []
for index, line in enumerate(content):
if line[0] == "o":
startVerts = index + 1
content = content[startVerts:]
break
for index, line in enumerate(content):
if "vn" in line:
endVerts = index
verts = content[:endVerts]
content = content[endVerts:]
break
normals = []
for index, line in enumerate(content):
if "vt" in line:
normals = content[:index]
content = content[index:]
break
startFaces = 0
faces = []
for index, line in enumerate(content):
if line[0] == "f":
startFaces = index
faces = content[startFaces:-1]
break
colors = ["{" + ",".join(vert.split(" ")[4:7]) + "}" for vert in verts]
verts = ["{" + ",".join(vert.split(" ")[1:4]) + "}" for vert in verts]
faces = [
",".join(
[
str(int((d.split("/")[0])) - 1) + "," + str(int((d.split("/")[2])) - 1)
for d in face.split(" ")[1:4]
]
)
for face in faces
]
normals = ["{" + ",".join(normal.split(" ")[1:4]) + "}" for normal in normals]
out = (
'#include "../renderer.h" \n const model testModel = {(vec3[]){'
+ ",".join(verts)
+ "},(int[]){"
+ ",".join(faces)
+ "},(vec3[]){"
+ ",".join(normals)
+ "},(vec3[]){"
+ ",".join(colors)
+ "},"
+ str(len(verts))
+ ","
+ str(len(faces) * 6)
+ "};"
)
with open("src/models/plane.h", "w") as f:
f.write(out)
print(faces)
# Close the file