Hi there stranger of the realm called internet! Finally I got myself to write article to this tutorial after almost two weeks since it has been created! Well, you know, beginnings of new years are usually a bit harsh and there's lot of stuff to do, so that's why. Anyway, let's not waste with prologue and let's get to work ! This tutorial is all about refactoring what we've done until now.
If you don't know, refactoring is the process of looking back at the code, that already works and writing it in a different manner, while perserving the functionality. In my case this means, that after 9 tutorials, I have decided that to make things better already now, before it would get out of control in later tutorials (for more information about refactoring in general, visit Wikipedia Refactoring article.
What was the problem in our case? Well, the easiest thing is to look at the code from tutorial 009:
And then somewhere later in the initializeScene() function:
What is exactly wrong with this, you might ask? The thing is, that we are creating now tens of variables out in the wilderness of the code, in the namespace of global variables. Morever, initalizing objects to render is done using one VAO and one VBO and then later, we have to write indices of the vertices correctly to see pyramid or cube. This was acceptable until now, because our scenes have not been so complex. But the more advanced tutorials I want to write, the more complex scenes are going to be rendered and we have to find some systematic way of doing this. And probably the most problematic thing was proper releasing of those objects. In the releaseScene() function, we just had not to forget about any variable, which is just wrong.
So let's discuss now what I've done, to make the code better and more readable .
The first major thing I've done was to create managers for OpenGL objects. These classes are responsible for creating and caching OpenGL objects (for later use) and they can release them all at once, not forgetting anything. I have created 4 OpenGL objects managers in total - shader manager, shader program manager, texture manager and sampler manager. There is one more manager - matrix manager, but this one just serves as a holder for matrices. All those classes are singletons - that means, there is only one instance of them available and you can't create your own (no two texture managers can even exist for example). Let's take texture manager as an example (all other classes are pratically same, they just handle different object types):
Let's discuss some C++ stuff here first. Can you see the first two functions, ending with delete keyword? With this, we are just telling compiler not to generate those two methods, that it does by default - copy constructor and copy assignment operator. By defining this, we have disabled a way to copy this object - that is to prevent anyone to make another copy of the texture manager. Moreover, for complete singleton implementation, the default constructor is made private - that means, nobody out of the class can really create the texture manager .
In order to get to that one and only texture manager, we have to call static function getInstance(). It's implementation looks like this:
Pretty simple huh? This approach is called lazy initialization - that means, that first time someone calls this method, that one and only instance called tm initializes and gets returned. From second time calling this function on, the object is just returned. The advantage is, that if we don't ever get to do anything with textures in our code, it won't be even initialized at all! This is a small, but nice optimization.
In every manager, we have a way to get to the object it manages and to add another one. In our case, we have a loadTexture2D function to add a texture to the manager. Managers have their cache of objects, implemented using std::map. Whenever you're loading texture, you have to provide string key to store it with. Let's have a look at the function:
The code starts with checking, if we aren't accidentally adding another texture with the same key. If not, then we try to load the texture and use the unique_ptr to hold it. In case we fail to load the texture, we throw an exception, otherwise, we just move the ownership of texture to our cache, so that only one holding our texture is the cache within the texture manager (that's why there is this std::move). If you don't understand the concepts behind unique pointers and C++ move semantics, I recommend you to read some articles about it, for example this one. So the basic idea is to store the texture using a string key, for example, to store the snow texture, we can simply use "snow" as a key to represent it .
As mentioned before, the other 3 managers - shader, shader program and sampler managers they manage shaders, shader programs and samplers, respectively. The principle behind it is really the same, so I won't explain it in detail again - anyway I recommend you to see the code for yourself. The last remaining manager I have introduced is MatrixManager, but this is really just a holder for matrices, nothing else.
Another stumbling block in our code is initialization of objects like cube or pyramid out in the wilderness with no information about them stored whatsoever! This has to change, especially for the tutorials I plan in the future. I have decided to create two classes, that will hold basically arbitrary static objects - StaticMesh3D and StaticMesh2D. One is used for rendering 3D and another one for rendering 2D objects. They are more or less the same, so let's go through the StaticMesh3D class:
This class holds everything about the static mesh, information like: does it use vertex positions (well, this one will probably always be true )? Does it use texture coordinates? Does it use normals? You could notice, that the render method is a pure virtual function - this is because every static mesh (cube, pyramid) has its own way to use its data and render itself. That means, that this class is just an abstract class to hold static meshes. Let's derive class Cube from it and examine its functions:
Basically, you can see that in the Cube class we have predefined array of vertices, texture coordinates and normals. By default, if you create cube, it is created with vertex positions, texture coordinates and normals. In constructor, we initialize the cube data by calling the initializeData() function:
This virtual method is always responsible for creating VAO and VBO and filling it with correct data in every static mesh, not only Cube. The important thing to notice here is, that this class has one inherited VAO and VBO, so all the vertex data are packed within that VBO and vertex attributes set are in that VAO. First, there are all vertex positions, then texture coordinates (if needed) follow and finally there are normals (if needed). After storing data in GPU, setVertexAttributesPointers() function is called to set vertex attributes correctly (this is similar for all static meshes, that is why this function is defined in the base class StaticMesh3D). Rendering cube is then just a matter of calling glDrawArrays(GL_TRIANGLES, 0, 36) .
In similar manner, other static meshes have their own classes. For now, it's pyramid, snow covered ground and house plus there is a 2D static mesh in class Quad (this will be used for HUD refactoring later). I won't go into details of every static mesh, but practically it's all very similar and every object is now responsible for its data and rendering, rather than setting stuff up in the wilderness of global variables . Feel free to study all of those classes and functions, but I hope you got the main idea and reasonings behind those changes .
Another major refactor is creating a HUD class. From now on, everything that will be rendered over the scene will be defined in the separate HUD class, which looks like this:
As with static meshes, render method is pure virtual, because every tutorial will have different HUD elements, so we have to inherit from this class. Nevertheless, this base HUD class should implement the common methods for all HUDs (in later tutorials, I will extend this class with fonts support). For now, we have just one function called renderTexturedQuad2D(), which renders a quad at certain position with certain size. But wait, there's more! This function supports rendering from the left or the right of the screen as well as from the bottom or top of the screen! This function simply renders 2D Quad static mesh and you have to bind the texture beforehand.
To render HUD used in the 9th tutorial about orthographic 2D projection (as well as in this, 10th tutorial), we define a dedicated class HUD010 for it:
The code is very similar to the code from 9th tutorial, except it's a bit nicer and more systematic. Instead of computing the coordiniates of our Christmas tree and snowflake, we can simply call the renderTexturedQuad2D method twice. Once we render from the left side of the screen, once from the right side of the screen and that's it!
There is one last important class and it's ShaderConstants. You might have seen, that many times, I am reusing the strings like "matrices.mainMatrix" or "matrices.projectionMatrix" This class simply holds all those commonly used uniform variable names, that we keep reusing among multiple shaders. It is basically just a storage of static methods, that return strings (I have made this class header only, that means - it's completely defined in header file and no cpp file is needed).
Then anywhere in the code, where we need the name of uniform variable for projection matrix or model matrix (or some other common shader names), we will refer to constants from this class from now on
There have been many refactorings and changes in this tutorial, so let's have a short reprise, what has been done:
Even though the code has been changed significantly, the visual result is same as in the previous tutorial:
But that does not mean we've done nothing! From now on, the readability of tutorials will improve. And especially, if we are to implement more advanced stuff, we have to come up with a systematic way to keep everything nice, maintanable and readable. So I hope that despite the fact we didn't see anything new this time, you've learned a lot anyway and enjoyed this tutorial . Next time, we will learn what is Indexed Rendering and we will learn, how to render cylinder with it.Download 3.77 MB (109 downloads)