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. The purpose of these sections is described in the table below. 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.
|FVTX||Describes an array of array of vertices which can have a number of attributes including position, normal, colour and texture coordinates.|
|FMAT||Describes the properties of a surface necessary to draw it including texture information.|
|FSKL||Describes a skeleton for the model which may be used in animations.|
|FSHP||Selects triples of vertices from one FVTX section to draw triangles between. Also specifies the FMAT material and the FSKL attachment of the triangles.|
|Parameter||Sets a named paramter for the model.|
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. Points to just after the last string in the string table.|
|0x0c||4||FSKL offset. Offset to the FSKL Header.|
|0x10||4||FVTX array offset. Offset to the first element in an array of FVTX Headers.|
|0x14||4||FSHP index group offset. Offset to an index group which contains named pointers to FSHP Headers.|
|0x18||4||FMAT index group offset. Offset to an index group which contains named pointers to FMAT Headers.|
|0x1c||4||Parameter index group offset. Offset to an index group which contains named pointers to parameter values. May be 0 if no parameters exist (common).|
|0x20||2||FVTX count: number of FVTX sections in the FVTX array.|
|0x22||2||FSHP count: number of FSHP sections in the the FSHP index group.|
|0x24||2||FMAT count: number of FMAT sections in the the FMAT index group.|
|0x26||2||Parameter count: number of parameters in the Parameter index group.|
|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 arrays of arbitrary elements 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 elements 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 (position, normal, colour, etc) in the attribute array.|
|0x05||1||Buffer count: number of attribute buffers in the buffer array.|
|0x06||2||Section index: index into FVTX array of this entry.|
|0x08||4||Number of vertices. This is also the number of elements in each buffer.|
|0x0c||1||Unknown; values seen are between 0 and 4 (normally 0).|
|0x0d||3||Unknown always 0.|
|0x10||4||Attribute array offset. Offset to the first element in the Attribute array.|
|0x14||4||Attribute index group offset. Named pointers to the members of the Attribute array to allow named lookups.|
|0x18||4||Buffer array offset. Offset to the first element in the Buffer array.|
|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 element in the buffer.|
|0x08||4||Format of the attribute's data in the buffer.|
|0xc||End of attribute|
The following values have been seen for the Format field. This list is not exhaustive.
|0x00000004||2||Two 8 bit values representing numbers between 0 and 1.|
|0x0000020a||4||Four signed 8 bit values.|
|0x0000020b||4||Unknown. Used for normals.|
|0x00000007||4||Two 16 bit values representing numbers between 0 and 1.|
|0x0000080d||8||Two 32 bit floating point values.|
|0x0000080f||8||Four 16 bit floating point values.|
|0x00000811||12||Three 32 bit floating point values.|
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|
The model may have one or more FSHP sections which describe the one polygon (e.g. the road of a track). The FSHP section consists of an 0x40 byte header, which references an FVTX section and indices of the polygons to draw. A basic FSHP amounts to a list of indices into the corresponding FVTX indicating which points to draw triangles between. The FSHP also references an FMAT to describe the material of the triangles, and an FSKL to indicate the bone structure of the model used in animation.
Every FSHP begins with an 0x40 byte header. This references other data structures which describe the polygon.
|0x00||4||"FSHP" section identifier, ASCII string.|
|0x04||4||Polygon name offset.|
|0x08||4||Unknown always 2.|
|0x0c||2||Section Index or FVTX Index. In every Mario Kart 8 model these numbers are the same.|
|0x0e||2||FMAT Index. Index of the material for this polygon.|
|0x10||2||FSKL Index of the bone for this polygon.|
|0x12||2||Section Index or FVTX Index. In every Mario Kart 8 model these numbers are the same.|
|0x14||2||FSKL Index Array Count. Often zero, Unknown purpose but seems to be related to FSKL.|
|0x16||1||Unknown: If FSKL Index Array is present this tends to be non-zero. Zero otherwise.|
|0x17||1||Drawing Command count. Number of elements in the array.|
|0x18||4||Drawing Extent Tree Node count.|
|0x1c||4||Unknown floating point.|
|0x20||4||FVTX offset to the vertex buffer for this polygon.|
|0x24||4||Drawing Command offset. Offset to the first element in the array.|
|0x28||4||FSKL Index Array Offset. Each element of this array is a 16 bit index.|
|0x2c||4||Unknown always 0.|
|0x30||4||Drawing Extent Tree Nodes offset.|
|0x34||4||Drawing Extent Tree Ranges offset.|
|0x38||4||Drawing Extent Tree Indices offset.|
|0x3c||4||Unknown always 0.|
|0x40||End of FSHP header|
A Drawing Command is a repeating 0x1c byte structure which are stored in a sequential array with location and size specified by the FSHP Header
|0x00||4||Unknown Always 4.|
|0x04||4||Unknown Always 4.|
|0x08||4||Count of points drawn in all Drawing Extents.|
|0x0c||2||Drawing Extent count. The number of elements in the array.|
|0x0e||2||Unknown Always 0.|
|0x10||4||Drawing Extent offset. Offset to the first element in the array.|
|0x14||4||Index Buffer offset.|
|0x18||4||Skip Vertices: the number of elements to skip in the corresponding FVTX buffer.|
|0x1c||End of Drawing Command|
A Drawing Extent is a repeating 0x8 byte structure describing an offset and a count in the corresponding Index Buffer. Drawing Extents are stored in a sequential array with location and count indicated by the corresponding Drawing Command. This allows multiple separate drawing commands to be issued using the same Index Buffer. In many cases, a single extent is used which simply draws the entire Index Buffer.
|0x00||4||Offset into index buffer in bytes.|
|0x04||4||Count of the points to draw.|
|0x08||End of Drawing Extent|
An Index Buffer is an 0x18 byte structure which describes a list of 16 bit values which are used to index the FVTX section when drawing the polygon. Index Buffers are referenced by Drawing Commands. The format is very similar to the FVTX Buffer format, which may indicate they are instances of the same structure.
|0x00||4||Unknown Always 0.|
|0x04||4||Size of index data in bytes.|
|0x08||4||Unknown Always 0.|
|0x0c||2||Unknown Always 0.|
|0x0e||2||Unknown Always 1.|
|0x10||4||Unknown Always 0.|
|0x14||4||Index data offset.|
|0x18||End of Index Buffer|
Drawing Extent Tree
The Drawing Extent Tree is some sort of tree structure that occurs in all FSHP sections. In all Mario Kart 8 files, when the FSHP has multiple Drawing Commands, the Drawing Extent Tree is a trivial one node tree. The Drawing Extent Tree may be used to control which Drawing Extents need to be drawn, for example as part of a dynamic level of detail system. The true workings and purpose of the structure are however unknown.
The Drawing Extent Tree Node is a repeating 0xc byte structure. The nodes form a binary tree. The FSHP header contains a pointer to node array, as well as specifying its length. The root node is the first node in this array. Each node specifies a range of Drawing Extents, with the root node always covering all nodes, and the children of each node always selecting disjoint subsets of that node's range.
|0x00||2||Left Child index. The current node's index if it has no left child.|
|0x02||2||Right Child index. The current node's index if it has no right child.|
|0x04||2||Unknown always the same as Left Child index.|
|0x06||2||Next Sibling index. Always the current node's parent's right child (the current node's index if it has no right child).|
|0x08||2||Drawing Extent Index selects a range of Drawing Extents along with the count.|
|0x0a||2||Drawing Extent Count selects a range of Drawing Extents along with the index.|
|0x0c||End of Node|
The Drawing Extent Tree Range is a repeating 0x18 byte structure. The purpose is unknown. There are exactly as many Ranges as Nodes. The range appears to contain two 3D positions, which are not necessarily distinct.
|0x00||12||Unknown 3D position (x,y,z float).|
|0x0c||12||Unknown 3D position (x,y,z float).|
|0x18||End of Range|
The Drawing Extent Tree Index array is an array of 16 bit values which appear to be the index of the leaf node of the Drawing Extent Tree which contains the corresponding Drawing Extent. Therefore this array has length equal to the number of Drawing Extents in the FSHP section.