MD2
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. MD2 is the file format used by Quake II for its 3D models. The format supports frame-based vertex animation and is easy to use once you understand the quirks of the encoding.
[edit] File Format Information[edit] HeaderThe MD2 header consists of 17 integer values:
Offset Name Desc
0 ID Always 0x32504449 ('IDP2')
4 Version Always 8
8 TexWidth Texture width in pixels, normally 256
12 TexHeight Texture height in pixels, normally 256
16 FrameSize The size of each frame in bytes
20 nTextures The number of textures
24 nVertices The number of vertices in each frame
28 nTexCoords The number of UV coordinates in the model
32 nFaces The number of triangles in the model
36 nGLCommands The number of GL instructions (4 bytes each) in the GLCmd list
40 nFrames The number of animation frames in the file
(All the following offsets are in bytes from the start of the file)
44 TexOffset An offset to the texture name(s)
48 UVOffset An offset to the UV coordinates
52 FaceOffset An offset to the face data
56 FrameOffset An offset to the first animation frame
60 GLCmdOffset An offset to the GLCmd list
64 EOFOffset An offset to the end of the file
[edit] Texture NamesTexture names are stored as 64 byte strings, the path is relative to the Quake II game directory. These 'skins' are normally PCX files. [edit] Texture CoordinatesTexture coordinates are stored as pairs of short integers. In order to use them with OpenGL, you should divide the first by the texture width and the second by the texture height. This will yield the more usual UV coordinates in the range 0.0f - 1.0f.
typedef struct
{
short U; // Divide by TexWidth
short V; // Divide by TexHeight
}MD2UVCoord;
[edit] Face DataMD2 only supports triangles for face data, these are stored as references into the vertex and UV arrays. Each triangle constists of six short integers, the first three are the vertex indices, the second three are the UV coordinate indices. The following structure may be useful in extracting these values:
typedef struct
{
short Point[3];
short UV[3];
}MD2Face;
[edit] Vertex and Animation Frame DataHere's the tricky part. MD2 animation works by providing complete vertex arrays for each frame, while the face offsets into the vertex and UV arrays remain the same. Frames consist of a header which provides a scale and offset value which must be applied to each vertex value in order to convert it into object coordinates. The header looks like this:
typedef struct
{
float Scale[3];
float Translate[3];
char Name[16];
}MD2FrameHeader;
The Name field is the name of the frame, these normally follow the form "Walk001","Walk002",etc. The vertex data follows the header, each vertex has the following format:
typedef struct
{
byte Vtx[3];
byte Normal;
}MD2Vtx;
The Normal value is an index into a lookup table of normals held by Quake II. This can be disregarded as normals can be calculated once the triangle data has been loaded. The vertices are compressed, in order to uncompress them for rendering you need to apply the scale and translation vectors from the frame header: vertex[0] = (frame.Scale[0] * Vtx[0]) + frame.Translate[0]; vertex[1] = (frame.Scale[1] * Vtx[1]) + frame.Translate[1]; vertex[2] = (frame.Scale[2] * Vtx[2]) + frame.Translate[2]; |


