-- model/md2 tests: generated MD2 files against assimp's frame-0 soup and the writer's exact frames. local T = require("@tests/T") local F = require("@rosetta") local Md2 = require("@rosetta/model/md2") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Soup = require("@tests/model/_soup") local Fx = require("@tests/gen/md2") local t = T.suite("md2") t:eq(Md2.name, "md2", "name") for _, c in Fx do local s, err = Md2.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(s, c.name .. ": " .. tostring(err)) t:eq(F.detect(c.data), "md2", c.name .. ": F.detect") local prim = s.meshes[1].primitives[1] t:eq(prim.indices.count, c.tris * 3, c.name .. ": triangles") local ok, at = Soup.close(Soup.positions(prim), c.assimp.pos, 1e-5) t:check(ok, c.name .. ": positions against assimp (" .. tostring(at) .. ")") local uok, uat = Soup.close(Soup.uvs(prim, 0, true), c.assimp.uv, 1e-6) t:check(uok, c.name .. ": texture coordinates against assimp (" .. tostring(uat) .. ")") t:eq(Crc32.of(Soup.positions(prim)), c.crcs[1], c.name .. ": frame 1 exact") for k = 1, c.frames do local f = Soup.positions(prim, k) -- base + delta in f32 can differ from the frame by an ulp; compare to the decoded frame directly local frame = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { frame = k, morph = false }) :: any t:eq(Crc32.of(Soup.positions(frame.meshes[1].primitives[1])), c.crcs[k], c.name .. ": frame " .. k .. " exact (opts.frame)") local fok = Soup.close(f, (function() local list = {} local fp = Soup.positions(frame.meshes[1].primitives[1]) for i = 0, buffer.len(fp) // 4 - 1 do list[i + 1] = buffer.readf32(fp, i * 4) end return list end)(), 1e-5) t:check(fok, c.name .. ": morph target " .. k .. " = frame " .. k) end local groups = {} for _, a in s.animations do table.insert(groups, a.name) end t:eq(#s.meshes[1].weights, c.frames, c.name .. ": one weight per frame") t:eq(s.meta.source.frameNames[1], c.names[1], c.name .. ": frame names") if c.name == "monster" then t:eq(table.concat(groups, ","), "stand,run,attack,pain", c.name .. ": animation groups") t:eq(s.animations[2].channels[1].input.count, 4, c.name .. ": run has 4 keys") end t:eq(#s.textures, #c.skins, c.name .. ": skins") if #c.skins > 0 then t:eq(s.textures[1].uri, c.skins[1], c.name .. ": skin uri") t:eq(s.materials[prim.material].baseColorTexture.texture, 1, c.name .. ": material") end t:eq(s.meta.up, "z", c.name .. ": z up") end do local c = Fx[1] local lean = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { morph = false }) :: any t:check(lean.meshes[1].primitives[1].targets == nil and #lean.animations == 0, "morph = false") local _, e1 = Md2.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 300)))) t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1)) local bad = buffer.fromstring(c.data) buffer.writei32(bad, 4, 7) local _, e2 = Md2.decode(Bytes.reader(bad)) t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7") end local samples = {} for _, c in Fx do table.insert(samples, c.data) end t:fuzz("decode", function(b: buffer) return Md2.decode(Bytes.reader(b)) end, samples, { mutations = 80 }) t:done()