Introduction
MD4 is a skeletal model format whose structures and loader are in the Quake III Arena source code, beside the MD3 format the game’s models use. Where MD3 stores every vertex for every frame, MD4 stores a set of bone matrices for each frame and ties each vertex to bones with weights. A model also carries several levels of detail in the one file, which share its frames and bones.
The format is defined by that source: the structures in qfiles.h,
the loader in tr_model.c and the drawing code in
tr_animation.c. This page follows all three. Two related releases declare
MD4 a little differently; see other declarations. The engine
recognises an MD4 file by its first four bytes, not by its name.
Structure
The file is binary and little-endian. Integers are 32-bit and signed, floats are
32-bit IEEE, and names are 64-byte fields (MAX_QPATH) ended by a zero byte.
A header leads to the bone names, the frames and a chain of levels of detail; each level
holds a chain of surfaces.
0HeaderIDP4, version 1, name, counts, offsets100 bytes…Bone names one per bone64 bytes each…Frames bounds and one matrix per bone40 + 48 × bones each…Levels of detail one after anothervaries+0Level header surface count and offsets12 bytes…Surfaces, each with its header, vertices with weights, triangles and bone referencesvaries
Header
md4Header_t, 100 bytes at the start of the file.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | char[4] | ident | IDP4 (49 44 50 34), 0x34504449 read as an integer |
| 4 | 4 | int32 | version | 1. The loader refuses any other value |
| 8 | 64 | char[64] | name | Model name |
| 72 | 4 | int32 | numFrames | Frames, shared by all levels of detail; the loader refuses a model with none |
| 76 | 4 | int32 | numBones | Bones, shared by all levels of detail; the source sets MD4_MAX_BONES at 128 |
| 80 | 4 | int32 | ofsBoneNames | Offset of the bone names; the engine does not read them |
| 84 | 4 | int32 | ofsFrames | Offset of the first frame, from the start of the file |
| 88 | 4 | int32 | numLODs | Levels of detail |
| 92 | 4 | int32 | ofsLODs | Offset of the first level of detail, from the start of the file |
| 96 | 4 | int32 | ofsEnd | End of the file, which is its size; the loader copies this many bytes |
The bone names are numBones fields of 64 bytes. The comment in
qfiles.h gives their shape (char name[MAX_QPATH]). No code in
the release reads them. The other offsets in this header are counted from the start of
the file, and ofsBoneNames is read here in the same way.
Frames and bones
The numFrames frames follow one another at ofsFrames. Each is
a md4Frame_t with numBones bones, so its size is
40 + 48 × numBones bytes, and frame f starts at
ofsFrames + f × that size.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 24 | float32[2][3] | bounds | Minimum then maximum x, y, z of all surfaces of all levels of detail in this frame |
| 24 | 12 | float32[3] | localOrigin | Middle of the bounds, used for culling with a sphere |
| 36 | 4 | float32 | radius | Distance from localOrigin to a corner of the bounds |
| 40 | 48 each | float32[3][4] | bones[] | One matrix per bone, stored row by row: three rows of four floats |
In each row the first three floats are a row of the rotation part and the fourth is the translation along that axis. The source describes the matrices this way: with all bones at identity the mesh shows exactly as it is stored, and in any other frame a bone holds the transformation from its position in the base frame to its position in this one. Between two frames the engine blends all twelve numbers of every bone linearly.
Levels of detail
The first level starts at ofsLODs; each level’s ofsEnd leads
to the next, numLODs in all. Each level has its own complete set of
surfaces. Offsets in a level header are counted from the start of that header
(md4LOD_t, 12 bytes).
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | int32 | numSurfaces | Surfaces in this level |
| 4 | 4 | int32 | ofsSurfaces | Offset of the first surface; each surface’s ofsEnd leads to the next |
| 8 | 4 | int32 | ofsEnd | Distance to the next level of detail |
Surfaces
A surface is one mesh drawn with one shader. Its header (md4Surface_t) is
168 bytes, and its offsets are counted from the start of the surface.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | int32 | ident | Replaced by the engine with its own surface type when loading |
| 4 | 64 | char[64] | name | Surface name; the engine makes it lower case |
| 68 | 64 | char[64] | shader | Shader or image path |
| 132 | 4 | int32 | shaderIndex | Filled in by the engine; the stored value means nothing |
| 136 | 4 | int32 | ofsHeader | A negative number: added to the surface’s position it gives the file header |
| 140 | 4 | int32 | numVerts | Vertices; the renderer stops with an error above 1000 |
| 144 | 4 | int32 | ofsVerts | Offset of the first vertex |
| 148 | 4 | int32 | numTriangles | Triangles; the renderer stops with an error above 2000 |
| 152 | 4 | int32 | ofsTriangles | Offset of the triangles |
| 156 | 4 | int32 | numBoneReferences | Entries in the bone reference list |
| 160 | 4 | int32 | ofsBoneReferences | Offset of the bone reference list, from the start of the surface: int32 numbers naming every bone used by the surface’s weights |
| 164 | 4 | int32 | ofsEnd | Size of the surface: the next one starts here |
The Quake III Arena renderer never reads the bone references. The
model
exporter in the FAKK2 SDK writes MD4 files with this surface header: it puts the list
straight after the triangles and sets ofsBoneReferences to
ofsTriangles + 12 × numTriangles, counted from the surface like
the two offsets before it.
Vertices and weights
Vertices vary in length, so they are read in order: a 24-byte start
(md4Vertex_t) followed by numWeights weights of 20 bytes
(md4Weight_t). The next vertex begins straight after the last weight.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 12 | float32[3] | normal | Vertex normal |
| 12 | 8 | float32[2] | texCoords | Texture coordinates |
| 20 | 4 | int32 | numWeights | Number of weights that follow |
| 24 + 20k | 4 | int32 | boneIndex | The bone of weight k |
| 28 + 20k | 4 | float32 | boneWeight | How much this bone counts |
| 32 + 20k | 12 | float32[3] | offset | The vertex position that this bone’s matrix transforms |
The position of a vertex is the sum, over its weights, of
boneWeight × (rotation × offset + translation), using the
matrix of that bone in the frame. The normal is the sum of
boneWeight × (rotation × normal); the engine does not
re-normalise it.
Triangles
numTriangles entries of 12 bytes at ofsTriangles
(md4Triangle_t): three int32 vertex numbers within the surface, counted
from 0.
Other declarations
Two other releases declare a format with the same IDP4 and version 1. The
surface, level, weight and triangle structures match those above.
96-byte header with named frames
The qfiles.h of
Return
to Castle Wolfenstein and of the
FAKK2
SDK leaves ofsBoneNames out, so the fields after
numBones move up four bytes and the header is 96 bytes:
ofsFrames at 80, numLODs at 84, ofsLODs at 88 and
ofsEnd at 92. Each frame gains a name:
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 40 | – | bounds, localOrigin, radius | As above |
| 40 | 16 | char[16] | name | Frame name. The FAKK2 SDK exporter copies the model name here |
| 56 | 48 each | float32[3][4] | bones[] | One matrix per bone |
The released Return to Castle Wolfenstein renderer declares these structures but has no code that loads MD4.
12-byte vertex field
The FAKK2 SDK header also starts every vertex with
vec3_t vertex, 12 bytes before the normal, so a vertex is 36 +
20 × numWeights bytes:
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 12 | float32[3] | vertex | Declared as a position. Positions are made from the weights; the SDK exporter writes this field without setting it, so it can hold anything |
| 12 | 12 | float32[3] | normal | Vertex normal |
| 24 | 8 | float32[2] | texCoords | Texture coordinates |
| 32 | 4 | int32 | numWeights | Number of weights that follow |
| 36 + 20k | 20 | – | weight k | As above |
The Quake III Arena loader and drawing code carry commented-out lines that step over 12 bytes before each vertex, with a note that this made one set of skeletons work. The code as released does not do it.
Divergence from the header file
- The comment in
qfiles.hsays a weight’sboneIndexcounts into the surface’s bone references. The drawing code intr_animation.cuses it directly as a number in the frame’s bone list and never reads the references. The FAKK2 SDK exporter writes the frame bone number too, although its header carries the same comment. - The renderer draws only the first level of detail.
In the explorer
A dropped file that starts with IDP4 and version 1 opens as an MD4 model. Files up
to 256 MB are read.
- Layouts: both headers, with or without frame names, and vertices with or without the 12-byte field. The layout is told from the offsets and from where the vertices of each surface end; the option “Vertex layout” can force it.
- Structure and tables: the header, bone names, every frame with its bone matrices, every level of detail with its surfaces, and every vertex, weight, triangle and bone reference. The tables are paged; the table of vertices gives each position as posed by frame 0.
- 3D view: one level of detail, chosen with the option “Level of detail” (the first one to start with), posed from the bone matrices. Frames play at ten a second, blended between frames; the file states no rate. A model file holds no list of named animations, so all frames play as one. The option “Bone numbers in weights” reads each number as a frame bone, as the drawing code does, or as an entry of the bone references, as the header comment says.
- Textures: shader paths are shown. 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. Otherwise each surface has its own colour. - Checks: the loader’s version and frame count tests, the limits on bones, vertices and triangles, offsets and counts, and bone and vertex numbers.
Not described
Known, not yet covered
- Viewer: shader scripts (
.shaderfiles) are not read: only an image under the shader path itself is used. Bone names are read from the start of the file, which rests on the point under Unknown.
Unknown
- Reference: what
ofsBoneNamesis counted from. The header file does not say, and no loader or writer of this header that was found in id’s source or the FAKK2 SDK uses it. Phaethon’s unofficial MD4 description leaves the base of MD4 offsets of this kind open too.
Links
Software
- Quake III Arena source code – the released engine, whose renderer loads and draws MD4 models
Reference
- qfiles.h – the structures and limits of MD4 and MD3
- tr_model.c – the loader, with its checks
- tr_animation.c – how bones and weights make the drawn mesh
- Return to Castle Wolfenstein qfiles.h – the 96-byte header and named frames
- FAKK2 SDK qfiles.h – the same, with the 12-byte vertex field
- max2skl.cpp – the FAKK2 SDK exporter, which writes MD4