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

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.

#pragma once
#include "Model.h"
namespace Rendering
  namespace Models
    class Quad : public Models::Model

       void Create();
       virtual void Draw() override final;
       virtual void Update() override final;

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

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.

#include "Quad.h"
using namespace Rendering;
using namespace Models;



void Quad::Create()
  GLuint vao;
  GLuint vbo;
  glGenVertexArrays(1, &vao);

  std::vector<VertexFormat> vertices;
  vertices.push_back(VertexFormat(glm::vec3(-0.25, 0.5, 0.0),//pos
                                  glm::vec4( 1, 0, 0, 1)));   //color
  vertices.push_back(VertexFormat(glm::vec3(-0.25, 0.75, 0.0),//pos
                                  glm::vec4( 0, 0, 0, 1)));   //color
  vertices.push_back(VertexFormat(glm::vec3(0.25, 0.5, 0.0),  //pos
                                  glm::vec4( 0, 1, 0, 1)));   //color
  //4th vertex
  vertices.push_back(VertexFormat(glm::vec3(0.25, 0.75, 0.0),//pos
                                  glm::vec4(0, 0, 1, 1)));   //color
  //nothing different from Triangle model
  glGenBuffers(1, &vbo);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);            //here we have 4
  glBufferData(GL_ARRAY_BUFFER, sizeof(VertexFormat) * 4, &vertices[0], GL_STATIC_DRAW);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
                        sizeof(VertexFormat), (void*)0);
  glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE,
                        (void*)(offsetof(VertexFormat, VertexFormat::color)));
  this->vao = vao;


void Quad::Update()
    //nothing here for the moment

void Quad::Draw()
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Last thing we have to do here is to include this new class in Models_Manager.h and create this object in Models_Manager.cpp constructor.

#pragma once
#include "../Rendering/Models/Quad.h"
 //in the future this code should be removed from constructor
 //it can be hard to track a bug
	Models::Triangle* triangle = new Models::Triangle();
	gameModelList["triangle"] = triangle;

	Models::Quad* quad = new Models::Quad();
	gameModelList["quad"] = quad;

Quad and Triangle

Quad and Triangle

The square will become really handy in Post Processing Effects and Render to Texture technique.

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). 

Source Code:OpenGL_Basic_Engine

Also on GitHub:

Tagged under:
blog comments powered by Disqus