feat: model sourcing now all at once

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

75
parseObj.py Normal file → Executable file
View File

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