MD2 (file format)
MD2 is a model format used by id Software's id Tech 2 engine and is thus used by Quake II as well as many other games, most of them using this engine, including SiN and Soldier of Fortune. The format is primarily used for animated player models although it can also be used for static models. Unlike more recent character model formats, MD2 animations are achieved via keyframes on a per-vertex level; the keyframes are stored within the model file and the engine interpolates between them to create a smooth animation.
An MD2 file begins with a fixed length header followed by static model data such as texture coordinates. Dynamic data such as vertices and normals are stored within a number of file chunks called frames (or key-frames) which each have their own short headers.
In defining the file structure several data types will be referred to.
int (4 bytes), short (2 bytes), and char (1 byte)
|0||int||ident||Magic number. Must be equal to "IDP2"|
|4||int||version||MD2 version. Must be equal to 8|
|8||int||skinwidth||Width of the texture|
|12||int||skinheight||Height of the texture|
|16||int||framesize||Size of one frame in bytes|
|20||int||num_skins||Number of textures|
|24||int||num_xyz||Number of vertices|
|28||int||num_st||Number of texture coordinates|
|32||int||num_tris||Number of triangles|
|36||int||num_glcmds||Number of OpenGL commands|
|40||int||num_frames||Total number of frames|
|44||int||ofs_skins||Offset to skin names (each skin name is an unsigned char and are null terminated)|
|48||int||ofs_st||Offset to s-t texture coordinates|
|52||int||ofs_tris||Offset to triangles|
|56||int||ofs_frames||Offset to frame data|
|60||int||ofs_glcmds||Offset to OpenGL commands|
|64||int||ofs_end||Offset to end of file|
At the offset ofs_st there are num_st of this structure:
To recover the floating-point texture coordinates as used by common 3D display API's such as OpenGL, divide the texture coordinates by the respective size dimensions of the texture:
sfloat = (float)s / texturewidth tfloat = (float)t / textureheight
At offset ofs_tris there are num_tris of the following structure
short vertexindex short textureindex
These are indices to the vertices and texture coordinates.
At offset ofs_frames frame data is stored, each frame has a short header followed by a number of vertex and surface normal indices, the frame header structure is like this:
float scale float translate char name
Then there is num_xyz of this structure:
unsigned char v unsigned char lightnormalindex
Each vertex is stored as an integer array. To recover the floating-point vertex coordinates, the MD2 reader multiplies each coordinate by the scaling vector for the current frame and then adds the frame's translation vector; these vectors can be found in the frame's header. Alternatively, you can set the translation/scale matrix with model's values before rendering the model's frame (e.g. if using OpenGL); please note that scaling using glScale may de-normalize the surface normals.
This is an example of how to read a single frame and display it, written in pseudocode.
loop while $(int)index is less than $(int)num_tris loop while $(int)j is less than 3 texture_function_s $(float)texture_coordinates[ $(short)triangle[ $(int)index ].textureindex[j] ].s / skinwidth texture_function_t $(float)texture_coordinates[ $(short)triangle[ $(int)index ].textureindex[j] ].t / skinheight normal_function $(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[j] ].lightnormalindex vertex_function_x ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[j] ].v * scale) + translate vertex_function_y ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[j] ].v * scale) + translate vertex_function_z ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[j] ].v * scale) + translate $(int)j = $(int)j + 1 end loop $(int)index = $(int)index + 1 end loop
- The Quake II's MD2 file format
- Quake2 Source
- Loading MD2 models with C++
- 3D model loader in C++, supports MD2 among others
- MD2 Animation on Android, with Java