www.xbdev.net xbdev - software development
Friday December 14, 2018
home | about | contact | Donations

MD2 File Format
Author bkenwright@xbdev.net

Lets just see how the information is arranged within the file.


md2 header (68 bytes).

Name

Number of Bytes

Description

magic 4 This is used to identify the file.
version 4 This version number of the file (Must be 8).
skinWidth 4 The skin width in pixels.
skinHeight 4 The skin height in pixels.
frameSize 4 The size in bytes the frames are.
numSkins 4 The number of skins associated with the model.
numVertices 4 The number of vertices (constant for each frame).
numTexCoords 4 The number of texture coordinates.
numTriangles 4 The number of faces (polygons).
numGlCommands 4 The number of gl commands.
numFrames 4 The number of animated frames.
offsetSkins 4 The offset in the file for the skin data.
offsetTexCoords 4 The offset in the file for the texture data.
offsetTriangles 4 The offset in the file for the face data.
offsetFrames 4 The offset in the file for the frames data.
offsetGlCommands 4 The offset in the file for the gl commands data.
offsetEnd 4 The end of file offset.

The good thing of the md2 file structure is that right up front you know all the information and locations of your 3d file data, instead of searching throught the file using chunks etc.

Lets analyse what we have above.

->Numbers<-

numSkins - this simply tells us how many different textures there are. E.g. there could be 3 different bitmap textures that we need to load in.

numVertices - Well it should speak for itself, it tells us how many x,y and z points there are. ( not that it means the total number of unique x,y, z points.... so it means there there would be 60 for example unique x,y, z points which will be refered to later ).

numTexCoords - Tells us how many tu, tv coordinates there are.

numTriangles - Again each triangle is made up of three sides, this tells us how many triangles ( or faces ) there are.

numGlCommands - OpenGL specific, we dont' need to know this.

numFrames - There can be hundreds of frames, this tells us how many there are... e.g. there could be 100 frames, of which we will find out later that 10 maybe for walking, 20 for flying etc.

We should be able to say how much memory we needed now...so we could allocate memory using the new or malloc functions then read in the data using the offsets to it in the file.

->Data Values<-

All the offset values, e.g. offsetSkins, offsetTriangles etc, is the offset from the start of the file to the actual beginning of the data. So you could use the C function as follows :

fseek( filePointer, offsetSkins, SEEK_SET );
fread( pSkins, sizeof(md2Skin), numSkins, filePointer);

So how is the data ordered I hear you ask? Well I'm going to show you now.

offsetSkins - data is ordered as 64 bytes (e.g. a null terminated string, char md2skin[64]; ). So you would have numSkins, each representing a string.

struct
{
char strName[64];
};  

offsetTexCoords - an array of 4 bytes, where first 2 bytes is tu, and the next 2 bytes tv. e.g. 2 short integers.

struct
{
signed short int tu;
signed short int tv;
};  

offsetTriangles - each element is 24 bytes, where the first 12 bytes is 3 vertex's of 4 bytes each, and the next three 4 bytes are for the normals...I think this can best be seen by doing a quick struct to show how each element would be represented:

struct md2Triangle
{
float vertex[3];
float normal[3];
};

offsetFrames - this is an array of frame values, each frame value is described as follows:

struct
{
float scale[3];
float translate[3];
char name[16];
byte vertexIndex[3];
byte normalIndex[3];
};

 

Now thats all there is to the md2 file format, once you get in there and start using it you'll soon see that its really easy to use. Just remember the Quake 2 file format is owned by ID Software - and this tutorial is used as a teaching tutorial.

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.