76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
# Quake II MD2 fixtures (tools/fixtures.py): MD2 files written here from the dmdl_t layout (random frames with
|
|
# scale / translate, distinct-vertex triangles, texture coordinates, skins, frame names in groups); references:
|
|
# assimp's triangle soup of frame 0 (positions, texture coordinates: assimp flips v) and this writer's own exact
|
|
# positions of every frame (CRC-32 of the f32 soup in our corner order: reversed to counter-clockwise).
|
|
# tests/gen/md2.luau { { name, data, frames = n, names, skins, tris, crcs = { per frame }, assimp = { pos, uv } } }
|
|
import importlib.util, 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 md2(rng, nverts, nst, ntris, frames, skins, sw=64, sh=32):
|
|
st = [(rng.randrange(sw), rng.randrange(sh)) for _ in range(nst)]
|
|
tris = []
|
|
for _ in range(ntris):
|
|
tris.append((tuple(rng.sample(range(nverts), 3)), tuple(rng.randrange(nst) for _ in range(3))))
|
|
fr = []
|
|
for name in frames:
|
|
scale = tuple(f32(rng.uniform(0.05, 0.2)) for _ in range(3))
|
|
tr = tuple(f32(rng.uniform(-20, 20)) for _ in range(3))
|
|
vs = [(rng.randrange(256), rng.randrange(256), rng.randrange(256), rng.randrange(162)) for _ in range(nverts)]
|
|
fr.append((scale, tr, name, vs))
|
|
framesize = 40 + 4 * nverts
|
|
o_skins = 68
|
|
o_st = o_skins + 64 * len(skins)
|
|
o_tris = o_st + 4 * nst
|
|
o_frames = o_tris + 12 * ntris
|
|
o_gl = o_frames + framesize * len(fr)
|
|
out = struct.pack("<4s16i", b"IDP2", 8, sw, sh, framesize, len(skins), nverts, nst, ntris, 1, len(fr), o_skins, o_st, o_tris, o_frames, o_gl, o_gl + 4)
|
|
for s in skins:
|
|
out += s.encode().ljust(64, b"\0")
|
|
for s, t in st:
|
|
out += struct.pack("<hh", s, t)
|
|
for v, t in tris:
|
|
out += struct.pack("<6H", *v, *t)
|
|
for scale, tr, name, vs in fr:
|
|
out += struct.pack("<6f", *scale, *tr) + name.encode().ljust(16, b"\0")
|
|
for v in vs:
|
|
out += bytes(v)
|
|
out += struct.pack("<i", 0)
|
|
crcs = []
|
|
for scale, tr, name, vs in fr:
|
|
soup = b""
|
|
for v, t in tris:
|
|
for c in (2, 1, 0):
|
|
p = vs[v[c]]
|
|
soup += struct.pack("<3f", *(p[k] * scale[k] + tr[k] for k in range(3)))
|
|
crcs.append(zlib.crc32(soup))
|
|
return out, crcs, len(tris)
|
|
|
|
|
|
def generate(ctx):
|
|
rng = ctx.rng(52)
|
|
R = ref(ctx)
|
|
specs = [
|
|
("monster", 40, 50, 60, ["stand01", "stand02", "stand03", "run1", "run2", "run3", "run4", "attack1", "attack2", "pain"], ["models/monsters/test/skin.pcx", "models/monsters/test/pain.pcx"]),
|
|
("single", 12, 12, 8, ["frame"], ["skin.pcx"]),
|
|
("noskin", 20, 30, 25, ["a1", "a2"], []),
|
|
]
|
|
out = []
|
|
for name, nv, nst, nt, frames, skins in specs:
|
|
data, crcs, ntris = md2(rng, nv, nst, nt, frames, skins)
|
|
out.append({"name": name, "data": data, "frames": len(frames), "names": frames, "skins": skins, "tris": ntris,
|
|
"crcs": crcs, "assimp": R.assimp_soup(ctx, data, "md2")})
|
|
ctx.emit("md2", out)
|