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.

File format

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)

MD2 Header

Offset Data type Name Description
0intidentMagic number. Must be equal to "IDP2"
4intversionMD2 version. Must be equal to 8
8intskinwidthWidth of the texture
12int skinheightHeight of the texture
16int framesizeSize of one frame in bytes
20int num_skinsNumber of textures
24int num_xyzNumber of vertices
28int num_stNumber of texture coordinates
32int num_trisNumber of triangles
36int num_glcmdsNumber of OpenGL commands
40int num_framesTotal number of frames
44int ofs_skinsOffset to skin names (each skin name is an unsigned char[64] and are null terminated)
48int ofs_stOffset to s-t texture coordinates
52int ofs_trisOffset to triangles
56int ofs_framesOffset to frame data
60int ofs_glcmdsOffset to OpenGL commands
64int ofs_endOffset to end of file

At the offset ofs_st there are num_st of this structure:

Data type Name
shorts
shortt

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[3]
 short textureindex[3]

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[3]
 float translate[3]
 char name[16]

Then there is num_xyz of this structure:

 unsigned char v[3]
 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.

Example

This is an example of how to read a single frame and display it, written in pseudocode.

 get struct triangle[], int num_tris, unsigned char vertex[], float scale[3], float translate[3]

 int index=0
 int j=0
 short tri

 loop while index < num_tris
   loop while j < 3

     tri = triangle[ index ].textureindex[j]

     texture_function_s texture_coordinates[ tri ].s / skinwidth
     texture_function_t texture_coordinates[ tri ].t / skinheight

     tri = triangle[ index ].vertexindex[j]

     normal_function vertex[ tri ].lightnormalindex

     vertex_function_x (vertex[ tri ].v[0] * scale[0]) + translate[0]
     vertex_function_y (vertex[ tri ].v[1] * scale[1]) + translate[1]
     vertex_function_z (vertex[ tri ].v[2] * scale[2]) + translate[2]

     j = j + 1
   end loop

   index = index + 1
 end loop

See also

Resources

This article is issued from Wikipedia - version of the Wednesday, February 10, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.