FMDL (File Format)
The FMDL model format is a format for models which appears as a subfile of a BFRES file in the 0th file group. Typically a BFRES file has one FMDL but some have none and some have more than one.
An FMDL file begins with an FMDL header. This is then followed by a number of sections pointed to by the header, many of which can occur multiple times. Unless otherwise noted, all offsets in BFRES files are relative to themselves, not the beginning of the file which is more common in other formats.
Every FMDL begins with an 0x2c byte FMDL header.
|0x00||4||"FMDL" file identifier, ASCII string.|
|0x04||4||File name offset (without file extension).|
|0x08||4||End of BFRES string table offset.|
|0x10||4||FVTX array offset.|
|0x14||4||FSHP index group offset.|
|0x18||4||FMAT index group offset.|
|0x1c||4||Parameter index group offset. May be 0 if no parameters exist (common).|
|0x20||2||FVTX count: number of FVTX sections.|
|0x22||2||FSHP count: number of FSHP sections.|
|0x24||2||FMAT count: number of FMAT sections.|
|0x26||2||Parameter count: number of parameter sections.|
|0x28||4||Unknown possibly something like an unused face count.|
|0x2c||End of FMDL header|
The model may have one or more FVTX sections which describe the vertices for one polygon (e.g. the road of a track). The section describes a series of buffers and attributes. Buffers are arbitrary pieces of data which contain the actual values of the vertices and are passed directly to the graphics card. Attributes are properties of vertices (e.g. position, texture coordinate, vertex colours) and they describe the format and layout of the data in the buffers.
Each FVTX section has a 0x20 byte header. These headers are stored sequentially in an array at the offset given in the FMDL header. The length of this array is also given in that header.
|0x00||4||"FVTX" section identifier, ASCII string.|
|0x04||1||Attribute count: number of different attributes in section (position, normal, colour, etc).|
|0x05||1||Buffer count: number of attribute buffers.|
|0x06||2||Section index: index into FVTX array of this entry.|
|0x08||4||Number of vertices.|
|0x0c||1||Unknown; values seen are between 0 and 4 (normally 0).|
|0x0d||3||Unknown always 0.|
|0x10||4||Attribute array offset.|
|0x14||4||Attribute index group offset.|
|0x18||4||Buffer array offset.|
|0x1c||4||Padding, always 0.|
|0x20||End of FVTX header|
The attributes of each FVTX section are stored as a simple repeating 0xc byte structure. These are stored in an array with location and length specified by the FVTX header. They are also referenced by the attribute index group, presumably to allow both index and name based lookup of attributes.
|0x00||4||Attribute name offset.|
|0x04||1||Buffer index of the buffer containing this attribute.|
|0x05||3||Buffer offset of the attribute in each structure in the buffer.|
|0x08||4||Format of the attribute's data in the buffer.|
|0xc||End of attribute|
Based on the some of the shader code in /vol/content/mapobj/PackunMusic/PackunMusic.bfres the following naming convention seems to be used for attributes.
|Attribute Name||Friendly Name||Description|
|_p0||position0||The position of the vertex.|
|_n0||normal0||The normal of the vertex as used in lighting calculations.|
|_t0||tangent0||The tangent of the vertex as used in advanced lighting calculations.|
|_b0||binormal0||The binormal of the vertex as used in advanced lighting calculations.|
|_u0||uv0||Texture coordinates used for texture mapping.|
|_c0||color0||Vertex colours used for simple shadow mapping.|
The buffers of each FVTX section are stored as a repeating 0x18 byte header structure which references the actual data stored later in the BFRES file. The headers are stored in an array with location and length specified by the FVTX header.
|0x00||4||Unknown always 0.|
|0x04||4||Size of the buffer in bytes.|
|0x08||4||Unknown always 0.|
|0x0c||2||Stride: the size of each element in the buffer.|
|0x0e||2||Unknown always 1.|
|0x10||4||Unknown always 0.|
|0x18||End of buffer|