This will be another long tutorial.

OpenGL 3 makes it easy to write complicated stuff, but at the expense that drawing a simple triangle is actually quite difficult.

Don’t forget to cut’n paste the code on a regular basis.

If the program crashes at startup, you’re probably running from the wrong directory. Read CAREFULLY the first tutorial on how to configure Visual Studio !

## The VAO

I won’t dig into details now, but you need to create a Vertex Array Object and set it as the current one :

``````1 GLuint VertexArrayID;
2 glGenVertexArrays(1, &VertexArrayID);
3 glBindVertexArray(VertexArrayID);``````

Do this once your window is created (= after the OpenGL Context creation) and before any other OpenGL call.

If you really want to know more about VAOs, there are a few other tutorials out there, but this is not very important.

## Screen Coordinates

A triangle is defined by three points. When talking about “points” in 3D graphics, we usually use the word “vertex” ( “vertices” on the plural ). A vertex has 3 coordinates : X, Y and Z. You can think about these three coordinates in the following way :

• X in on your right
• Y is up
• Z is towards your back (yes, behind, not in front of you)

But here is a better way to visualize this : use the Right Hand Rule

• Z is your middle finger. If you put your thumb to the right and your index to the sky, it will point to your back, too.

Having the Z in this direction is weird, so why is it so ? Short answer : because 100 years of Right Hand Rule Math will give you lots of useful tools. The only downside is an unintuitive Z.

On a side note, notice that you can move your hand freely : your X, Y and Z will be moving, too. More on this later.

So we need three 3D points in order to make a triangle ; let’s go :

``````1 // An array of 3 vectors which represents 3 vertices
2 static const GLfloat g_vertex_buffer_data[] = {
3    -1.0f, -1.0f, 0.0f,
4    1.0f, -1.0f, 0.0f,
5    0.0f,  1.0f, 0.0f,
6 };``````

The first vertex is (-1,-1,0). This means that unless we transform it in some way, it will be displayed at (-1,-1) on the screen. What does this mean ? The screen origin is in the middle, X is on the right, as usual, and Y is up. This is what it gives on a wide screen :

This is something you can’t change, it’s built in your graphics card. So (-1,-1) is the bottom left corner of your screen. (1,-1) is the bottom right, and (0,1) is the middle top. So this triangle should take most of the screen.

## Drawing our triangle

The next step is to give this triangle to OpenGL. We do this by creating a buffer:

``````1 // This will identify our vertex buffer
2 GLuint vertexbuffer;
3 // Generate 1 buffer, put the resulting identifier in vertexbuffer
4 glGenBuffers(1, &vertexbuffer);
5 // The following commands will talk about our 'vertexbuffer' buffer
6 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
7 // Give our vertices to OpenGL.
8 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);``````

This needs to be done only once.

Now, in our main loop, where we used to draw “nothing”, we can draw our magnificent triangle :

`````` 1 // 1rst attribute buffer : vertices
2 glEnableVertexAttribArray(0);
3 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
4 glVertexAttribPointer(
5    0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
6    3,                  // size
7    GL_FLOAT,           // type
8    GL_FALSE,           // normalized?
9    0,                  // stride
10    (void*)0            // array buffer offset
11 );
12 // Draw the triangle !
13 glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
14 glDisableVertexAttribArray(0);``````

If you’re on lucky, you can see the result (don’t panic if you don’t) :

Now this is some boring white. Let’s see how we can improve it by painting it in red. This is done by using something called shaders.

In the simplest possible configuration, you will need two shaders : one called Vertex Shader, which will be executed for each vertex, and one called Fragment Shader, which will be executed for each sample. And since we use 4x antialising, we have 4 samples in each pixel.

Shaders are programmed in a language called GLSL : GL Shader Language, which is part of OpenGL. Unlike C or Java, GLSL has to be compiled at run time, which means that each and every time you launch your application, all your shaders are recompiled.

