top of page
Search
chilaganirajesh95

Creating Custom Mesh file(Assignment_06)

In this blog, we will discuss how to create our own human-readable mesh file format and use it in our engine replacing the hardcoded mesh data.





Human Readable Mesh File

What are mesh files and why do we need them to be human-readable? Mesh files are a representation of the actual geometry data that is used to render the meshes. From previous blogs, we know that how we submit meshes for rendering we submit vertex data and index data to the graphics to draw. But it is not convenient to have this data inside the code imagine an artist want to do some changes to mesh it is not easy for him to go through the code and change the mesh data itself. Remember we are creating tools and systems to make it simple for content creators and gameplay programmers to do their work. They need not worry about what graphics is doing and how is it working. So a having a mesh file solves this problem the content creators can edit the mesh file and you can use the mesh file to render as we did previously. Ideally, the artist will not edit the mesh file directly they will use some kind of tool that makes it easy for them to create mesh or art in a way that it is convenient to them and the tool will export the mesh file and we will use it.

So why do we need the mesh file to be human-readable or understandable anyway the artist is not going to see the actual mesh file. Yes, but it is not required for artists it is useful for us. The human-readable mesh file helps us in debugging when things go wrong. Mistakes happen all the time and there could be many reasons for that and it is helpful for us to have a human-readable mesh file. We can go through the mesh file and see why things are not working as expected. If the mesh file was not human-readable there would be no way for us to tell what is causing the issue.


Following is the mesh file the I created for my engine and we have to keep few things in mind while creating these files and Of course things might change a little bit but we should have a proper design so that it is not too hard to add few things to the mesh file in future.

{
    Vertices =
    {
        {
            Position = { -2.0,-1.5,0.0,},
        },
        {
            Position = { 2.0,-1.5,.0,},
        },
        {
            Position = { 1.0,-1.5,-1.0,},
        },
        {
            Position = { -1.0,-1.5,-1.0,},
        },
    },
 
    Indices =
    {
       0,1,2,
       2,3,0
    },
}

I used Lua to represent the mesh file but other formats like JSON can also be used. Pick whatever you are comfortable with. Lua does not contain arrays or other data structures it has tables that can be used as arrays or as maps with key-value pairs. You can read more about Lua tables here


In my mesh file, I have a root table that represents the whole mesh with Vertices and Indices as keys and their corresponding tables as values. Remember Lua tables can be used as arrays and dictionaries so I could also use a simple array type table without keys which also works fine. But when something goes wrong and the mesh file needs to be looked for debugging how would any engine programmer know what data is what we cannot assume that the programmer understands that data just by looking at it. So I went with a key table instead of an array table. Coming to Indices it is a simple array table with numbers. From previous blogs, we know that the order of indices does matter and we will have to iterate over them so an array table works fine here. For Vertices also an array table works fine In fact I used an array table during the initial testing of the mesh files. But I modified the vertices tables a little bit keeping things that we might add to it in the future. A vertex does not only contain position information there other kinds of information that it can store. Some examples are color data, normals data, and sometimes texture coordinates and etc. So if we just have a single array it might be a little confusing for us later when we have all these details in vertex. So I created a table for each vertex that contains tables for each of the data that it can contain. Currently, we only have position information so we have a table for the position in the future when we add color it gets its own table like below.

 {
            Position = { -2.0,-1.5,0.0,},
	    Color    = {1.0,0.0,1.0,1.0},
 },

I created a new mesh builder project which generates the corresponding mesh files for the game and I have updated the Factory functions to create a mesh from a Lua file instead of hardcoded data. It is very simple now to add many meshes, all we have to do is create a mesh file in Lua and provide it to the game object. I added a new plane to the simulation just by adding a new Lua file which you can see in the above gif.


The Mesh builder project is a command-line tool and we can debug this by providing appropriate command-line arguments to the project whenever we have to debug the mesh file generation. Following is a screenshot of debugging the Mesh Builder project.


Controls:

ESC – exit.

Up/Down/Left/Right - Move Triangle Object

W/A/S/D/Q/E - Move Camera Up/Left/Down/Right/Front/Back

ENTER - Changes the mesh to square and effect to the animating color of the object.

Space - Toggle between main and secondary camera. Note: the secondary camera does not support the movement.



14 views0 comments

Recent Posts

See All

Commentaires


Post: Blog2_Post
bottom of page