OpenGL is a graphics API that has several versions, each introducing new features and functionality.
OpenGL is a graphics API that has several versions, each introducing new features and functionality. To use these latest features, it is necessary to load version-specific functions at runtime using libraries:
GLAD is a library used to generate OpenGL function loaders. It helps load the functions specific to the OpenGL version you are using at runtime.
GLFW is a library that supports OpenGL window creation, input handling, and context management. It makes it easy to set up an OpenGL window and context, but it doesn’t directly handle OpenGL function loading.
GLFW, SDL2, SFML or similar can be used in conjunction with OpenGL to make it easier to manage the OpenGL window and context while you focus on the graphics operations themselves.
Vulkan (also developed by the same company, Khronos Group) and Direct3D are alternatives to OpenGL.
Unzip glad.zip and move the folders: include and src to your OpenGLProject\;
Unzip glfw-3.3.9.bin.WIN64.zip and move the include/GLFW folder to OpenGLProject\include;
Move glfw-3.3.9.bin.WIN64\lib-mingw-64\glfw3.dll to the root of your OpenGLProject\;
Create a folder inside the OpenGLProject root named lib;
Move the file glfw-3.3.9.bin.WIN64\lib-mingw-64\libglfw3dll.a into ProjectOpenGL\lib;
Create a main.cpp file inside src\: OpenGLProject\src;
Create a folder in the root of your OpenGLProject\ named .vscode and inside it create a file named: task.json;
Edit this file as below:
main.cpp:
And task.json:
To compile, open the built-in VSCode terminal, enter your project: cd OpenGLProject\ and run the command below:
Then just run the a.exe file, if a window appears with a black background and the title: “C++ OpenGL on Windows” everything is ok!
Installation on Ubuntu
Use APT to install the following packages below:
Create a C++ file, example main.cpp and insert the content below:
To compile run:
Creating a basic event
Let’s create a basic event that changes the background color of the window when clicked and when clicked again it returns the previous color and so on.
1. Create a global variable:
2. Create a function that will change the background color:
3. Add a function before the main loop that will call the callback function:
4. Add the black background color at the beginning of the loop, but defined through our array that we defined as global:
Attention, the video is in Portuguese, but it is possible to follow the procedures mentioned here regardless of your language. If you prefer, use YouTube’s automatic translation feature!