Converting Maya exported files to Binary files for loading into the game.
This assignment is focused on creating a binary mesh format and converting the existing mesh files which are exported from Maya in our file format to this binary file format during the build time, so that we can load the binary files during runtime.
Advantages of Binary Files over Human Readable files.
- Binary files are smaller compared to human readable files: The size taken by binary files on the disk is many times smaller than comparative human readable files. This difference can be huge when the source human readable file is very big. Below is the table outlining the difference in sizes between binary and human readable files.
Name | Human Readable File Size | Binary file size | Difference |
Plane | 459 Bytes | 80 Bytes | 379 Bytes |
Donut | 154KB | 29.6KB | 124.4KB |
Gas Can | 1.37MB | 262KB | 1108 KB |
Lambo | 2.12MB | 403KB | 1717 KB |
Helix (Index count 62436) | 4.15MB | 772KB | 3378 KB |
- Faster load and read times from Disk : Since binary files are small and contains information in binary format, the time taken to load the file and the time taken to read the file after loading are very small compared to Human readable files where we are using Lua to convert data from the file into binary format to use in the game.
Name | Time to load and read in Lua | Time to load and read binary files | Difference ( in Seconds) |
Plane | 0.002427 | 0.000095 | 0.002332 |
Chair | 0.006533 | 0.000081 | 0.006452 |
Helix | 0.117267 | 0.000528 | 0.116739 |
As seen from the data above, there is a small but considerable difference between load times using Lua and binary files even for simple meshes like a plane. The time taken to load the Human readable mesh also increases with increasing mesh complexity, but the times for binary files are still considerably less.
Building different Mesh files for different Platforms:
As discussed in previous posts, Direct3D and OpenGL use opposing winding order to render a given triangle. Since its easier to have a common winding order across the engine, mine uses the OpenGL one as the default and until now I have been switching the winding order when initializing the mesh. Even though this process still works for binary files, to take the advantage of the gain in speed, I moved the part of switching the winding order to the place where I am building the binary file. This allows me to have two different binary files for the different platforms and I do not have to worry about switching the winding order during loading the mesh.
Order of data in Binary files:
The above picture shows the plane mesh in the Binary file format. The red highlighted bytes are the Index Count, the purple is the Vertex Count, Yellow is the Index array and Blue is Vertex Array. The light blue underlined part is the vertex positions in the vertex array and the brown underlined one is the position of color data of a vertex.
Sizes of different parts :
Index count: uint16_t = 2 Bytes
Vertex Count: uint16_t = 2 Bytes
Index Array: each is uint16_t = 2Bytes * number of indices
Vertex Array: each is 3 * floats for position + 4 * uint8_t for Color = 16 Bytes * Number of vertices
Extraction of data:
The data is extracted in the same order as the file format.
To extract Index and Vertex arrays, we just have to create pointer to the correct location from where the corresponding array starts, since the data is already binary and is loaded in memory.
Optional Challenges:
- Using a different file extension for the binary files: To differentiate the Human readable files from the binary files, I changed the extension of my binary files to “.meshbinary”.
- Loading a recognizable object: I am using a Gas can and a Chair which I have used in my previous assignment for this one. Meshes are courtesy of Chris.
Final game output:
Downloads: