-- model/bsp tests: generated Quake 1 / Half-Life / Quake II / Quake III maps; Quake III faces against assimp. -- Fixtures: tools/fixtures/bsp.py. local T = require("@tests/T") local F = require("@rosetta") local Bsp = require("@rosetta/model/bsp") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/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 for _, mat in s.materials do local ref = mat.baseColorTexture t:check(ref and s.textures[ref.texture] and s.textures[ref.texture].name == mat.name, label .. ": material " .. mat.name .. " links its texture") end t:eq(s.meta.unit, 0.0254, label .. ": inch units") 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 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 -- Half-Life / Quake II RGB lightmaps for _, name in { "hl", "q2" } do local c = Fx[name] local s = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any local data = buffer.fromstring(c.data) local la = buffer.readi32(data, (if name == "q2" then 8 + 7 * 8 else 4 + 8 * 8)) local atlas for _, tx in s.textures do if tx.name == "lightmap" then atlas = tx.image end end local placed = {} for _, r in s.meta.lightmaps do placed[r.face] = r end local bad = 0 for _, want in c.lightmaps do local r = placed[want.face] if not r or r.w ~= want.w or r.h ~= want.h then bad += 1 else for k = 0, want.w * want.h - 1 do local px = buffer.readu32(atlas.pixels, ((r.y + k // want.w) * atlas.width + r.x + k % want.w) * 4) local o = la + want.ofs + k * 3 if px ~= 0xFF000000 + buffer.readu8(data, o + 2) * 65536 + buffer.readu8(data, o + 1) * 256 + buffer.readu8(data, o) then bad += 1 end end end end t:check(#c.lightmaps == 12 and bad == 0, name .. ": RGB lightmaps (" .. bad .. " wrong)") end 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 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 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") -- 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") t:eq(s.meta.unit, 0.0254, "q3: inch units") local lean = Bsp.decode(Bytes.reader(buffer.fromstring(q.data)), { patchLevel = 1 }) :: any t:check(lean ~= nil, "q3: patchLevel 1") end 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 -- ericw-tools maps: BSP 29 / BSP2 / 2PSB, lightmap atlas, .lit, visibility and hulls do local base: any = nil for _, name in { "bsp29", "bsp2", "2psb" } do local c = Fx.ericw[name] local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), { lit = c.lit }) t:check(s, name .. ": " .. tostring(err)) local sc = s :: any t:eq(F.detect(c.data), "bsp", name .. ": F.detect") t:eq(sc.meta.source.variant, "quake", name .. ": variant") t:eq(sc.meta.source.layout, ({ bsp29 = nil, bsp2 = "BSP2", ["2psb"] = "2PSB" } :: any)[name], name .. ": layout") t:eq(sc.meta.skippedFaces, 0, name .. ": every face read") -- the same compile in three layouts: the same triangles local all = {} for _, mesh in sc.meshes do for _, p in mesh.primitives do for _, x in Soup.list(Soup.positions(p)) do table.insert(all, x) end end end if base then t:check(near(all, base, 0), name .. ": geometry equals BSP 29's") else base = all end -- lightmaps: sizes as CalcSurfaceExtents, luxels copied from the lump (coloured from the .lit) local placed = {} for _, r in sc.meta.lightmaps do placed[r.face] = r end local atlas for _, tx in sc.textures do if tx.name == "lightmap" then atlas = tx.image end end t:check(atlas ~= nil, name .. ": lightmap atlas") local data = buffer.fromstring(c.data) local litBuf = buffer.fromstring(c.lit) local sizeBad, pixBad = 0, 0 for _, want in c.lightmaps do local r = placed[want.face] if not r or r.w ~= want.w or r.h ~= want.h then sizeBad += 1 else for k = 0, want.w * want.h - 1, 7 do local x, y = r.x + k % want.w, r.y + k // want.w local px = buffer.readu32(atlas.pixels, (y * atlas.width + x) * 4) local o = 8 + (want.ofs + k) * 3 local exp = 0xFF000000 + buffer.readu8(litBuf, o + 2) * 65536 + buffer.readu8(litBuf, o + 1) * 256 + buffer.readu8(litBuf, o) if px ~= exp then pixBad += 1 end end end end t:eq(sizeBad, 0, name .. ": lightmap sizes (" .. #c.lightmaps .. " faces)") t:eq(pixBad, 0, name .. ": lightmap luxels from the .lit") local _ = data local prim = sc.meshes[1].primitives[1] t:check(prim.attributes.TEXCOORD_1 ~= nil, name .. ": TEXCOORD_1") local mat = sc.materials[prim.material] t:eq(sc.textures[mat.extras.bsp.lightmap].name, "lightmap", name .. ": material names the atlas") local lu = Types.Scene.reader(prim.attributes.TEXCOORD_1) :: any local inRange = true for k = 0, prim.attributes.TEXCOORD_1.count - 1 do if lu(k, 0) < 0 or lu(k, 0) > 1 or lu(k, 1) < 0 or lu(k, 1) > 1 then inRange = false end end t:check(inRange, name .. ": lightmap coordinates inside the atlas") local grey = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any local gr for _, r in grey.meta.lightmaps do if r.face == c.lightmaps[1].face then gr = r end end local gimg for _, tx in grey.textures do if tx.name == "lightmap" then gimg = tx.image end end local v = buffer.readu8(data, 0) local _ = v local px = buffer.readu32(gimg.pixels, (gr.y * gimg.width + gr.x) * 4) t:eq(bit32.band(px, 0xFF), bit32.band(bit32.rshift(px, 8), 0xFF), name .. ": without .lit: grey luxels") -- visibility as vis wrote it t:eq(#sc.meta.bsp.leaves, c.leaves, name .. ": leaves") local pvsBad = 0 for leaf, want in c.pvs do local got = Bsp.pvs(sc, leaf - 1) or {} if table.concat(got, ",") ~= table.concat(want, ",") then pvsBad += 1 end end t:eq(pvsBad, 0, name .. ": PVS of every leaf") t:check(c.visible > 0, name .. ": vis ran") -- contents in the point hull and the clipping hulls local cBad = 0 for _, pr in c.probes do for h = 0, 2 do local got = Bsp.contents(sc, pr.p[1], pr.p[2], pr.p[3], h) if got ~= pr.c[h + 1] then cBad += 1 t:note("%s: contents at %s hull %d: %s, want %s", name, table.concat(pr.p, " "), h, tostring(got), tostring(pr.c[h + 1])) end end end t:eq(cBad, 0, name .. ": contents at the probes") t:eq(Bsp.contents(sc, -128, -128, 100, 0), -1, name .. ": the room is empty") t:eq(Bsp.contents(sc, 0, 0, 100, 0), -2, name .. ": the pillar is solid") t:eq(Bsp.contents(sc, -250, 0, 100, 1), -2, name .. ": hull 1 is solid near a wall") t:eq(Bsp.contents(sc, 0, 0, -8, 0), -2, name .. ": the floor is solid") local leaf = Bsp.leafAt(sc, -128, -128, 24) t:check(leaf and leaf > 0, name .. ": player start leaf") local seen = Bsp.pvs(sc, leaf :: number) or {} t:check(table.find(seen, Bsp.leafAt(sc, 0, 0, 220) :: number) ~= nil, name .. ": the light's leaf is visible from the start") end t:fuzz("bsp2", function(b: buffer) return Bsp.decode(Bytes.reader(b), { lit = Fx.ericw.bsp2.lit }) end, { Fx.ericw.bsp2.data, Fx.ericw["2psb"].data, Fx.ericw.bsp29.data }, { mutations = 60 }) end t:done()