This assignment had us create binary files out of human-readable files for our meshes. The result looks no different from last time, but all the meshes are now stored in a binary file behind the scenes. Try it out here!
The controls are the same as last time where you can move the camera forward/backward by using the Up/Down Arrows and Left/Right using the Left/Right Arrows. You can also move the cube Up/Down using the W/S keys and Left/Right using the A/D keys.
Binary Mesh File
Here's an example of a plane mesh as a binary file. The colors indicate what the bytes represent with red being the vertex count, green being the index count, blue being the vertex array, and yellow being the index array.
The order of these bytes matter as the vertex and index counts need to appear before the arrays so that we know how many vertices and indices to read in.
With that, the advantages of binary files are that they are smaller and faster than human-readable files. These advantages are why binary files are preferred at run time because they are faster to load. However, human-readable formats are still preferable during development for their flexibility and clarity.
Built binary mesh files need to be tailored for different platforms because of winding order. This is so that we can directly use the data stored in the file rather than worry about switching winding order.
Reading Binary Mesh Files
This is the code to load a binary file and extract mesh values:
eae6320::Platform::sDataFromFile dataFromFile; eae6320::Platform::LoadBinaryFile(i_path, dataFromFile); auto currentOffset = reinterpret_cast<uintptr_t>( dataFromFile.data ); const auto finalOffset = currentOffset + dataFromFile.size; const auto vertexCount = *reinterpret_cast<uint16_t*>( currentOffset ); currentOffset += sizeof( vertexCount ); const auto indexCount = *reinterpret_cast<uint16_t*>( currentOffset ); currentOffset += sizeof( indexCount ); auto* vertexData = reinterpret_cast<VertexFormats::sMesh*>( currentOffset ); currentOffset += sizeof( VertexFormats::sMesh ) * vertexCount; auto* indexData = reinterpret_cast<uint16_t*>( currentOffset );
File Size and Load Times
For a comparison between human-readable versus binary files, I created a helix with 23 axis subdivisions and 259 coil subdivisions in Maya. This sizes of these files were:
Human-Readable Size: 4734 KB
Binary File Size: 1048 KB
Finally, the load times were:
Human-Readable Load Time: 0.2065894 seconds
Binary File Load Time: 0.0005329 seconds