- MD2 (file format)
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.
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
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] // zExample
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 loopee also
*
id Tech 2 Engine.
*Id Software .Resources
[http://tfc.duke.free.fr/old/models/md2.htm The Quake II's MD2 file format]
[ftp://ftp.idsoftware.com/idstuff/source/quake2.zip Quake2 Source]
Wikimedia Foundation. 2010.