model formats (3ds..lwo): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
3bddd582bc
35 files changed
+233
-697
No files matched your search
@@ -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
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
--!strict
|
||||
-- model/bsp/Quake3: Quake III Arena maps (IBSP version 46; Return to Castle Wolfenstein 47 has the same layout) to a
|
||||
-- Scene: polygon and mesh faces (meshvert triangles, reversed to counter-clockwise), biquadratic patches
|
||||
-- tessellated (opts.patchLevel subdivisions per 3x3 control patch, default 4), one primitive per shader and lightmap
|
||||
-- per model (model 0 = the world, the others "*1", ...), POSITION / NORMAL / TEXCOORD_0 (surface) / TEXCOORD_1
|
||||
-- (lightmap) / COLOR_0 (vertex colours, unorm8); shaders as texture URIs; the 128 x 128 RGB lightmaps as
|
||||
-- texture.image (material.extras.bsp.lightmap = its texture index, texCoord 1). Billboards (flares) are skipped.
|
||||
-- Written from the Unofficial Quake 3 Map Specs (Kekoa Proudfoot); no code taken.
|
||||
-- model/bsp/Quake3: Quake III Arena maps (IBSP 46; RTCW 47 has the same layout) -> Scene, from the Unofficial Quake 3
|
||||
-- Map Specs (Kekoa Proudfoot; no code taken). Lightmaps are 128 x 128 RGB; billboards (flares) are skipped.
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Quake3 = {}
|
||||
@@ -17,19 +12,18 @@ local writef32, writeu32, writeu8 = buffer.writef32, buffer.writeu32, buffer.wri
|
||||
export type Lump = { at: number, size: number }
|
||||
export type Options = { patchLevel: number?, maxVertices: number? }
|
||||
|
||||
type Batch = { shader: number, lightmap: number, verts: { number } } -- vertex records (indices into the vertex lump, or -1 - k for patch vertices)
|
||||
type Batch = { shader: number, lightmap: number, verts: { number } } -- indices into the vertex lump, or -1 - k for patch vertices
|
||||
|
||||
local function cstr(b: buffer, at: number, n: number): string
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
|
||||
end
|
||||
|
||||
-- decode(b, base, lumps, opts) -> Scene | nil, message
|
||||
function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts: Options?): (Types.Scene?, string?)
|
||||
local T, M, VX, MV, F, LM = lumps.textures, lumps.models, lumps.vertexes, lumps.meshverts, lumps.faces, lumps.lightmaps
|
||||
local nT, nM, nV, nMV, nF, nLM = T.size // 72, M.size // 40, VX.size // 44, MV.size // 4, F.size // 104, LM.size // 49152
|
||||
local level = math.clamp(math.floor(opts and opts.patchLevel or 4), 1, 16)
|
||||
local maxV = opts and opts.maxVertices or 2 ^ 24
|
||||
-- patch vertices: 12 floats + 4 colour bytes each, appended as negative ids
|
||||
-- patch vertices: 12 floats + 4 colour bytes each
|
||||
local extra: { number } = {}
|
||||
local function vertex(i: number): { number }
|
||||
local o = base + VX.at + i * 44
|
||||
@@ -92,7 +86,6 @@ function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts:
|
||||
local L = level
|
||||
for py = 0, (h - 1) // 2 - 1 do
|
||||
for px = 0, (w - 1) // 2 - 1 do
|
||||
-- the 3x3 control points of this sub-patch
|
||||
local ctrl: { { number } } = {}
|
||||
for j = 0, 2 do
|
||||
for i = 0, 2 do
|
||||
@@ -127,7 +120,7 @@ function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts:
|
||||
for gx = 0, L - 1 do
|
||||
local a, c2 = grid[gy * (L + 1) + gx + 1], grid[gy * (L + 1) + gx + 2]
|
||||
local d, e = grid[(gy + 1) * (L + 1) + gx + 1], grid[(gy + 1) * (L + 1) + gx + 2]
|
||||
-- counter-clockwise for Quake's clockwise-front control grids
|
||||
-- Quake's control grids are clockwise-front
|
||||
for _, v in { a, d, c2, c2, d, e } do
|
||||
table.insert(tris, v)
|
||||
end
|
||||
@@ -166,7 +159,6 @@ function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts:
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
-- shaders, lightmaps
|
||||
for i = 0, nT - 1 do
|
||||
local name = cstr(b, base + T.at + i * 72, 64)
|
||||
table.insert(scene.textures, { name = name, uri = name })
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
--!strict
|
||||
-- model/bsp: compiled game maps read into a Scene: Quake 1 (BSP 29) and Half-Life (BSP 30), Quake II (IBSP 38)
|
||||
-- through model/bsp/Brush, Quake III Arena / RTCW (IBSP 46 / 47) through model/bsp/Quake3. Each model of the map is a
|
||||
-- mesh (model 0 "world", brush entities "*1", "*2", ... as nodes), one primitive per texture (and Quake III
|
||||
-- lightmap). The entities lump is parsed into meta.entities = { { classname = ..., origin = ..., ... } } (strings as
|
||||
-- stored) with the text in meta.source.entities. Quake textures need the game's palette (gfx/palette.lmp, not
|
||||
-- redistributable: never built in): opts.palette, else opts.resolve / opts.files["gfx/palette.lmp"], else grey.
|
||||
-- Quake II .wal sizes come from opts.resolve("textures/<name>.wal") / opts.files. Units are map units, z up.
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29), Half-Life (BSP 30), Quake II (IBSP 38), Quake III / RTCW
|
||||
-- (IBSP 46 / 47). Quake textures need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up.
|
||||
-- Not supported: Source engine VBSP, Quake 1/2 lightmaps, visibility and collision data, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
@@ -28,10 +23,10 @@ local Bsp = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
palette: (buffer | string)?, -- Quake 1: the 768-byte palette
|
||||
palette: (buffer | string)?, -- Quake 1: 768 bytes
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
patchLevel: number?, -- Quake III patch subdivisions (default 4)
|
||||
patchLevel: number?, -- default 4
|
||||
maxPixels: number?, -- default 2^24
|
||||
maxVertices: number?, -- default 2^24
|
||||
}
|
||||
@@ -55,7 +50,6 @@ function Bsp.sniff(r: Bytes.Reader): number
|
||||
return if v == 38 or v == 46 or v == 47 then 1 else 0.3
|
||||
end
|
||||
if v == 29 or v == 30 then
|
||||
-- no magic: check that the lump directory fits the file
|
||||
local size = r:remaining()
|
||||
for i = 0, 14 do
|
||||
local o, n = buffer.readi32(r.buf, r.pos + 4 + i * 8), buffer.readi32(r.buf, r.pos + 8 + i * 8)
|
||||
@@ -69,7 +63,6 @@ function Bsp.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the entities text -> { { key = value } }
|
||||
local function entities(text: string): { { [string]: string } }
|
||||
local out: { { [string]: string } } = {}
|
||||
for block in string.gmatch(text, "%b{}") do
|
||||
|
||||
Reference in new issue
Block a user