In this section, we’ll be looking at how to draw a triangle in OpenGL using the GLFW library. To begin with, let’s go to the file in which we wrote code to create an OpenGL rendering window using the GLFW library in the previous chapter, and make the necessary changes to it. Let's take a look at the following steps to understand the code required to draw a triangle:
- We'll begin by including the essential header files in our code:
#include <iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> // Window dimensions const GLuint WIDTH = 800, HEIGHT = 600;
- To create shapes in modern OpenGL, we need to create shaders. So, let’s begin by adding some shaders to our code. Firstly, we’ll add a constant,
GLchar *
, and we’ll call itvertexShaderSource
. This is going to be a string and its version will be330 core
:
// Shaders const GLchar* vertexShaderSource = "#version 330 core\n"
The 330 core
defines the core shader language version...