feat: model sourcing now all at once

This commit is contained in:
2026-04-19 20:45:59 +02:00
parent 6aafa7be81
commit 5432141c0d

149
parseObj.py Normal file → Executable file
View File

@@ -1,72 +1,95 @@
# Open the file in read mode #!/usr/bin/env python3
file = open("plane.obj", "r")
# Read the entire content of the file import os
content = file.read().split("\n")
file.close() dir_name = "3dModels/"
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 = [] def source_model(file_name: str):
for index, line in enumerate(content): # Open the file in read mode
if "vt" in line: file = open(dir_name + file_name + ".obj", "r")
normals = content[:index]
content = content[index:]
break
startFaces = 0 # Read the entire content of the file
faces = [] content = file.read().split("\n")
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] file.close()
verts = ["{" + ",".join(vert.split(" ")[1:4]) + "}" for vert in verts]
faces = [ startVerts = 0
",".join( verts = []
[ for index, line in enumerate(content):
str(int((d.split("/")[0])) - 1) + "," + str(int((d.split("/")[2])) - 1) if line[0] == "o":
for d in face.split(" ")[1:4] 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]
mtl_file = open(dir_name + file_name + ".mtl", "r")
shiny = mtl_file.read().split("\n")[4].split(" ")[1]
mtl_file.close()
print(shiny)
out = (
'#include "../renderer.h" \n const model '
+ file_name
+ "_model = {(vec3[]){"
+ ",".join(verts)
+ "},(int[]){"
+ ",".join(faces)
+ "},(vec3[]){"
+ ",".join(normals)
+ "},(vec3[]){"
+ ",".join(colors)
+ "},"
+ str(len(verts))
+ ","
+ str(len(faces) * 6)
+ ","
+ str(float(shiny) / 20)
+ "};"
) )
for face in faces
]
normals = ["{" + ",".join(normal.split(" ")[1:4]) + "}" for normal in normals]
out = ( with open("src/models/" + file_name + ".h", "w") as f:
'#include "../renderer.h" \n const model testModel = {(vec3[]){' f.write(out)
+ ",".join(verts) # Close the file
+ "},(int[]){" f.close()
+ ",".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) model_files = os.listdir("3dModels")
print(faces) li = range(0, 10)
# Close the file
for file in model_files:
if file.endswith(".obj"):
source_model(file[:-4])