model formats (3ds..lwo): comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:53:16 -05:00
1 parent 0bd7b87d2b
commit 3bddd582bc
35 files changed
+233 -697

No files matched your search

+6 -21
View File
@@ -1,13 +1,7 @@
--!strict
-- model/bsp/Brush: Quake 1 (BSP 29), Half-Life (BSP 30) and Quake II (IBSP 38) maps to a Scene: every face (a convex
-- polygon of surface edges) fan-triangulated counter-clockwise about its plane's normal, one primitive per texture
-- per model (model 0 = the world, the others = brush entities "*1", "*2", ... as child nodes), TEXCOORD_0 from the
-- texture vectors (s = v . s.xyz + s.w, divided by the texture size), NORMAL = the plane normal. Textures: Quake /
-- Half-Life embedded mip textures (mip 0) decoded through the palette (Half-Life: each texture's own; Quake: the
-- caller's), textures kept in WAD files as URIs; Quake II textures are external .wal files (URIs; their size from
-- opts.resolve("textures/<name>.wal") when available, else texture coordinates stay in texels: extras.bsp.texels).
-- Written from the Quake specs (bspfile.h of the Quake / Quake II source releases); no code taken.
-- Not supported: lightmaps (light offsets are kept per face in extras), visibility / BSP tree / clip hulls.
-- 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")
@@ -20,7 +14,7 @@ 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: the .wal file of a texture (for its size)
wal: ((name: string) -> buffer?)?, -- Quake II: a texture's .wal file (for its size)
maxPixels: number?,
maxVertices: number?,
}
@@ -32,7 +26,6 @@ local function cstr(b: buffer, at: number, n: number): string
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
end
-- miptex lump (Quake / Half-Life) -> textures
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
@@ -80,9 +73,6 @@ local function miptex(b: buffer, base: number, l: Lump, hl: boolean, pal: buffer
return out, nil
end
-- decode(b, base, size, lumps, variant, opts) -> Scene | nil, message
-- variant "q1" (29), "hl" (30) or "q2" (38); lumps by name: vertices, edges, surfedges, faces, texinfo, planes,
-- models, miptex (q1 / hl)
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
@@ -95,9 +85,8 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
if nF > (opts and opts.maxVertices or 2 ^ 24) then
return nil, "limit: too many faces"
end
-- textures
local texs: { Tex } = {}
local texOf: { number } = {} -- texinfo -> texture index (1-based)
local texOf: { number } = {}
if q2 then
local byName: { [string]: number } = {}
for i = 0, nTI - 1 do
@@ -126,7 +115,6 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
texOf[i + 1] = if mt >= 0 and mt < #texs then mt + 1 else 0
end
end
-- faces of each model
local scene = Scene.new()
scene.meta.up = "z"
local faceModel: { number } = {}
@@ -160,7 +148,6 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
skipped += 1
continue
end
-- the polygon
local poly: { number } = {}
local ok = true
for k = 0, numEdges - 1 do
@@ -189,7 +176,7 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
if side ~= 0 then
nx, ny, nz = -nx, -ny, -nz
end
-- orient counter-clockwise about the normal (Newell's normal of the polygon as stored)
-- 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
@@ -239,7 +226,6 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
end
end
end
-- textures, materials
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)
@@ -250,7 +236,6 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } } :: any
table.insert(scene.materials, mat)
end
-- meshes and nodes
local root: Types.Node = { name = "bsp", children = {} }
table.insert(scene.nodes, root)
scene.roots[1] = 1