Introduction
Version 4 of MD4 is the layout the modder Gongo published as
“md4
v4 file spec”, dated 17 January 2006. It grew from the preliminary MD4 export of the
FAKK2 SDK’s max2skl tool; the author notes that the MD4 support of Quake III
Arena 1.32b was not complete. It keeps the IDP4 identifier and the idea of
bone matrices per frame with weighted vertices, and replaces the header, the surfaces and
the levels of detail of version 1.
His converter skl2md4 writes it. All values are little-endian; integers are
32-bit signed, floats are IEEE single precision, and names are NUL-terminated text in
fixed fields.
Structure
A file is a 100-byte header followed by three lists, each found through an offset: the frames, the bones and the surfaces. Each surface carries its own vertices, triangles, bone references and collapse map.
| Limit | Value |
|---|---|
| Bones | 256 |
| Frames | 2048 |
| Surfaces | 32 |
| Triangles in a surface | 8192 |
| Vertices in a surface | 4096 |
| Weights on a vertex | 8 |
The limits on vertices and triangles were raised from 2816 and 5632 with version 4.
Header
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | char[4] | ident | IDP4 |
| 4 | 4 | int32 | version | 4 |
| 8 | 64 | char[64] | name | Model name |
| 72 | 4 | int32 | numFrames | Number of frames |
| 76 | 4 | int32 | numBones | Number of bones |
| 80 | 4 | int32 | numSurfs | Number of surfaces |
| 84 | 4 | int32 | ofsFrames | Offset of the frames, from the start of the file |
| 88 | 4 | int32 | ofsBones | Offset of the bone list, from the start of the file |
| 92 | 4 | int32 | ofsSurfs | Offset of the first surface, from the start of the file |
| 96 | 4 | int32 | ofsEnd | End of the file |
Frames
A frame is 40 + 48 × numBones bytes. Unlike some declarations of version 1,
it has no name.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 24 | float32[3][2] | bounds | Box around the frame: the minimum corner, then the maximum corner |
| 24 | 12 | float32[3] | localOrigin | Middle of the box |
| 36 | 4 | float32 | radius | Distance from the middle to the corners |
| 40 | 48 each | float32[3][4] | bones[] | One matrix per bone: three rows of four |
Bones
numBones records of 40 bytes. The specification calls this part still under
development.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 32 | char[32] | name | Bone name |
| 32 | 4 | int32 | parent | Number of the parent bone in this list |
| 36 | 4 | int32 | flags | Bone flags |
The flags are named MD4_BONE_FLAG_H (head), _U (upper body),
_L (lower body) and _T (tag). The specification gives no numbers
for them. skl2md4 takes them from a configuration file, and a bone passes its
flag to its children unless they have their own.
Surfaces
The numSurfs surfaces follow one another; each starts with a 180-byte
header, and ofsEnd leads to the next. The offsets in a surface header count
from the start of that surface.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | int32 | ident | Surface identifier. The specification gives no value; a writer of the later ZEQ2-Lite layout stores IDP4 |
| 4 | 64 | char[64] | name | Surface name |
| 68 | 64 | char[64] | shader | Shader path; one to a surface |
| 132 | 4 | int32 | shaderIndex | For the engine’s use |
| 136 | 4 | int32 | lodBias | Reserved for the renderer; 0 stands for full detail |
| 140 | 4 | int32 | minLod | Fewest vertices the surface may be reduced to |
| 144 | 4 | int32 | ofsHeader | Leads back to the file header; the ZEQ2-Lite writer stores the negative of the surface’s position |
| 148 | 4 | int32 | numVerts | Number of vertices |
| 152 | 4 | int32 | ofsVerts | Offset of the vertices |
| 156 | 4 | int32 | numTris | Number of triangles |
| 160 | 4 | int32 | ofsTris | Offset of the triangles |
| 164 | 4 | int32 | numBoneRefs | Number of bone references |
| 168 | 4 | int32 | ofsBoneRefs | Offset of the bone references: integers that number bones of the file’s list |
| 172 | 4 | int32 | ofsCollapseMap | Offset of the collapse map |
| 176 | 4 | int32 | ofsEnd | Size of the surface; the next one starts here |
Vertices and weights
A vertex is 36 + 20 × numWeights bytes.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 12 | float32[3] | vertex | Position |
| 12 | 12 | float32[3] | normal | Normal |
| 24 | 8 | float32[2] | texCoords | Texture coordinates |
| 32 | 4 | int32 | numWeights | Number of weights that follow, at most 8 |
| 36 + 20k | 4 | int32 | boneIndex | Entry of the surface’s bone references |
| 40 + 20k | 4 | float32 | boneWeight | 0.01 to 1.0; the weights of a vertex add to 1.0 |
| 44 + 20k | 12 | float32[3] | offset | Position of the vertex in the bone’s space |
A posed position is the sum, over the weights, of boneWeight × the bone’s
matrix applied to offset. skl2md4 keeps at most eight influences
and drops those under 0.01.
Triangles
numTris records of 12 bytes: three vertex numbers within the surface.
Collapse map
The map is one integer for each vertex. Vertices are stored in the order in which they collapse: the highest number goes first, and vertex 0 never does. Entry i names the vertex that vertex i is merged into.
To draw a surface with n vertices, where n is at least
minLod, replace each triangle corner of n or more with its map
entry until it is below n, and leave out the triangles that now name a vertex
twice. The merged corner takes the texture coordinates of the vertex it moved to. One
collapse along an edge removes two triangles. The engine is meant to choose
n between minLod and numVerts.
The skl2md4 converter
Version 1.1 of skl2md4, of January 2006, converts a FAKK2 .skl
animation set to version 4:
skl2md4 <filename.skl> [args]. Its options set paths (/dpath,
/d, /f), a scale (/s), the origin bone
(/org, Bip01 by default), the frame the collapse map is built
from (/base; 0 by default, −1 for the last), an offset (/ofs),
/iflags, /fps, /q and /?.
Bone flags come from a text file named after the model with the extension
.cfg. It opens with BONECONFIG and VERSION 1, then
lists TAGS with a count and bone names, and names the HEAD,
TORSO and LEGS bones with their counts, up to END.
Divergence from specification
- The specification declares
boundsasfloat[3][2], which read as C would give a minimum and maximum for each axis in turn. The ZEQ2-Lite tool and engine, which descend from this layout, write the minimum corner and then the maximum corner. The viewer takes whichever of the two agrees withlocalOriginin more frames, and the second when neither wins. - The specification does not say what surface offsets count from. The ZEQ2-Lite writer counts them from the start of the surface.
- The comment on
boneIndexmakes it an entry of the bone references. ZEQ2-Lite removed the bone references as unused in its version 5. The viewer offers both readings.
In the explorer
A dropped file that starts with IDP4 and version 4 opens as an MD4 version 4
model. Files up to 256 MB are read.
- Structure and tables: the header, bones, frames and their matrices, surfaces, vertices with stored and posed positions, weights, triangles, bone references, collapse maps and shader paths. The tables are paged.
- 3D view: the surfaces posed from the bone matrices. Frames play at ten a second, blended between frames; the file states no rate.
- Options: “Vertices per surface” reduces each surface through its
collapse map, never below
minLod; 0 shows it in full. “Bone numbers in weights” reads them through the bone references or directly as bones of the file. - Textures: when the model is opened from a folder that holds the image,
under the shader path or with
.tga,.jpgor.png, the surface is textured with it, opaque. Otherwise each surface has its own colour. - Shader scripts:
.shaderfiles in the model’s folder or up to two levels below it are read and the surfaces they define are drawn from their stages, as described for version 1. - Checks: the limits above, the range and sum of the weights, parent, bone, vertex and collapse numbers, offsets and counts.
Not described
Known, not yet covered
- Viewer: parts of shader scripts, videos and effects that need a map, are not drawn; see version 1.
Unknown
- Reference: the numbers behind the four bone flags. The specification names them only, and the converter’s source archives are no longer served. The ZEQ2-Lite header declares flags of its own for a later version.
- Reference: the value a surface’s
identholds, and the scale oflodBiasbeyond 0.
Variants
Links
Software
- ZEQ2-Lite – an open game with a model exporter and engine that grew from this layout (its own version 6)
Reference
- md4 v4 file spec – Gongo’s specification, archived
- skl2md4 readme – the converter’s options and bone configuration file, archived
- Gongo’s news page – the changes made with version 4, archived
- md4file.h and zskeleton.c – in ZEQ2-Lite’s “zMesh Exporter” folder: the version history and a writer of the later layout