Blogger Templates

Sunday, November 12, 2017


Graphics (10 of ∞) Data driven mesh powered by Lua.

Posted in ,
EAE 6320-001

In this write-up, I will talk about my experience of converting the mesh vertex points, color information and index points to be data driven from a Lua file and have them build using a mesh builder and parse the information in a manner that can be used by the engine to process the mesh information.

Lua Format
When I started working on the Lua format, I had two things in my mind. I should follow an array table pattern when there is an order and use a dictionary(key-value) table pattern when the order is not required. Also, I feel a dictionary makes the value more prominent as to which key it belongs to. So I preferred using a dictionary for the vertex coordinates like x, y, and z over array as I felt its more human readable. I followed the same dictionary format for color formats like r, g, b and a. Since the entry of each vertex and color would follow a specific order, I preferred using an array for that and this is to preserve the winding order flow. The advantage of making these files human readable is that these files could be either manually crafted based on the type of geometry that has to be created or a 3rd party system can interface to create this modular human-readable component. One example would be a person designing a model in Maya can export a mesh that is based on Lua format and integrates it with our application.
The plane I created follows a cuboid format with a very low height. In mathematical terms, though plane doesn’t have any height, I wanted to give this as an option to the gameplay programmer to play with. Many game engines like Unreal follow the same pattern under the name floor. Here is the Lua structure that I created for my plane mesh.

The pattern I have come up has vertex and index data. The vertex follows dictionary of an array of a dictionary of arrays and the indices follow dictionary of arrays. Here you can clearly see that each value is standing out and is easily readable and modifiable.

Mesh Builder
Okay, I have a mesh now and this mesh has to be built to be usable in my project. For the time being, I will just make a copy of this mesh Lua file to the game install folder. To get his working, I had to create a new project MeshBuilder replicating the same pattern as that of TextureBuilder. I did some code digging as to how the file IO for copying was performed and I did the exact same thing in a previous write-up for the settings.ini file. So I followed the C++ implementation of the copy and directly accessed it from the build overloaded method in mech builder. This way I can modify AssetBuildLibrary to make use of my MeshBuilder.exe to build the mesh related Lua files(which actually performs a copy of the mesh files in the game installation directory.)

More Handles
Since now we are dealing with assets, it would be a good idea to use a handle over the pointer. So I had to make some structural changes such that my project can support mesh handles from the game code using which I can get access to the asset for all the data operations.

I want to compare the experience of reading/parsing the file content from the Lua file to the movie Inception. To get one data, I need to traverse several layers deep. This was a bit challenging and interesting as I need to play with the Lua stack by understanding which element has been pushed and poped. The thing that helped me with debugging is whenever I try to access a table, I would check it with lua_istable and luaL_len to confirm the number of entries I am reading based on the layer of content that I am in. I was able to figure out the pattern by analyzing the source code of UsingLua project and I followed similar pattern and code style in fetching the required entries from my plane and cube Lua tables.

Right Arrow - Move the 3D game object to the right.
Left Arrow - Move the 3D game object to the left.
Up Arrow - Move the 3D game object upwards.
Down Arrow - Move the 3D game object downwards.

W - Forward Acceleration
S - Backward Acceleration
A - Rotate towards the left side
D - Rotate towards the right side
Q - Rotate Upwards
E - Rotate Downwards