--!strict -- model/bsp/Brush: Quake 1 (BSP 29), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from bspfile.h of the -- Quake / Quake II source releases (no code taken). Texture coordinates: s = v . s.xyz + s.w, divided by the texture -- size. local Types = require("@rosetta/Types") local Pixels = require("@rosetta/codecs/image/Pixels") local Brush = {} local Scene = Types.Scene local readi32, readu16, readf32, readu32 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32 local writef32, writeu32 = buffer.writef32, buffer.writeu32 export type Lump = { at: number, size: number } export type Options = { palette: buffer?, wal: ((name: string) -> buffer?)?, -- Quake II: a texture's .wal file (for its size) maxPixels: number?, maxVertices: number?, } type Tex = { name: string, width: number, height: number, image: Types.Image?, texels: boolean } type Batch = { tex: number, pos: { number }, nrm: { number }, uv: { number } } local function cstr(b: buffer, at: number, n: number): string return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string end local function miptex(b: buffer, base: number, l: Lump, hl: boolean, pal: buffer?, maxPixels: number): ({ Tex }?, string?) local out: { Tex } = {} if l.size < 4 then return out, nil end local at = base + l.at local n = readi32(b, at) if n < 0 or n > 65536 or 4 + n * 4 > l.size then return nil, "bad miptex lump" end local gpal = if pal then Pixels.palette(pal, 0, 256) else nil for i = 0, n - 1 do local o = readi32(b, at + 4 + i * 4) if o < 0 or o + 40 > l.size then out[i + 1] = { name = "missing" .. i, width = 64, height = 64, image = nil, texels = false } else local m = at + o local name = cstr(b, m, 16) local w, h, off0 = readu32(b, m + 16), readu32(b, m + 20), readu32(b, m + 24) local t: Tex = { name = name, width = math.max(w, 1), height = math.max(h, 1), image = nil, texels = false } if off0 ~= 0 and w > 0 and h > 0 and w * h <= maxPixels then local px = m + off0 local need = w * h local palAt = m + readu32(b, m + 36) + (w // 8) * (h // 8) + 2 if o + off0 + need <= l.size then local p = gpal if hl then p = if palAt - at + 768 <= l.size then Pixels.palette(b, palAt, 256) else nil end local img = Types.Image.new(w, h) if p then Pixels.indexed(b, px, w, 8, img.pixels, w, h, true, p) else for k = 0, need - 1 do writeu32(img.pixels, k * 4, 0xFF000000 + buffer.readu8(b, px + k) * 0x10101) end end img.meta.source = { format = "bsp", miptex = name, palette = p ~= nil } t.image = img end end out[i + 1] = t end end return out, nil end function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, opts: Options?): (Types.Scene?, string?) local q2 = variant == "q2" local maxPixels = opts and opts.maxPixels or 2 ^ 24 local V, E, SE, Fc, TI, P, M = lumps.vertices, lumps.edges, lumps.surfedges, lumps.faces, lumps.texinfo, lumps.planes, lumps.models local nV, nE, nSE, nF = V.size // 12, E.size // 4, SE.size // 4, Fc.size // 20 local tiSize = if q2 then 76 else 40 local nTI, nP = TI.size // tiSize, P.size // 20 local mSize = if q2 then 48 else 64 local nM = M.size // mSize if nF > (opts and opts.maxVertices or 2 ^ 24) then return nil, "limit: too many faces" end local texs: { Tex } = {} local texOf: { number } = {} if q2 then local byName: { [string]: number } = {} for i = 0, nTI - 1 do local name = cstr(b, base + TI.at + i * 76 + 40, 32) local ti = byName[name] if not ti then local w, h, texels = 1, 1, true local wal = opts and opts.wal and opts.wal(name) if wal and buffer.len(wal) >= 40 then w, h, texels = math.max(1, readu32(wal, 32)), math.max(1, readu32(wal, 36)), false end table.insert(texs, { name = name, width = w, height = h, image = nil, texels = texels }) ti = #texs byName[name] = ti end texOf[i + 1] = ti end else local list, err = miptex(b, base, lumps.miptex, variant == "hl", opts and opts.palette, maxPixels) if not list then return nil, err end texs = list for i = 0, nTI - 1 do local mt = readi32(b, base + TI.at + i * 40 + 32) texOf[i + 1] = if mt >= 0 and mt < #texs then mt + 1 else 0 end end local scene = Scene.new() scene.meta.up = "z" local faceModel: { number } = {} local models: { { first: number, count: number, origin: { number } } } = {} for m = 0, nM - 1 do local o = base + M.at + m * mSize local origin = { readf32(b, o + 24), readf32(b, o + 28), readf32(b, o + 32) } local fo = if q2 then 40 else 56 local first, count = readi32(b, o + fo), readi32(b, o + fo + 4) table.insert(models, { first = first, count = count, origin = origin }) if first >= 0 and count >= 0 and first + count <= nF then for f = first, first + count - 1 do faceModel[f + 1] = faceModel[f + 1] or (m + 1) end end end if #models == 0 then table.insert(models, { first = 0, count = nF, origin = { 0, 0, 0 } }) for f = 1, nF do faceModel[f] = 1 end end local batches: { [number]: { [number]: Batch } } = {} local orders: { [number]: { number } } = {} local skipped = 0 for f = 0, nF - 1 do local fo = base + Fc.at + f * 20 local plane, side, firstEdge, numEdges, texinfo = readu16(b, fo), readu16(b, fo + 2), readi32(b, fo + 4), readu16(b, fo + 8), readu16(b, fo + 10) local model = faceModel[f + 1] if not model or numEdges < 3 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then skipped += 1 continue end local poly: { number } = {} local ok = true for k = 0, numEdges - 1 do local se = readi32(b, base + SE.at + (firstEdge + k) * 4) local e = math.abs(se) if e >= nE then ok = false break end local v = if se >= 0 then readu16(b, base + E.at + e * 4) else readu16(b, base + E.at + e * 4 + 2) if v >= nV then ok = false break end local vo = base + V.at + v * 12 table.insert(poly, readf32(b, vo)) table.insert(poly, readf32(b, vo + 4)) table.insert(poly, readf32(b, vo + 8)) end if not ok then skipped += 1 continue end local po = base + P.at + plane * 20 local nx, ny, nz = readf32(b, po), readf32(b, po + 4), readf32(b, po + 8) if side ~= 0 then nx, ny, nz = -nx, -ny, -nz end -- Newell's normal of the polygon as stored local n = #poly // 3 local ax, ay, az = 0, 0, 0 for k = 0, n - 1 do local j = (k + 1) % n local x1, y1, z1 = poly[k * 3 + 1], poly[k * 3 + 2], poly[k * 3 + 3] local x2, y2, z2 = poly[j * 3 + 1], poly[j * 3 + 2], poly[j * 3 + 3] ax += (y1 - y2) * (z1 + z2) ay += (z1 - z2) * (x1 + x2) az += (x1 - x2) * (y1 + y2) end local order: { number } = {} if ax * nx + ay * ny + az * nz >= 0 then for k = 0, n - 1 do order[k + 1] = k end else order[1] = 0 for k = 1, n - 1 do order[k + 1] = n - k end end local tio = base + TI.at + texinfo * tiSize local sx, sy, sz, so = readf32(b, tio), readf32(b, tio + 4), readf32(b, tio + 8), readf32(b, tio + 12) local tx, ty, tz, to = readf32(b, tio + 16), readf32(b, tio + 20), readf32(b, tio + 24), readf32(b, tio + 28) local ti = texOf[texinfo + 1] or 0 local tex = texs[ti] local w, h = if tex then tex.width else 1, if tex then tex.height else 1 local bm = batches[model] or {} batches[model] = bm local bt = bm[ti] if not bt then bt = { tex = ti, pos = {}, nrm = {}, uv = {} } bm[ti] = bt local ol = orders[model] or {} orders[model] = ol table.insert(ol, ti) end local batch: Batch = bt for k = 2, n - 1 do for _, c in { order[1], order[k], order[k + 1] } do local x, y, z = poly[c * 3 + 1], poly[c * 3 + 2], poly[c * 3 + 3] local pp, nn, uu = batch.pos, batch.nrm, batch.uv pp[#pp + 1], pp[#pp + 2], pp[#pp + 3] = x, y, z nn[#nn + 1], nn[#nn + 2], nn[#nn + 3] = nx, ny, nz uu[#uu + 1] = (x * sx + y * sy + z * sz + so) / w uu[#uu + 1] = (x * tx + y * ty + z * tz + to) / h end end end for i, t in texs do local tex: Types.Texture = { name = t.name, image = t.image, uri = if t.image then nil else (if q2 then "textures/" .. t.name .. ".wal" else t.name) } table.insert(scene.textures, tex) local mat = Scene.material(t.name) mat.metallic = 0 local extras: any = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } } mat.extras = extras mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } } :: any table.insert(scene.materials, mat) end local root: Types.Node = { name = "bsp", children = {} } table.insert(scene.nodes, root) scene.roots[1] = 1 for m, mdl in models do local list = orders[m] if list then local mesh: Types.Mesh = { name = if m == 1 then "world" else "*" .. (m - 1), primitives = {} } for _, ti in list do local bt = batches[m][ti] local nv = #bt.pos // 3 local pb, nb, ub, ib = buffer.create(nv * 12), buffer.create(nv * 12), buffer.create(nv * 8), buffer.create(nv * 4) for k = 1, nv * 3 do writef32(pb, (k - 1) * 4, bt.pos[k]) writef32(nb, (k - 1) * 4, bt.nrm[k]) end for k = 1, nv * 2 do writef32(ub, (k - 1) * 4, bt.uv[k]) end for k = 0, nv - 1 do writeu32(ib, k * 4, k) end table.insert(mesh.primitives, { attributes = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) }, indices = Scene.accessor(ib, "u32", 1, nv), mode = "triangles" :: any, material = if ti > 0 then ti else nil, }) end table.insert(scene.meshes, mesh) table.insert(scene.nodes, { name = mesh.name, children = {}, mesh = #scene.meshes, extras = { bsp = { model = m - 1, origin = mdl.origin } } }) table.insert(root.children, #scene.nodes) end end scene.meta.skippedFaces = skipped return scene, nil end return Brush