Game Engineering – Building Game Engine – Part 6

Creating a human readable mesh file

Till this point in the class, we were submitting meshes from the code as vertex and index buffer arrays in the game. Even though this method is better compared to when we were submitting the data in the engine, this has its drawbacks. Chief among them is we have to modify the code every time the meshes have to change. Having meshes load from a separate file removes the need for that, as we can load the vertex and index buffer from the file and render the mesh. There can be two different approaches to how the data is stored in files, storing them as binary files or storing in a human readable format.

Storing the files in a binary format can be very beneficial as loading binary files is considerably faster compared to loading plain text files. The downside is that they are not human readable and there is no easy way of editing binary files. The second is having them stored in a human readable format. The main advantage of such files is that they can be easily editable. The downside of this is that they can be slow to load.

Example Mesh File:

return
{
	VertexBuffer =
	{
		{
			Position = {0,0.5,0},
			Color = {0,0,0,0},
		},
		{
			Position = {1,0.5,0},
			Color = {0,0,0,0},
		},
		{
			Position = {0.5,1,0},
			Color = {0,0,0,0},
		},
		{
			Position = {0,-0.5,0},
			Color = {0,0,0,0},
		},
		{
			Position = {1,-0.5,0},
			Color = {0,0,0,0},
		},
	},
	IndexBuffer = {1,0,3,4,1,3},
}

The above code shows a mesh file. It has two parts the vertex buffer and index buffer. Each table in the vertex buffer represents one vertex. Each vertex contains a position and color for that position. The position array is contains the x,y,z coordinates in that order. The color has r,g,b and alpha in the that order. Since these files will be used by people with basic knowledge about the arrangement of position and color, they do not have labels associated with them while still being readable. The color attribute is currently not being used but has been added to future proof the mesh format.

Handles:

The file is processed with lua and the data is fed into the existing mesh class to create a mesh. Instead of creating a new mesh every time the game wants, we implemented a handle system. In this system, the mesh class contains a manager which tracks all the meshes being created and the game gets a handle to a mesh instead of the mesh itself. Whenever the game wants to use the mesh, it asks for the mesh using the handle and the manager will either create the mesh or return the mesh if it has been already created.

Ouput:

Even though visually the previous output and the current one look almost the same (hello again tree), there have been significant changes under the hood

Controls:

  1. WASD to move the rectangle
  2. IJKL to move the triangle
  3. F2 to swap the effects
  4. F3 to swap the triangle to a house
  5. Left and Right arrow to move the camera in X axis.
  6. Up and Down arrows to move the camera in Z axis.
  7. Z and X to rotate the camera around itself
  8. Ctrl and Alt to move camera along the Y axis

Downloads:

MyGame_x64_1003MyGame_x86_1003

Leave a Reply

Your email address will not be published.