This week assignment was about the Binary Files. Instead of loading mesh data from human readable lua files as last week, we generated binary files and used that to initialize our mesh.
For creating our mesh, we require 4 important pieces of data. I have stored this data in the binary file in the following order:
The green part is index count followed by index data in red. After that its vertex count and then vertex data. We should try to store the data which is most feasible to us in terms of reading this data.
Two major advantages of a binary file are its size and processing time. Binary files are much smaller as compared to human readable files and the reading of data is much faster in them. For my helix test mesh, I found the following information in the release build:
[Note: There are various ways we can write the binary file. We can use fwrite() (C approach) or ofstream (C++ approach). Since while writing the binary file, we are first parsing the lua file, we can either open the file, write the information as we parse it and close the file (and keep on opening and closing as we parse the relevant information from lua). Otherwise, we can parse all the information from lua first and then write it at once to the file. It’s advisable to parse all the information first and then write it to the file to avoid multiple opening and closing calls to the file. This also improves readability of the code as all the write calls are together. While doing multiple open/write/close calls, it’s also important to always open the file in binary. I missed this once and as a result was getting additional 0D before every 0A byte in my binary file. This was because file was getting opened in text mode and MS-DOS (windows) will add an 0D before every 0A in text mode (translation of Line Feed (0A) and Carriage Return (0D) occurred).
This thing took me some time to figure out (I even tried the memory debugging as I was not sure what I am seeing in the binary file is correct!). While reading serialized binary files (by serialized I mean when we have a fair idea about the data in the binary file), one thing which might be helpful is to check how and where the pattern of data is changing. I realized this as in our binary file every alternate byte was 00 (index count and index data) and 0D was getting added right before 0A, the order of 00 suddenly changed. It will not tell the root cause, but certainly will take us one more step closer to the problem.]
For this assignment, we moved the parsing of lua code to the mesh builder. Mesh builder is an application which will build our binary file in build time. I am changing the winding order for Direct3D in mesh builder while writing the binary file rather than dong it on run time as before. Both the ways will work but I think we should do it in mesh builder as:
As the mesh builder is running when we build the project, its difficulty to debug inside this (We can log debug messages but not been able to hit the break points is obnoxious!). The way to debug here is:
Playable EXE Download
W S A D to move game object.
Up Down Left Right to move camera.
Ctrl to switch the mesh of game object.
Space to hide one object (plane).