Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import argv
- from struct import pack
- from math import ceil,log
- if len(argv) < 2: print("no file name given!"); exit()
- vertices,faces=[],[]; byteBuff=b''; lineNum=1; amountMax=0
- fileI=open(argv[1],"r")
- for l in fileI:
- line=l[:-1].split(" ")
- if line[0] == "v":
- byteBuff=b''
- for v in range(1,len(line)): byteBuff+=pack("f", float(line[v]))
- vertices.append(byteBuff)
- elif line[0] == "f":
- faces.append([]); last=len(faces)-1
- for index in range(1,len(line)): faces[last].append( int(line[index].split("/")[0])-1 )
- if len(faces[last]) != 3: print(("line {}: face is not a triangle").format(lineNum)); exit()
- lineNum+=1
- fileI.close(); fileO=open(argv[1].split(".")[0]+".rtr","wb")
- for face in faces: amountMax=max(amountMax or 0,face[0],face[1],face[2])
- bc=ceil(log(amountMax,2)/8)
- fileO.write(bc.to_bytes(1,"little")+len(vertices).to_bytes(4,"little")+len(faces).to_bytes(4,"little"))
- for vertex in vertices: fileO.write(vertex)
- for face in faces: fileO.write(face[0].to_bytes(bc,"little")+face[1].to_bytes(bc,"little")+face[2].to_bytes(bc,"little"))
- fileO.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement