Create a Game Engine: Part VII Square

In this part we are going to add a quad/ square (still in NDC space) in our engine. This should be an easy task. Basically we have to create another class which inherits the Model class, just like we did with the triangle. Also we will talk a bit about Triangle Strip primitive use to draw this model.

So let’s create a quad.h and quad.cpp inside Models folder. Your project should look like this:

Quad Model

Now let’s write the code for Quad.h. You will see that this class is identical with the Triangle class created in the previous tutorial.

Now let’s see Quad.cpp. For quad we have to add one more vertex and render it with Triangle_Strip primitive. You can also represent a quad with two triangles and use Triangle primitive but you will need two duplicate vertices (6 in total). That’s because first triangle doesn’t have any connection with the second triangle.

In Triangle_Strip mode the video card remembers the last 2 vertices and together with the new vertex will create a new triangle. This way we send less data to our buffers (4 instead of 6). The problem is that you can’t use Triangle_Strip in any situation and you have to use the Triangle primitive. Luckily there is another technique which can be used with Triangle primitive to minimize the number of vertices sent to the video card. This technique is called indexed drawing and we will talk more about it in Chapter II.

Triangle and Triangle Strip differences.

Let’s say we have 1000 squares to render. If we use Triangle primitive we will have 6000 total vertices in our buffers. Now if we use Triangle Strip there will be only 4000 vertices.

In the next tutorial we will talk about performance issues with this engine and what can we do to make it better. Also I did some code cleanup (keep the same ident style and all methods starts with a upper letter).

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