52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
def convert(input_filename):
|
|
with open(input_filename, "r") as f:
|
|
lines = [line.strip() for line in f if line.strip()]
|
|
|
|
# --- Step 1: Parse header ---
|
|
idx = 0
|
|
_ignored = int(lines[idx]) # First line, ignored
|
|
idx += 1
|
|
|
|
num_vertices = int(lines[idx])
|
|
idx += 1
|
|
|
|
# --- Step 2: Extract vertex data ---
|
|
vertices = []
|
|
for _ in range(num_vertices):
|
|
# Each vertex: x y z u v nx ny nz (8 numbers)
|
|
vertex = [lines[idx + i] for i in range(8)]
|
|
vertices.append(vertex)
|
|
idx += 8
|
|
|
|
# --- Step 3: Write mesh.txt ---
|
|
with open("mesh.txt", "w") as mesh_file:
|
|
mesh_file.write(str(num_vertices) + "\n")
|
|
for v in vertices:
|
|
for comp in v:
|
|
mesh_file.write(comp + "\n")
|
|
|
|
# --- Step 4: Parse texture ---
|
|
width = int(lines[idx]); idx += 1
|
|
height = int(lines[idx]); idx += 1
|
|
|
|
num_pixels = width * height
|
|
pixels = []
|
|
for _ in range(num_pixels):
|
|
r = int(lines[idx]); idx += 1
|
|
g = int(lines[idx]); idx += 1
|
|
b = int(lines[idx]); idx += 1
|
|
pixels.append((r, g, b))
|
|
|
|
# --- Step 5: Write texture.bin ---
|
|
with open("texture.bin", "wb") as tex_file:
|
|
for r, g, b in pixels:
|
|
tex_file.write(bytes([r, g, b]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
if len(sys.argv) != 2:
|
|
print(f"Usage: {sys.argv[0]} <input_file>")
|
|
else:
|
|
convert(sys.argv[1])
|