The two shaders are usually in separate files. In this example, we have SimpleFragmentShader.fragmentshader and SimpleVertexShader.vertexshader . The extension is irrelevant, it could be .txt or .glsl .

So here’s the code. It’s not very important to fully understand it, since you often do this only once in a program, so comments should be enough. Since this function will be used by all other tutorials, it is placed in a separate file : common/loadShader.cpp . Notice that just as buffers, shaders are not directly accessible : we just have an ID. The actual implementation is hidden inside the driver.

`````` 1 GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
2
6
11 		std::string Line = "";
13 			VertexShaderCode += "\n" + Line;
15 	}else{
16 		printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
17 		getchar();
18 		return 0;
19 	}
20
25 		std::string Line = "";
27 			FragmentShaderCode += "\n" + Line;
29 	}
30
31 	GLint Result = GL_FALSE;
32 	int InfoLogLength;
33
34
36 	printf("Compiling shader : %s\n", vertex_file_path);
37 	char const * VertexSourcePointer = VertexShaderCode.c_str();
40
44 	if ( InfoLogLength > 0 ){
48 	}
49
50
51
53 	printf("Compiling shader : %s\n", fragment_file_path);
54 	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
57
61 	if ( InfoLogLength > 0 ){
65 	}
66
67
68
71 	GLuint ProgramID = glCreateProgram();
75
76 	// Check the program
78 	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
79 	if ( InfoLogLength > 0 ){
80 		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
81 		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
82 		printf("%s\n", &ProgramErrorMessage[0]);
83 	}
84
85
88
91
92 	return ProgramID;
93 }``````

Let’s write our vertex shader first. The first line tells the compiler that we will use OpenGL 3’s syntax.

``1 #version 330 core``

The second line declares the input data :

``1 layout(location = 0) in vec3 vertexPosition_modelspace;``

Let’s explain this line in detail :

• “vec3” is a vector of 3 components in GLSL. It is similar (but different) to the glm::vec3 we used to declare our triangle. The important thing is that if we use 3 components in C++, we use 3 components in GLSL too.
• “layout(location = 0)” refers to the buffer we use to feed the vertexPosition_modelspace attribute. Each vertex can have numerous attributes : A position, one or several colours, one or several texture coordinates, lots of other things. OpenGL doesn’t know what a colour is : it just sees a vec3. So we have to tell him which buffer corresponds to which input. We do that by setting the layout to the same value as the first parameter to glVertexAttribPointer. The value “0” is not important, it could be 12 (but no more than glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &v) ), the important thing is that it’s the same number on both sides.
• “vertexPosition_modelspace” could have any other name. It will contain the position of the vertex for each run of the vertex shader.
• “in” means that this is some input data. Soon we’ll see the “out” keyword.

The function that is called for each vertex is called main, just as in C :

``1 void main(){``

Our main function will merely set the vertex’ position to whatever was in the buffer. So if we gave (1,1), the triangle would have one of its vertices at the top right corner of the screen. We’ll see in the next tutorial how to do some more interesting computations on the input position.

``````1 gl_Position.xyz = vertexPosition_modelspace;
2   gl_Position.w = 1.0;
3 }``````

gl_Position is one of the few built-in variables : you *have *to assign some value to it. Everything else is optional; we’ll see what “everything else” means in Tutorial 4.

For our first fragment shader, we will do something really simple : set the color of each fragment to red. (Remember, there are 4 fragment in a pixel because we use 4x AA)

``````1 #version 330 core
2 out vec3 color;
3 void main(){
4   color = vec3(1,0,0);
5 }``````

So yeah, vec3(1,0,0) means red. This is because on computer screens, colour is represented by a Red, Green, and Blue triplet, in this order. So (1,0,0) means Full Red, no green and no blue.

## Putting it all together

``````1 // Create and compile our GLSL program from the shaders

Now inside the main loop, first clear the screen. This will change the background color to dark blue because of the previous glClearColor(0.0f, 0.0f, 0.4f, 0.0f) call:

``1 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);``

``````1 // Use our shader