Create a triangle in OpenGL Part I

Go to OpenGL Home

Last time we saw how to create a triangle using constant vertices in a vertex shader. Let’s say that we have a model like a monster or a car with a mesh containing around 2000 vertices. What are we going to do? Write all 2000 vertices in our vertex shader? No, that would be madness; we have to pass them dynamically to our shader. This way we can pass different models with different number of vertices to the same shader. Usually a game model is loaded from a file exported from Maya/3DS Max or other rendering programs. To keep things super simple let’s create the same triangle from the previous tutorial in a C++ class and send it to the vertex shader. In a future tutorial we will see how to load meshes from a file.

In this first part we will talk about:

  • Vertex Format and attributes
  • create a basic class to hold our objects

In part II we will talk about:

  • Vertex Array Object (VAO)
  • Vertex Buffer Object (VBO)

In part III we will talk about:

  • Tying attributes
  • Modify our vertex shader
  • Draw triangle

First we want to create a structure called VertexFormat which describes the contents(also called attributes) of a vertex like position, texture coordinates, normals, material etc also . For the moment we only need position for our triangle. Because we are in 3D we always need to know the x,y,z values of a vertex. For this reason we will use a 3D vector which is the simplest way to represent the position. Later on we will need to do vector operations like: dot product, cross product, subtraction, addition, etc; so using a vector makes sense. We can create a vector class to handle these operations or we can save some time and use a math library. Let’s use OpenGL Mathematics (GLMbecause it provides all we need at this moment.

To use GLM in our project, you can download the latest archive from their website and put the contents in the Dependencies folder near GLEW and FreeGLUT folders or we can install it using NuGet Package Manager. 

This time I will use NuGet to add GLM in our project:

NuGet Visual Studio

NuGet Visual Studio

When the NuGet console pops up, just write : Install-Package glm and that’s all.

Now we are ready to create our VertexFormat structure

#ifndef  VertexFormat_H_
#define VertexFormat_H_

#include "glm\glm.hpp" //installed with NuGet

struct VertexFormat
  glm::vec3 position;//our first vertex attribute

  VertexFormat(const glm::vec3 &pos)
     position = pos;


Next step is to create a our C++ class called GameModels.h to hold our game models:

#ifndef _GAME_MODELS_H_
#define _GAME_MODELS_H_

#pragma once
#include "../Dependencies/glew/glew.h"
#include "../Dependencies/freeglut/freeglut.h"
#include "VertexFormat.h" //added 3/21/2015
#include <vector>
#include <map>

namespace Models
  //I explain this structure in Part III
  struct Model
      unsigned int vao;
      std::vector<unsigned int> vbos;


  class GameModels
         void CreateTriangleModel(const std::string& gameModelName);
         void DeleteModel(const std::string& gameModelName);
         unsigned int GetModel(const std::string& gameModelName);
        std::map<std::string, Model> GameModelList;//keep our models

We will expand and modify this class in future tutorials, but for the moment this is all we need to create our triangle. We store our models in a map and access them by name. The private map GameModelList  holds Vertex Array Objects (VAOs) which we will talk about in the next tutorial. Don’t forget to create GameModels.cpp too.

So far, you should have these folders and files. packages.config comes with the NuGet configuration for GLM.

project folders so far

project folders so far

blog comments powered by Disqus