The Full Wiki

More info on MD2 (file format)

MD2 (file format): Wikis


Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.


From Wikipedia, the free encyclopedia

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 that use this engine such as SiN and Soldier of Fortune. The format is used mostly for player models and static models in maps. Unlike modern character model formats, MD2 animations are animated with keyframes on a per-vertex level; the keyframes are stored in the model and the engine interpolates between them to create a smooth animation.


File format

An MD2 file consists of a single header followed by model data such as texture coordinates and vertex positions.

MD2 Header

Data type Name Description
int ident magic number. must be equal to "IDP2"
int version md2 version. must be equal to 8
int skinwidth width of the texture
int skinheight height of the texture
int framesize size of one frame in bytes
int num_skins number of textures
int num_xyz number of vertices
int num_st number of texture coordinates
int num_tris number of triangles
int num_glcmds number of opengl commands
int num_frames total number of frames
int ofs_skins offset to skin names (each skin name is an unsigned char[64] and are null terminated)
int ofs_st offset to s-t texture coordinates
int ofs_tris offset to triangles
int ofs_frames offset to frame data
int ofs_glcmds offset to opengl commands
int ofs_end offset to end of file

At the offset ofs_st there are (num_st) of this structure:

Data type Name
short s
short t

The texture coordinates are multiplied by the skin's width and height in order to convert to an integer. To recover the floating-point texture coordinates, divide by the dimensions of the skin:

 sfloat = (float)s / skinwidth
 tfloat = (float)t / skinheight

At offset ofs_tris there are num_tris * this structure

 short vertexindex[3]
 short textureindex[3]

These are indexes to each vertex and texture coordinate and tell the engine what vertex comes next. You use these indexes to find the right vertex in a frame to display.

At offset ofs_frames frame data is stored each frame has a header and then a number of vertex and lightnormal indexes after it the frame header is like this:

 float scale[3]
 float translate[3]
 char name[16]

Then there is num_xyz of this structure after it:

 unsigned char v[3]
 unsigned char lightnormalindex

Each vertex is stored as an integer. 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:

 float x = (v[0] * scale[0]) + translate[0]
 float y = (v[1] * scale[1]) + translate[1]
 float z = (v[2] * scale[2]) + translate[2]

The frames scale and translation vector can be found in the frame's header.


This is an example of how to decompress a single frame and display it. Its not in any specific programming language to try and make it easy for every one to interpret. variables have $ before them and their type

 loop while $(int)index is less than $(int)num_tris
   texture_function_s $(float)texture_coordinates[ $(short)triangle[ $(int)index ].textureindex[0] ].s / skinwidth
   texture_function_t $(float)texture_coordinates[ $(short)triangle[ $(int)index ].textureindex[0] ].t / skinheight
   normal_function $(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[0] ].lightnormalindex
   vertex_function_x ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[0] ].v[0] * scale[0]) + translate[0]
   vertex_function_y ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[0] ].v[1] * scale[1]) + translate[1]
   vertex_function_z ($(unsigned char)vertex[ $(short)triangle[ $(int)index ].vertexindex[0] ].v[2] * scale[2]) + translate[2]
   $(int)index = $(int)index + 1
 end loop

See also


The Quake II's MD2 file format Quake2 Source



Got something to say? Make a comment.
Your name
Your email address