Geometry (Donkey Kong Country Returns)
|This file format is almost completely documented |
There is an unknown value in the surface header, and the possible vertex formats should be documented.
An important thing to note off the bat is that both CMDL and MREA files are split up into a number of 32-byte-aligned sections; their respective headers both declare a section count, and list the sizes of each section contained in the file. This is required to navigate to different parts of the file, and is required in order to read the geometry data correctly. Sections are generally split based on the type of data contained in them, so one section might contain vertex coordinates, while another contains a surface (submesh) definition. For the purposes of this article, each subheader will mark the start of a new section; use that as your cue to advance to the next one. Check the CMDL or MREA pages for more info on how the sections work.
All data sections appear in the order that they're listed on this page.
For vertex attributes, note that there's no count value present anywhere. If you want a rough count, you can divide the size of that attribute's section by the attribute's size; this approach doesn't account for padding, however.
In the CMDL format, vertex coords can be stored as either floats or signed shorts, depending whether flag 0x20 is set in the header. If they're shorts, then they need to be divided by 0x2000 to get the corresponding float value. Since they're signed numbers, that means that 0 to 0x7FFF correspond to the range 0.0-4.0, and 0x8000 to 0xFFFF correspond to the range -4.0-0.0. In the MREA format, these will always be stored as floats.
In the CMDL format, vertex normals are usually stored as shorts. To get the corresponding float value, read as a signed short and divide by 0x4000. In the MREA format, normals are never packed into shorts and are always stored as floats.
Normally unused, so this section is typically empty and zeroed out.
Float UV Coordinates
These are stored as a sequence of two floats.
Short UV Coordinates
Like the short vertex coordinates, these are stored as signed shorts that should be divided by 0x2000 to get the corresponding float value. The short UVs section has received a fairly significant increase in usage in DKCR compared to the Prime series; in the Prime games, they were generally only used for lightmaps, whereas in DKCR they're often used by pretty much anything.
This section is very small and simple; it simply declares a surface count and then lists the offsets to the end of each surface. Following this section, there'll be one additional section per surface.
|u32||Surface Count||Surface Offsets||Relative to the start of the first surface.|
|Surface sections begin|
There will be one of these sections per surface. Each surface starts with a small 32-byte header; after that, the primitive data begins.
|0x0||float||3||Center Point||Unknown purpose|
|0xE||u16||1||Primitive Table Size||This value is not reliable in the Metroid Prime series, so you probably shouldn't rely on it in DKCR either.|
|0x10||u32||1||Model Pointer Storage||Always 0. This is filled in at runtime with a pointer to the owner model.|
|0x14||u32||1||Next Surface Pointer Storage||Always 0. This is filled in at runtime with a pointer to the next sibling surface.|
|0x18||u16||1||Skin Matrix Bank Index|
|0x1D||u8||1||Visibility Group Index||This value will be 0xFF if this surface isn't in a visibility group. (Always 0xFF in MREA.)|
|0x1E||u8||1||UV Array Index||If 0, this surface uses the float UVs array; if 1, this surface uses the short UVs array|
|0x1F||u8||1||Extra Data Size||Amount of space taken up by extra data. Always 0 on CMDLs, 0x18 on MREAs.|
|0x20||u8||Extra Data Size||Extra Data||Empty on CMDL; contains a bounding box used for depth sorting on MREA.|
|End of surface header; pad to multiple of 32 before primitive data starts|
The primitive data is a standard GX display list. Each primitive begins with a byte that contains the primitive type in the upper 5 bits, and the vertex format setting in the lower 3. After that is a 16-bit vertex count, followed by a series of 16-bit vertex attribute indices.
You'll need to check some settings on the material to read the primitive data properly; the material determines which vertex attributes are present.
There is no primitive count value; you'll need to continue reading data until you hit the end of the primitive table. There are a couple values you can use for reference; either the surface section size, or the surface's end offset. It's not recommended to use the primitive table size value because that value has been shown to be inaccurate in some cases in the Prime series and this may be the case in DKCR as well.
There are 7 primitive types supported by GX, indicated in the upper 5 bits of the flag value. Note that the game only ever actually uses triangles, triangle strips, and triangle fans; however, all of these primitives are supported by GX and are therefore supported by the game, and so they could be used in custom model files.