Files
rosetta/tools/fixtures/md3.py
T

94 lines
4.1 KiB
Python

# Quake III MD3 fixtures (tools/fixtures.py): MD3 files written here from the md3Header_t / md3Surface_t layout
# (several surfaces with shaders, frames of 1/64-unit i16 positions with lat / long normals, tags per frame);
# references: assimp's triangle soup of frame 0 (every surface: positions, texture coordinates) and this writer's
# exact positions and decoded normals of every frame (CRC-32 of the f32 soup in our corner order), tag origins.
# tests/gen/md3.luau { { name, data, frames, surfaces = { { name, shader, tris, crcs, ncrcs } }, tags = { ... },
# assimp = { pos, uv, meshes } } }
import importlib.util, math, os, struct, zlib
INPUTS = ["tools/fixtures/_quake_ref.py"]
def ref(ctx):
spec = importlib.util.spec_from_file_location("_quake_ref", os.path.join(ctx.root, "tools", "fixtures", "_quake_ref.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def f32(x):
return struct.unpack("<f", struct.pack("<f", x))[0]
def normal(n):
lat = ((n >> 8) & 255) * math.pi / 128
lng = (n & 255) * math.pi / 128
return (math.cos(lat) * math.sin(lng), math.sin(lat) * math.sin(lng), math.cos(lng))
def surface(rng, name, shader, nframes, nverts, ntris):
tris = [tuple(rng.sample(range(nverts), 3)) for _ in range(ntris)]
st = [(f32(rng.uniform(0, 1)), f32(rng.uniform(0, 1))) for _ in range(nverts)]
xyz = [[(rng.randrange(-3000, 3000), rng.randrange(-3000, 3000), rng.randrange(-3000, 3000), rng.randrange(65536)) for _ in range(nverts)] for _ in range(nframes)]
o_tri = 108
o_sh = o_tri + 12 * ntris
o_st = o_sh + 68
o_xyz = o_st + 8 * nverts
o_end = o_xyz + 8 * nverts * nframes
out = struct.pack("<4s64siiiiiiiiii", b"IDP3", name.encode(), 0, nframes, 1, nverts, ntris, o_tri, o_sh, o_st, o_xyz, o_end)
for t in tris:
out += struct.pack("<3i", *t)
out += struct.pack("<64si", shader.encode(), 0)
for s in st:
out += struct.pack("<2f", *s)
for f in xyz:
for x, y, z, n in f:
out += struct.pack("<3hH", x, y, z, n)
crcs, ncrcs = [], []
for f in xyz:
soup, nsoup = b"", b""
for t in tris:
for c in (2, 1, 0):
x, y, z, n = f[t[c]]
soup += struct.pack("<3f", x / 64, y / 64, z / 64)
nsoup += struct.pack("<3f", *normal(n))
crcs.append(zlib.crc32(soup))
ncrcs.append(zlib.crc32(nsoup))
return out, {"name": name, "shader": shader, "tris": ntris, "crcs": crcs, "ncrcs": ncrcs}
def md3(rng, nframes, surfs, ntags):
frames = b""
for i in range(nframes):
frames += struct.pack("<10f16s", *([0.0] * 10), b"frame%d" % i)
tags, tagref = b"", []
for f in range(nframes):
for k in range(ntags):
o = tuple(f32(rng.uniform(-10, 10)) for _ in range(3))
ang = rng.uniform(0, math.pi)
ax = ((math.cos(ang), math.sin(ang), 0.0), (-math.sin(ang), math.cos(ang), 0.0), (0.0, 0.0, 1.0))
tags += struct.pack("<64s12f", b"tag_%d" % k, *o, *[c for a in ax for c in a])
tagref.append({"name": "tag_%d" % k, "frame": f + 1, "origin": list(o), "angle": ang})
body, info = b"", []
for args in surfs:
s, i = surface(rng, args[0], args[1], nframes, args[2], args[3])
body += s
info.append(i)
o_frames = 108
o_tags = o_frames + len(frames)
o_surf = o_tags + len(tags)
o_end = o_surf + len(body)
head = struct.pack("<4si64siiiiiiiii", b"IDP3", 15, b"models/test/test.md3", 0, nframes, ntags, len(surfs), 0, o_frames, o_tags, o_surf, o_end)
return head + frames + tags + body, info, tagref
def generate(ctx):
rng = ctx.rng(63)
R = ref(ctx)
out = []
for name, nframes, surfs, ntags in [("two-surfaces", 4, [("body", "models/test/body.tga", 30, 40), ("head", "models/test/head", 12, 10)], 2),
("one-frame", 1, [("s", "textures/x.tga", 8, 6)], 0)]:
data, info, tags = md3(rng, nframes, surfs, ntags)
out.append({"name": name, "data": data, "frames": nframes, "surfaces": info, "tags": tags, "assimp": R.assimp_soup(ctx, data, "md3")})
ctx.emit("md3", out)