Create a Game Engine: Part VI Rendering Triangle

In this part we are going to create the Triangle class which inherits the Model class discussed in the previous tutorial. We will have a Create method where we copy all the code required to create the triangle from this tutorial. Of course we need to adjust a few things to work. I won’t explain VBOs and VAOs all over again, there are a few tutorials back on this subject.

Now, with this modular structure it’s easy for us to add new models to our program and we don’t have to modify the engine to support new types. I will actually add a square just like the triangle to show how easy it is. In later chapters we will use this Create method to load more complex models from files and render them just like the triangle.

First let’s add a new header file called Triangle.h in the Models folder and make this class inherit the Model class

With our new engine, please delete GameModels.hand GameModels.cpp (used in the beginning to create the triangle) because we don’t need them anymore.

As you might guess, we need a place where we can keep our models and loop over them when we render the scene. Let’s call this class ModelsManager. This manager will communicate with ShaderManager because we need to pass the shader program to our models and it will be instanced in SceneManager. Here we will have a map structure to hold our models and Draw and Update methods used to loop over the models.

So let’s add Models_Manager.h and Models_Manager.cpp in the Managers folder. So far your project should look like this (notice the triangle.cpp and triangle.h in the Models folder):

Note!!! As you can see we are using a map container to store our models. For little games that’s fine (it’s more elegant) but if you have a huge game with lots of models to render this can be a problem from a performance point of view and map should be avoided and replaced with a vector. STL map wasn’t build for fast iterations. You can always profile and see what’s the bottleneck. I wrote more about this here: http://in2gpu.com/2015/03/09/iterating-over-stl-map-stl-vector-stl-list-and-vector/

We will create the triangle object right in the constructor, but if you want you can create a specific method for this. As I said earlier, in Draw and Update we loop over our objects and draw/update them. Delete will delete a specific model and GetModel will return a model. Here is the code for Models_Manager.cpp note that I used some C++11 syntax to iterate over the map.

The last thing we have to do in order to see that this basic game engine architecture works it is to link Models_Manager with Scene_Manager created in the previous tutorial. So we have to to create a Models_Manager object in Scene_Manager and call Draw and Update methods.

I have been working as a software engineer in computer graphics for almost 5 years. In the free time I love playing guitar, playing video games, hiking and studying computer graphics techniques.Follow me on : Google + , Twitter, Facebook