Files

88 lines
4.3 KiB
Luau

-- model/proc tests: dmap-layout Doom 3 and Quake 4 files written by the fixture (models, surfaces, portals, nodes,
-- shadow models) against the writer's geometry. Fixtures: tools/fixtures/proc.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Proc = require("@rosetta/model/proc")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/proc")
local t = T.suite("proc")
t:eq(Proc.name, "proc", "name")
for key, c in Fx do
t:eq(F.detect(c.text), "proc", key .. ": F.detect")
local s, e = Proc.decode(Bytes.reader(buffer.fromstring(c.text)))
t:check(s, key .. ": " .. tostring(e))
local sc = s :: any
t:eq(#sc.meshes, #c.models, key .. ": models")
for mi, m in c.models do
local mesh = sc.meshes[mi]
t:eq(mesh.name, m.name, key .. ": model name")
t:eq(#mesh.primitives, #m.surfaces, key .. ": surfaces of " .. m.name)
for si, w in m.surfaces do
local p = mesh.primitives[si]
local label = key .. " " .. m.name .. " surface " .. si
t:eq(p.attributes.POSITION.count, w.verts, label .. ": vertices")
t:eq(Crc32.of(p.attributes.POSITION.data), w.pos, label .. ": positions")
t:eq(Crc32.of(p.attributes.TEXCOORD_0.data), w.uv, label .. ": texcoords")
t:eq(Crc32.of(p.indices.data), w.idx, label .. ": indices, counter-clockwise")
t:eq(sc.materials[p.material].name, w.material, label .. ": material")
-- every triangle faces its vertex normal after the turn
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
local idx = Types.Scene.reader(p.indices) :: any
local ok = true
for k = 0, p.indices.count - 1, 3 do
local a, b, cc = idx(k, 0), idx(k + 1, 0), idx(k + 2, 0)
local ux, uy, uz = pos(b, 0) - pos(a, 0), pos(b, 1) - pos(a, 1), pos(b, 2) - pos(a, 2)
local vx, vy, vz = pos(cc, 0) - pos(a, 0), pos(cc, 1) - pos(a, 1), pos(cc, 2) - pos(a, 2)
local d = (uy * vz - uz * vy) * nrm(a, 0) + (uz * vx - ux * vz) * nrm(a, 1) + (ux * vy - uy * vx) * nrm(a, 2)
ok = ok and d > 0
end
t:check(ok, label .. ": winding faces the normals")
if key == "quake4" then
t:check(p.attributes.COLOR_0 ~= nil, label .. ": vertex colours")
end
end
end
local info = sc.meta.proc
t:eq(info.areas, c.areas, key .. ": areas")
t:eq(#info.portals, #c.portals, key .. ": portals")
t:eq(info.portals[1].positive, c.portals[1].positive, key .. ": portal areas")
t:eq(#info.portals[1].points, #c.portals[1].points, key .. ": portal points")
t:eq(info.portals[1].points[12], c.portals[1].points[12], key .. ": portal point")
t:eq(#info.nodes, #c.nodes, key .. ": nodes")
t:eq(info.nodes[1].positive, 2, key .. ": node children 1-based")
t:eq(info.nodes[1].negative, -1, key .. ": area children kept")
t:eq(info.nodes[1].plane[4], c.nodes[1].plane[4], key .. ": node plane")
t:eq(info.shadowModels[1].name, c.shadow.name, key .. ": shadow model")
t:eq(#info.shadowModels[1].vertices, c.shadow.verts * 3, key .. ": shadow vertices")
t:eq(#info.shadowModels[1].indices, c.shadow.indices, key .. ": shadow indices")
t:eq(info.models[1].area, 0, key .. ": area number")
t:eq(#sc.nodes, 1 + #c.models, key .. ": a node per model")
t:eq(sc.meta.up, "z", key .. ": z up")
end
t:eq((F.load(Fx.quake4.text) :: any).meta.source.version, "PROC 4", "Quake 4 version")
do
local function code(s: string, opts: any?): any
local _, e = Proc.decode(Bytes.reader(buffer.fromstring(s)), opts)
return e and e.code
end
t:eq(code("hello"), Err.CORRUPT, "not a proc")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0 5 } }'), Err.CORRUPT, "index out of range")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 ) 0 0 0 } }'), Err.CORRUPT, "short vertex")
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0'), Err.CORRUPT, "truncated")
t:eq(code(Fx.doom3.text, { maxVertices = 10 }), Err.LIMIT, "maxVertices")
local s = Proc.decode(Bytes.reader(buffer.fromstring("mapProcFile003 unknownSection { a { b } c } model { \"m\" 0 }"))) :: any
t:check(s and #s.meshes == 1, "unknown sections skipped")
end
t:fuzz("decode", function(b: buffer)
return Proc.decode(Bytes.reader(b))
end, { buffer.fromstring(Fx.doom3.text), buffer.fromstring(Fx.quake4.text) }, { mutations = 300 })
t:done()