DumpDVD/DumpDVD/Gui/DvdSpin/compile_cd_model_to_h.py

63 lines
2.4 KiB
Python

import struct
from PIL import Image
imgName = "ceedee"
hdr = "#ifndef _LI_"+imgName.upper()+"_H\n"
hdr += "#define _LI_"+imgName.upper()+"_H 1\n"
hdr += "#include \"vec.hpp\"\n"
hdr += "#include <stdint.h>\n"
def readTexture(imgName):
global hdr
with Image.open(imgName+".png") as img:
img = img.convert("RGBA")
imgWidth, imgHeight = img.size
hdr += "const uint32_t "+imgName+"ImgWidth = "+hex(imgWidth)+";\n"
hdr += "const uint32_t "+imgName+"ImgHeight = "+hex(imgHeight)+";\n"
hdr += "const uint32_t "+imgName+"ImgChannels = "+hex(4)+";\n"
rgbaValues = []
for y in range(0, imgHeight):
for x in range(0, imgWidth):
r, g, b, a = img.getpixel((x, y))
rgba = 0;
rgba |= a << 24
rgba |= b << 16
rgba |= g << 8
rgba |= r
rgbaValues.append(hex(rgba))
hdr += "const uint32_t "+imgName+"ImgDataSz = "+hex(len(rgbaValues))+";\n"
hdr += "static uint32_t "+imgName+"ImgData["+hex(len(rgbaValues))+"] {"+", ".join(rgbaValues)+"};\n"
def read3D(vboName):
global hdr
with open(vboName + ".vbo", "rb") as vbo:
totalVertex, totalIndex = struct.unpack("II", vbo.read(4 * 2))
hdr += "const uint32_t "+imgName+"ModelTotalVertices = "+hex(totalVertex)+";\n"
hdr += "const uint32_t "+imgName+"ModelTotalIndices = "+hex(totalIndex)+";\n"
verticies = []
indicies = []
for i in range(0, totalVertex):
posX, posY, posZ = struct.unpack("fff", vbo.read(4 * 3))
norX, norY, norZ = struct.unpack("fff", vbo.read(4 * 3))
texX, texY = struct.unpack("ff", vbo.read(4 * 2))
verticies.append("{ Vec3({"+str(posX*30.0)+"f, "+str(posY*30.0)+"f, "+str(posZ*30.0)+"f}), Vec2({"+str(texX)+"f, "+str(texY)+"f}) }")
for i in range(0, totalIndex):
index = struct.unpack("H", vbo.read(2))
indicies.append(hex(index[0]))
hdr += "static ModelVertex "+imgName+"ModelVertexBuffer["+hex(len(verticies))+"] { "+", ".join(verticies)+"};\n"
hdr += "static uint16_t "+imgName+"ModelIndexBuffer["+hex(len(indicies))+"] { "+", ".join(indicies)+" };\n"
readTexture(imgName)
read3D(imgName)
hdr += "#endif"
print(hdr)