227 lines
8.6 KiB
Luau
227 lines
8.6 KiB
Luau
-- model/bsp tests: Quake 1, Half-Life, Quake II and Quake III maps written by tools/fixtures/bsp.py: every model's
|
|
-- triangles per texture (positions, normals, texture coordinates against the documented rules), counter-clockwise
|
|
-- about the face normals, embedded textures (Quake through the palette, Half-Life through its own), WAD / .wal
|
|
-- textures as URIs, entities; Quake III: polygon / mesh faces against assimp's soup (of a .pk3) and the writer,
|
|
-- lightmaps, vertex colours, a tessellated patch; errors; the fuzz pass.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Bsp = require("@formats/model/bsp")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Crc32 = require("@formats/core/Crc32")
|
|
local Err = require("@formats/core/Err")
|
|
local Types = require("@formats/Types")
|
|
local Soup = require("@tests/model/_soup")
|
|
local Fx = require("@tests/gen/bsp")
|
|
|
|
local t = T.suite("bsp")
|
|
t:eq(Bsp.name, "bsp", "name")
|
|
|
|
local function near(a: { number }, b: { number }, tol: number): boolean
|
|
if #a ~= #b then
|
|
return false
|
|
end
|
|
for i = 1, #b do
|
|
if math.abs(a[i] - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function meshOf(s: any, model: number): any
|
|
for _, n in s.nodes do
|
|
if n.mesh and n.extras and n.extras.bsp and n.extras.bsp.model == model then
|
|
return s.meshes[n.mesh]
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- winding: every triangle's geometric normal agrees with its NORMAL
|
|
local function windingOk(p: any): boolean
|
|
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
|
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
|
|
local idx = p.indices
|
|
for k = 0, idx.count - 1, 3 do
|
|
local a, b, c = buffer.readu32(idx.data, k * 4), buffer.readu32(idx.data, k * 4 + 4), buffer.readu32(idx.data, k * 4 + 8)
|
|
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(c, 0) - pos(a, 0), pos(c, 1) - pos(a, 1), pos(c, 2) - pos(a, 2)
|
|
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
|
if cx * nrm(a, 0) + cy * nrm(a, 1) + cz * nrm(a, 2) <= 0 then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function brush(label: string, c: any, opts: any)
|
|
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), opts)
|
|
t:check(s, label .. ": " .. tostring(err))
|
|
t:eq(F.detect(c.data), "bsp", label .. ": F.detect")
|
|
for model, texs in c.models do
|
|
local mesh = meshOf(s, tonumber(model) :: number)
|
|
t:check(mesh, label .. ": model " .. model)
|
|
for tname, want in texs do
|
|
local prim
|
|
for _, p in mesh.primitives do
|
|
if p.material and s.materials[p.material].name == tname then
|
|
prim = p
|
|
end
|
|
end
|
|
t:check(prim, label .. ": model " .. model .. " texture " .. tname)
|
|
t:check(near(Soup.list(Soup.positions(prim)), want.pos, 1e-6), label .. ": " .. tname .. " positions")
|
|
t:check(near(Soup.uvs(prim, 0), want.uv, 1e-5), label .. ": " .. tname .. " texture coordinates")
|
|
t:check(near(Soup.list(prim.attributes.NORMAL.data), want.nrm, 1e-6), label .. ": " .. tname .. " normals")
|
|
t:check(windingOk(prim), label .. ": " .. tname .. " counter-clockwise")
|
|
end
|
|
end
|
|
t:eq(#s.meta.entities, 3, label .. ": entities")
|
|
t:eq(s.meta.entities[2].classname, "info_player_start", label .. ": entity classname")
|
|
t:eq(s.meta.entities[3].model, "*1", label .. ": brush entity model")
|
|
return s
|
|
end
|
|
|
|
-- 1. Quake 1, Half-Life
|
|
do
|
|
local q1 = brush("q1", Fx.q1, { palette = Fx.q1.palette })
|
|
t:eq(Crc32.of(q1.textures[1].image.pixels), Fx.q1.textures[1], "q1: embedded texture through the palette")
|
|
t:eq(q1.textures[2].uri, "+0door", "q1: WAD texture as a URI")
|
|
t:eq(q1.meta.source.variant, "quake", "q1: variant")
|
|
local hl = brush("hl", Fx.hl, nil)
|
|
t:eq(Crc32.of(hl.textures[1].image.pixels), Fx.hl.textures[1], "hl: embedded texture through its own palette")
|
|
t:eq(hl.meta.source.variant, "halflife", "hl: variant")
|
|
local grey = Bsp.decode(Bytes.reader(buffer.fromstring(Fx.q1.data))) :: any
|
|
t:eq(grey.meta.source.palette, false, "q1 without a palette: grey")
|
|
end
|
|
|
|
-- 2. Quake II
|
|
do
|
|
local q2 = brush("q2", Fx.q2, { files = { ["textures/brick.wal"] = Fx.q2.wal } })
|
|
t:eq(q2.textures[1].uri, "textures/brick.wal", "q2: .wal URI")
|
|
t:eq(q2.materials[1].extras.bsp.width, 16, "q2: size from the .wal")
|
|
t:eq(q2.materials[2].extras.bsp.texels, true, "q2: texel coordinates without the .wal")
|
|
end
|
|
|
|
-- 3. Quake III
|
|
do
|
|
local q = Fx.q3
|
|
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(q.data)))
|
|
t:check(s, "q3: " .. tostring(err))
|
|
t:eq(F.detect(q.data), "bsp", "q3: F.detect")
|
|
for model, keys in q.models do
|
|
local mesh = meshOf(s, tonumber(model) :: number)
|
|
for key, want in keys do
|
|
local sh, lm = string.match(key, "^(%-?%d+):(%-?%d+)$")
|
|
local prim
|
|
for _, p in mesh.primitives do
|
|
local mat = s.materials[p.material]
|
|
local lt = mat.extras.bsp.lightmap
|
|
local tex = mat.baseColorTexture and mat.baseColorTexture.texture
|
|
if tex == tonumber(sh) :: number + 1 and ((lt and s.textures[lt].image.meta.source.lightmap) or -1) == tonumber(lm) then
|
|
prim = p
|
|
end
|
|
end
|
|
t:check(prim, "q3: model " .. model .. " batch " .. key)
|
|
t:check(near(Soup.list(Soup.positions(prim)), want.pos, 1e-6), "q3: " .. key .. " positions")
|
|
t:check(near(Soup.uvs(prim, 0), want.uv, 1e-6), "q3: " .. key .. " texture coordinates")
|
|
t:check(near(Soup.uvs(prim, 1), want.lm, 1e-6), "q3: " .. key .. " lightmap coordinates")
|
|
local col = {}
|
|
local cr = Types.Scene.reader(prim.attributes.COLOR_0) :: any
|
|
for k = 0, prim.indices.count - 1 do
|
|
local v = buffer.readu32(prim.indices.data, k * 4)
|
|
for c = 0, 3 do
|
|
table.insert(col, math.floor(cr(v, c) * 255 + 0.5))
|
|
end
|
|
end
|
|
t:check(near(col, want.col, 0), "q3: " .. key .. " vertex colours")
|
|
t:check(windingOk(prim), "q3: " .. key .. " counter-clockwise about the normals")
|
|
end
|
|
end
|
|
-- assimp: the polygon / mesh faces
|
|
local all = {}
|
|
local apos = q.assimp.pos
|
|
for _, mesh in s.meshes do
|
|
for _, p in mesh.primitives do
|
|
local mat = s.materials[p.material]
|
|
if mat.name ~= "textures/base/floor" or p.indices.count == 6 then
|
|
if p.indices.count == 6 then
|
|
for _, x in Soup.list(Soup.positions(p)) do
|
|
table.insert(all, x)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:eq(q.assimp.faces, 6, "q3: assimp reads the 3 quads")
|
|
-- compare triangle sets: every assimp triangle is one of ours (up to corner rotation); assimp keeps the stored
|
|
-- clockwise order, we reverse it
|
|
local found = 0
|
|
for k = 0, #apos // 9 - 1 do
|
|
local tri = Soup.slice(apos, k * 9 + 1, 9)
|
|
tri = { tri[7], tri[8], tri[9], tri[4], tri[5], tri[6], tri[1], tri[2], tri[3] }
|
|
for j = 0, #all // 9 - 1 do
|
|
if Soup.triangles(Soup.slice(all, j * 9 + 1, 9), tri, 1e-5) then
|
|
found += 1
|
|
break
|
|
end
|
|
end
|
|
end
|
|
t:eq(found, 6, "q3: every assimp triangle matches one of ours")
|
|
t:eq(#q.lightmaps, 2, "q3: lightmaps")
|
|
for i, crc in q.lightmaps do
|
|
local tex
|
|
for _, tx in s.textures do
|
|
if tx.image and tx.image.meta.source.lightmap == i - 1 then
|
|
tex = tx
|
|
end
|
|
end
|
|
t:eq(Crc32.of(tex.image.pixels), crc, "q3: lightmap " .. i)
|
|
end
|
|
-- the patch: a flat 32 x 32 square at z = 100, tessellated
|
|
local area = 0
|
|
for _, mesh in s.meshes do
|
|
for _, p in mesh.primitives do
|
|
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
|
if pos(0, 2) == 100 then
|
|
t:check(windingOk(p), "q3: patch counter-clockwise")
|
|
for k = 0, p.indices.count - 1, 3 do
|
|
local ux, uy = pos(k + 1, 0) - pos(k, 0), pos(k + 1, 1) - pos(k, 1)
|
|
local vx, vy = pos(k + 2, 0) - pos(k, 0), pos(k + 2, 1) - pos(k, 1)
|
|
area += (ux * vy - uy * vx) / 2
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:check(math.abs(math.abs(area) - q.patchArea) < 1e-3, "q3: patch area " .. area)
|
|
t:eq(s.meta.faces.patches, 1, "q3: one patch")
|
|
local lean = Bsp.decode(Bytes.reader(buffer.fromstring(q.data)), { patchLevel = 1 }) :: any
|
|
t:check(lean ~= nil, "q3: patchLevel 1")
|
|
end
|
|
|
|
-- 4. errors, fuzz
|
|
do
|
|
local _, e1 = Bsp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.q1.data, 1, 100))))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
|
local _, e2 = Bsp.decode(Bytes.reader(buffer.fromstring("VBSP" .. string.rep("\0", 100))))
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "VBSP")
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Bsp.decode(Bytes.reader(b), { palette = Fx.q1.palette })
|
|
end, { Fx.q1.data, Fx.hl.data, Fx.q2.data }, { mutations = 80 })
|
|
do
|
|
local rand = T.rng(13)
|
|
local threw = 0
|
|
local q = Fx.q3.data
|
|
for _ = 1, 60 do
|
|
local b = buffer.fromstring(q)
|
|
for _ = 1, 1 + rand(10) do
|
|
buffer.writeu8(b, 8 + 17 * 8 + rand(#q - 8 - 17 * 8 - 98304), rand(256))
|
|
end
|
|
if not pcall(Bsp.decode, Bytes.reader(b)) then
|
|
threw += 1
|
|
end
|
|
end
|
|
t:eq(threw, 0, "q3: mutated lumps never throw")
|
|
end
|
|
t:done()
|