The Assignment:
In Assignment 8, I have modified my MeshBuilder class to output binary files instead of copying human readable Lua mesh representations, so that now, in the run time, meshes are initialized by getting data from binary data files directly.
The Results:
A Windows application that renders several simple meshes in a window where both the camera and the mesh object are movable. This time I’ve added in a robot model I created three years ago.
Control of the game: Hold W/A/S/D to move the camera up/left/down/right. Hold Q/E to move the camera forward/backward. Hold arrow keys to move the cube up/left/down/right.
Binary Geometry File:
The binary files are written in a little-endian fashion. The data is written in the order of the following: the number of vertices, the number of indices, an array of vertex data, an array of indices. The picture above shows the binary data of a triangle mesh. The value in the red box represents the triangle mesh contains three vertices. The blue box shows the length of the index array is three. The green area is the vertex data array of three vertices. It contains the position and the color information of each of three vertices. The yellow area at the last is the index array of three indices.
While a human readable file has significantly better readability and is user friendly and easy to modify, a binary file with the same data is much smaller in size. And it’s much easier and faster to extract the data out of a binary file than doing the same from human readable files. I’ll share more detail of those advantages in a later section. Thus, it is good to use binary files for run time purposes for better performance, and to use human readable files as asset to be built for better editability and maintainability.
Should the Binary Geometry Files Be the Same or Different for the Different Platforms?
In my current implementation, the binary files are the same for both D3D and OpenGL. However, I do think there is an advantage of having different binary geometry file for different platforms. For now, the default winding order of mesh files is in the OpenGL flavor. To use those mesh files in D3D, an extra step of changing the winding order is required when initializing Mesh instances. If we do move this step to the build function then the wind order of the mesh files would be corrected at build time, and thus it would slightly increase the performance for the D3D platform at run time.
Extracting Data:
Measuring the Advantages of Binary Files:
Helix Mesh File Size Time to load file and extract data
In Binary 608 KB 0.000938 s
In Human Readable 3476 KB 0.191515 s
The table above show the same helix mesh in both binary format and human readable format.
In binary format, the data of the helix mesh only takes 609 KB on the disk. Whereas it costs a total of 3476 KB on the disk in the human readable format. We can clearly see that the binary format is more than 5 times smaller than the human readable format.
When it comes to performance, loading the binary file and extracting data from it is about 200 times faster than the human readable file. We can easily see it would be much faster at run time if we choose to initialize meshes with binary files. The difference here is significant.