Creating and loading a Binary mesh file
Purpose:
The assignment was to
Create a human readable mesh file that stores information about the vertices and indices.
Create a MeshBuilder.exe that takes an ASCII mesh file and outputs it into binary file for game to load.
Mesh File Format:
First step was to decide and create a mesh file in Lua. I came up with the below file format after trying out different ones. We in class tried to decide the level of detail that should go into this file and the audience (Mostly Artists) who this file should be understandable to.

I think an artist who would want to come in and edit / create a mesh file would easily understand, from above, that,
There are multiple vertices per mesh,
Each vertex has Position and Color
Indices there vertices map to, to form a triangle.
I wanted to add another Key-Value pair to it, Number of Primitives instead of Indices count, but decided against it because having a user list out all the indices and its count made much more sense in my head with relation to the existing data in the mesh file being Indices.
Assets to build entry:
As we have seen the creation of this in previous posts, MeshBuilder.exe takes “plane” and “cube” files with extension “.mesh.lua” as input and outputs “plane.dat” and “cube.dat”. The extension “.dat” made sense to me because we are storing the output in binary format. Other alternatives I thought were “.bin” and “.mesh”.

Converting the mesh file into binary file:
Loading a binary file while runtime has multiple advantages compared to ASCII file, few of them being,
It is faster, as the data is already cooked into the format the game is expecting. In case of ASCII the game should parse it and find the values that it needs. Offloading this computation to build time is always desirable.
Binary formatted file is smaller in size and takes up lot less space in a released game.
Binary formatted file is also not easy to decipher, letting a shipped game have ASCII human readable file could lead to unexpected game behavior, if someone meddles with this file.
Process: Involved two step,
Build time: Pretty much the ASCII version of the mesh file contained list of vertices and indices with respective counts. All that needed was to parse the values, store it in a structure and write out the structure in binary format during build time.
Run time: Use the same structure from the build time and read the binary file into a buffer during runtime and parse for four chunks of data, Vertex Count, Index Count, all the vertices and all the indices.
Number of reads at runtime and why I chose what I chose:
Since parsing a loaded memory is always faster than multiple reading from disc, I decided to load all the data (Vertex Count, Index Count, Vertices, Indices) in one big memory buffer. Used pointer arithmetic to find the pieces of data to create vertex and index buffer. I am doing 2 memcpy, one for all the vertices and another for all the indices.
Misc:
In the below screen capture you can see how the data looks like in memory,

I have verified that it is the same data once I write it in a binary file at build time. You can see how the binary mesh file looks, when opened in a hex editor. The hex editor is trying to read the values as 32bit floats, which is the reason the first two values (Vertex count and Index count) stored as int is displayed as junk values.

Time Spent:
Reading: 2hr
Creating MeshBuilder with Lua logic to parse the mesh file: 4hr
Writing and Reading the binary mesh file: 1hr
Write-up: 1hr
Total: 8hr


![Flappy Bird clone ! [Using my own C++ Game Engine]](https://static.wixstatic.com/media/5ab37f_6f5eaa61ba744460bf8bbc86ef50cc47.png/v1/fill/w_306,h_250,fp_0.50_0.50,q_35,blur_30,enc_avif,quality_auto/5ab37f_6f5eaa61ba744460bf8bbc86ef50cc47.webp)
![Flappy Bird clone ! [Using my own C++ Game Engine]](https://static.wixstatic.com/media/5ab37f_6f5eaa61ba744460bf8bbc86ef50cc47.png/v1/fill/w_38,h_31,fp_0.50_0.50,q_95,enc_avif,quality_auto/5ab37f_6f5eaa61ba744460bf8bbc86ef50cc47.webp)
Comments