OpenGL Tutorial for Beginners
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.
Installation on Windows
You must have MinGW installed, if not, use this link: How to Install GCC/G++ MinGW on Windows.
Download GLAD: https://glad.dav1d.de/
- → Language: C/C++;
- → API > gl: Version 3.3 (for compatibility with the other package that we will download, which only has version 3.3.9);
- → Profile: Core,
- Generate a loader
- → GENERATE → click on
glad.zip
.
And also download GLFW: https://www.glfw.org/download.html
- → Windows pre-compiled binaries
- → 64-bit Windows binaries;
- → click to download:
glfw-3.3.9.bin.WIN64.zip
- Create a folder named: OpenGLProject
- Unzip
glad.zip
and move the folders:include
andsrc
to yourOpenGLProject\
; - Unzip
glfw-3.3.9.bin.WIN64.zip
and move theinclude/GLFW
folder toOpenGLProject\include
; - Move
glfw-3.3.9.bin.WIN64\lib-mingw-64\glfw3.dll
to the root of yourOpenGLProject\
; - Create a folder inside the
OpenGLProject
root namedlib
; - Move the file
glfw-3.3.9.bin.WIN64\lib-mingw-64\libglfw3dll.a
intoProjectOpenGL\lib
; - Create a
main.cpp
file insidesrc\
: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:
Now just recompile and test!
Useful links
Watch the video
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!
Comments