Thu, Sep 24, 2009

iPhone MD2 example

The first major step when developing a 3D app is finding a way of importing models created in external editing software. There are hundreds of formats to choose from each with their own pro's and con's.

For most 3D apps on the iPhone all that is required is the mesh (vertices, triangles, texture coordinates, normals) so the simpler the format the better.

Using a format that is well supported across a variety of editors and platforms is also essential for an optimal work flow especially if coders and artists are in different cities/states/countries.

The MD2 file format was created by id Software in 1997 for non-level models in Quake II. It is a very light weight file format containing only the mesh. Animation is handled via keyframes with up to 512 frames stored in a file. Vertices are stored in a compressed format while normals are stored as an integer lookup into a pre-calculated table.

It proves to be a great fit for the iPhone in most instances.

Using my KEMD2Object class you can easily load, render and animate MD2 files. Animation is handled by filling a KEMD2Animation struct with the start frame, end frame and FPS of the animation and setting the animation property to that struct.

For animated MD2's make sure to call the tick method each frame passing in the time in seconds since the last call as this is where the keyframe interpolation occurs.