Game Engineering 2 Assignment 7

Objective:

1. Create a human-readable mesh file.

2. Create a mesh builder to save the mesh file as a binary file.

3. Read the binary mesh file to create mesh in the game.

Mesh File

Here is the mesh file I used to define the floor mesh in my game:

mesh

I stored the data of each vertex in an array so I can iterate all the vertices in a for loop. For each vertex, there is a key  “position” and a key “color” and this is for human to read.

Here is the binary version of the same file after it is processed by my mesh builder:

binary

 

A binary file is much smaller than a text file. In my case, the size of my human-readable mesh file is 436 bytes and the size of binary file is 128 bytes, just 1/4 of the text file. So using binary files can reduce the memory consumption of my program.

Writing a Lua Helper Class 

Now I have 2 types of asset are represented as Lua tables, one is mesh and the other is material. Instead of writing reading Lua table functions for each asset, I decided to write a helper class to deal with Lua tables.

Consider my mesh file, here is how I use my helper class:

LuaHelperUsage

Basically what I want to achieve is I can use a key to get any value I want in the Lua table.

I used this tutorial  Using Lua with C++ as my reference when I code my helper class.

Here is my helper class:

LuaHelper

 

The most important function is PushToStack. What it does is it pushes the table we want to manipulate on the top of the stack, which has a index of -1 in Lua. Here is the code:

PushToStack

Write/Read Binary Files in C++

I used std::ofstream and std::ifstream to manipulate binary files. I used 4 writes and 4 reads to write and read my mesh data.

Here is how I write file:

writemesh

 

And here is how I read file:

readmesh

It is possible to use less read and write commands. For example, if I put vertex_count and index_count into a struct, I can reduce my read/write commands to 3. I didn’t do that because I want to keep my code as simple as possible.

Time Estimates

  • Reading – 4 hours
  • Coding – 6 hours
  • Write up – 2 hours
  • Total – 12 hours

Download Executable

Leave a Reply