Each animation of an MD5 model is its own .md5anim file, and scripted
camera moves are .md5camera files. Both use the same text tokens as the
mesh file: white space between tokens, quoted
names, // and /* */ comments, and vectors in parentheses.
This page follows the loaders in the Doom 3 source code (Anim.cpp and
Camera.cpp).
Animation header
| Keyword | Value | Meaning |
|---|---|---|
MD5Version | integer | Always 10 |
commandline | string | Exporter options; ignored |
numFrames | integer | Frames, at least 1 |
numJoints | integer | Joints, at least 1. To be used with a mesh the count must equal the mesh’s, with the same joint name and the same parent index at every position (see Pairing with a mesh) |
frameRate | integer | Frames per second. Only a negative value is refused, so 0 passes the check, and then the length formula below divides by it |
numAnimatedComponents | integer | Values in each frame block, 0 to 6 × numJoints |
Hierarchy
hierarchy {, one line per joint, }:
"name" parent flags startIndex
| Field | Type | Meaning |
|---|---|---|
| name | string | Joint name |
| parent | integer | Parent joint, which must come earlier (a parent number of i or more for joint i is refused). Only the first joint may be a root, and for it any negative number is accepted (Anim.cpp 222-227); every later joint needs 0 or more. Pairing with a mesh needs exactly −1 for the root, because the mesh’s root parent index is −1 and CheckModelHierarchy compares the numbers |
| flags | integer | Which of the joint’s six values the frames change; only the six bits below are allowed |
| startIndex | integer | Position in each frame block of the joint’s first changing value. Checked to be 0 to numAnimatedComponents − 1 only when numAnimatedComponents is above 0 |
Flags
| Bit | Value | Name | Value replaced |
|---|---|---|---|
| 0 | 1 | ANIM_TX | Position x |
| 1 | 2 | ANIM_TY | Position y |
| 2 | 4 | ANIM_TZ | Position z |
| 3 | 8 | ANIM_QX | Orientation x |
| 4 | 16 | ANIM_QY | Orientation y |
| 5 | 32 | ANIM_QZ | Orientation z |
A joint with flags 63 changes all six; one with 0 keeps its base frame throughout.
Pairing with a mesh
When the game attaches an animation to a model, idMD5Anim::CheckModelHierarchy
(Anim.cpp 888-912) stops with an error unless all of these hold:
- the animation has the same number of joints as the mesh;
- at every index, the animation’s joint name equals the mesh’s joint name at that index (a match by position, not a search by name);
- at every index, the parent index equals the mesh’s parent index (−1 for the root).
Source: id-Software/DOOM-3 neo/game/anim/Anim.cpp, raw.
Bounds
bounds {, then one line per frame, ( minx miny minz ) ( maxx maxy
maxz ), then }: a box around the model in each frame.
Base frame
baseframe {, then one line per joint, ( x y z ) ( qx qy qz ),
then }. These are the joint’s position and orientation relative to its
parent (unlike the mesh file, where joints are in model space). As in the mesh file,
w is not stored and Doom 3 takes w = √|1 − qx² − qy² − qz²|.
Frames
For each frame, in order from 0: frame, the frame number (which must
equal its position), {, exactly numAnimatedComponents floats,
and }.
Nothing checks that a joint’s startIndex plus the number of set flag
bits stays within numAnimatedComponents; only startIndex itself is
range-checked (see the hierarchy table), so a joint can read values past its share of the
frame block, into the next joint’s values or beyond the block. This is read from
Anim.cpp (LoadAnim, lines 205-240), not run.
To rebuild joint j in a frame, start from its base frame values. Then, with
k = the joint’s startIndex, go through the flags in the order
x, y, z, qx, qy, qz: for each flag that is set, replace that value with the frame’s
value number k, and add 1 to k. Compute w, then combine each joint
with its parent’s result to get model space, and use those joints to compute the
vertices as on the mesh page.
Root translation
After reading the frames, LoadAnim treats joint 0 (the first hierarchy
line, the only root) specially, using its startIndex as the start of its
values. If numAnimatedComponents is 0, the total move is zero. Otherwise,
for each of x, y and z whose flag is set on joint 0, it subtracts the base frame’s
value from that value in every frame, and stores the last frame’s result as the total
move (totaldelta); an axis whose flag is not set has a total move of 0.
Whatever the flags, it then sets the base frame position of joint 0 to (0, 0, 0). So
the root’s translation is held as movement relative to its base position, and the
base frame’s root position is not used as a position afterwards. The root’s
orientation is not touched.
Playback length
The animation length in milliseconds is
((numFrames − 1) × 1000 + frameRate − 1) / frameRate in integer arithmetic,
that is the time of numFrames − 1 frame intervals, rounded up. The last frame
is not counted, as the source comment says, “because it would cause a 1 frame pause at
the end”. With frameRate 0 this is an integer division by zero.
Source: idMD5Anim::LoadAnim, Anim.cpp, id-Software/DOOM-3,
read as raw text. The strings and number forms are those of the
mesh page; this loader also forbids
string concatenation. Like the mesh loader, it stops reading after the last
frame block’s closing brace: any tokens after it are ignored. The
lexer rules (keywords by text only, white space and NUL, /*/) are those
listed under Lexer details.
Playback
These are the rules by which the engine turns the stored values into a pose at a given
time. They come from idMD5Anim::ConvertTimeToFrame (Anim.cpp
384-427), GetInterpolatedFrame (630-813), GetSingleFrame
(820 onwards), idSIMD_Generic::BlendJoints (Simd_Generic.cpp
2246-2254), idQuat::Slerp (Quat.cpp 160-205) and
idVec3::Lerp (Vector.cpp 232-240), read as raw text and not
run.
Which frames, and how far between them
- The time is in milliseconds since the animation started.
frameTime = time × frameRate,frameNum = frameTime / 1000(integer division) and the number of whole cycles isframeNum / ( numFrames − 1 ). - The first frame is
frame1 = frameNum mod ( numFrames − 1 )and the second isframe2 = frame1 + 1. The last frame is therefore never a first frame: the cycle hasnumFrames − 1steps and the last frame is only the end of the final step. (The code wrapsframe2to 0 if it reachesnumFrames, butframe1is at mostnumFrames − 2, so that never happens.) - The blend fraction is
( frameTime mod 1000 ) / 1000: the share offrame2;1 −that is the share offrame1. - Special cases: with
numFrames1 or less the pose is frame 0 with no blend. With a time of 0 or less it is frame 0 (frame21, blend 0). If the caller limits the number of cycles and the count is reached, the animation stays on the last frame (frame1=frame2=numFrames − 1).
Blending
- Each joint whose flags are not 0 is built twice, once from
frame1’s values and once fromframe2’s, each on top of the base frame as described under Frames. Orientation is computed with w for both (CalcW). - The two joint positions are blended linearly:
t = t1 + f × ( t2 − t1 ), withfthe blend fraction (Lerpreturns the first value forf≤ 0 and the second forf≥ 1). - The two orientations are blended by spherical linear interpolation (
Slerp). If their four-component dot product is negative, the second is negated first so the short way round is taken, and when they are nearly equal (1 − dotnot above 1e-6) it falls back to a linear mix. The source uses approximations of sin and atan (Sin16,ATan16), so results differ slightly from an exact Slerp. - A joint with flags 0 is not blended: it keeps its base frame. The root’s base-frame position is zero after loading (see Root translation), so the root’s position is the offset in the frame values.
Repeating: totaldelta × cycle count
When the cycle count is above 0, the root joint’s position gets totaldelta × cycleCount added (Anim.cpp 810-812). That is why LoadAnim keeps the last frame’s root movement: each time the animation repeats it starts the next cycle with the root moved on by one more total move, so a walk keeps going forward instead of snapping back.
When numAnimatedComponents is 0
The frame blocks are empty, so there is nothing to read. GetInterpolatedFrame copies the base frame and returns at once (Anim.cpp 644-649), whatever flags the hierarchy sets and whatever the frame, time or cycle count; LoadAnim sets totaldelta to 0 in this case, and the root’s base position is still zeroed (see Root translation). The animation then holds the base frame throughout.
Single frames
GetSingleFrame( framenum, … ) builds a pose with no blending. For frame number 0, or when numAnimatedComponents is 0, it returns the base frame alone and does not read frame 0’s block (Anim.cpp 828-834); a frame 0 whose values differ from the base frame is therefore not what the engine shows for frame 0 here. For other frames it replaces the flagged values from that frame’s block, as in Frames, and adds no totaldelta. Anim_Blend.cpp calls it for a fixed frame requested by number (the 1-based request is passed as frame − 1, at 1767 and 1791) and with 0 for the first animation of a definition (3924). The bounds that go with a frame are the union of the two frames’ bounds with the root’s offset at that moment subtracted (GetBounds, 589-623).
Worked example: with the hierarchy line "head" 1 56 10, flags 56 =
8 + 16 + 32, so only the three orientation values change, and they are values 10, 11
and 12 of each frame block. The position stays at the base frame’s.
Camera files
An .md5camera file moves the game’s camera for cut scenes.
| Keyword | Value | Meaning |
|---|---|---|
MD5Version | integer | Always 10 |
commandline | string | Exporter options; ignored |
numFrames | integer | Frames, at least 1 |
frameRate | integer | Frames per second, at least 1 |
numCuts | integer | Cuts, 0 to numFrames |
cuts { … } | integers | The frame numbers, each from 1 to numFrames − 1, at which a cut occurs; the camera skips those frames when it plays (see Playback) |
camera { … } | one line per frame | ( x y z ) ( qx qy qz ) fov: position, orientation without w, and horizontal field of view in degrees |
Playback
From idCameraAnim::GetViewParms (Camera.cpp 560-651), read as raw text, not run:
- The time since the start gives a frame number and a fraction (
frameRateframes per second):frameTime = ( time − start ) × frameRate,frame = frameTime / 1000,lerp = ( frameTime mod 1000 ) / 1000(Camera.cpp570-572). - If
frameRateequalsUSERCMD_HZ, which is 60 (UsercmdGen.h40), the frame is instead( time − start ) / gameLocal.msec, one camera frame per game tick, andlerpis 0: there is no interpolation, and each tick shows exactly one frame’s position, orientation and field of view (Camera.cpp565-569, and thelerp == 0branch at 635-639). - Cut frames are skipped: going through the cuts in the order listed, the frame number is increased by one for each cut that is less than or equal to it (the loop stops at the first cut greater than the frame, so the
cutslist is expected in ascending order). A cut frame is therefore never a stopping frame of its own; playing goes from frame c − 1 to frame c + 1 for a cut at c. While frame c − 1 is playing, the position still blends towards frame c, as for any frame. - Before the start (a negative frame number) the camera holds frame 0 (
Camera.cpp607-610). - The end comes when the frame number, after the cut adjustment, passes
numFrames− 2, the last frame that has a next frame to blend to (Camera.cpp611). What happens then is set by the camera entity’scyclekey, not by the file (idCameraAnim::Start, 455-458, and 612-621): 0 or absent is taken as 1, a positive count is lowered by one at each end and the camera loops while it is not 0, and a negative count loops for ever. A loop advances the start time by( numFrames − numCuts ) × 1000 / frameRatemilliseconds, one frame interval more than the point where the end was detected. - When the count reaches 0 the camera stops. If stopping activated another camera, that camera’s view is used; otherwise the last frame’s position, orientation and field of view are kept (624-633).
- While a cinematic is being skipped,
GetViewParmsis not called, soidCameraAnim::Think(Camera.cpp495-535) does the end test instead. It uses the frame number without the cut adjustment (frameTime / 1000, orframeTime / gameLocal.msecatUSERCMD_HZ) and ends when it passesnumFrames + numCuts − 2(line 523), where playback ends when the cut-adjusted frame passesnumFrames − 2. With cuts, a skipped camera therefore ends 2 ×numCutsframes later than a played one. It then lowerscycleand loops or stops as playback does, and a one-frame camera never ends there either. - A camera with only one frame (
numFrames1) never ends: its single frame is used for ever, “mainly used for static cams”, as the source comment says. - Between two frames the position and the field of view are interpolated linearly and the orientation by spherical interpolation (
Slerp). fovis the horizontal field of view in degrees (it goes intofov_x).CalcFov(Game_local.cpp2376-2390) derives the vertical angle from it for a 640 by 480 view,fov_y = 2·atan2( 480, 640 / tan( fov_x / 2 ) )in degrees, and then adjusts for ther_aspectRatiosetting; at the default 4:3 settingfov_xis kept as written.
The loader stops after the closing brace of camera { … } (Camera.cpp 402): any tokens after it are ignored, as in the animation and mesh files. The orientation of each camera frame is a quaternion without w, with w computed as on the Rotation convention (positive root, idCQuat::ToQuat, Quat.h 391-394); the camera’s view axes are then q.ToMat3() (Camera.cpp 608, 637, 646). The quaternion is the camera’s own orientation, camera to world, with the same row-vector meaning as a joint’s: R_SetViewMatrix (tr_main.cpp 838-886) writes the three rows of viewaxis as the first three rows of the view matrix, with the translation −origin·viewaxis[i], so it is the view matrix that holds the inverse. Row 0 is the direction the camera looks: the quaternion (0 0 0) looks along +X, and (0 0 sin(θ/2)) looks along (cos θ, −sin θ, 0), so a positive angle about +Z turns the camera to the right (clockwise seen from above).
Coordinate system
The engine uses +Z as up, +X forward and +Y left, a right-handed system, derived from the gravity vector and the view matrix; see Units and axes on the mesh page for the source lines. This applies to joints, bounds and camera positions alike. No unit is stated by any file read.