MD2 (file format)

From Wikipedia, the free encyclopedia

MD2 is a model format used by id's id Tech 2 engine as well as many other games that use this engine such as Half-Life and sin. The format is used mostly for player models and static models in maps. Unlike modern formats, animations are not bone based but frame based the keyframes are stored in the model and the engine must create the frames between them.

Contents

[edit] File format

The MD2 file format is made of two things a header and then the model data such as texture coordinates and vertices.

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 is num_st * this structure

short s
short t

These are compressed texture coordinates to decompress them into a float you devide them by the skinwidth and skinheight intigers like this:

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 indexs to each vertex and texture coordinate and tell the engine what vertex comes next. You use these indexs 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 indexs 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 compressed to decompress them you must multiply each coordinate by the frames scale and then add the frames translation vector onto that. The light normal index reffers to a premade array of light normals. The frames scale and translation vector can be found in the frames header here is an example of how to decompress this.

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


[edit] Example

This is an example of how to decompress a single frame and display it. Its not in any specific programing language to try and make it easy for every one to interperate. 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

[edit] See also

[edit] Resources

The Quake II's MD2 file format
Quake2 Source