When I started 3D programming, OpenGL 2 did not yet exist. I grew up using
glEnd, but quickly learned that you needed to use vertex arrays to get any decent performance. Since OpenGL 3.3 core profile, both are gone and you need to use buffer objects to send a list of vertices to your GPU. However, the biggest change is that the fixed pipeline is gone, which means that you’re forced to use shaders.
During my master in computer science and engineering, I did get some experience in using shaders and their programming language GLSL. However, I used Qt4 which provided a very nice class for dealing with shader programs. So, I have no experience with compiling shader programs and binding data using the OpenGL API. Furthermore, the GLSL language itself has also changed quite a lot. Basically, I have to learn OpenGL from scratch again.
There are also big differences between minor OpenGL 4 versions. While no functionality is removed, the added functionality is sometimes a huge improvement over the functionality it seeks to replace, for example: the debug output added in 4.3. I will be using OpenGL 4.5, for the sole reason that my GPU claims to support it.
Normally, I would pick up a good tutorial; read through it; try out some of the programs; and I’d be back on track in no-time. Unfortunately, good OpenGL 4 tutorials don’t exist (yet, or at least I couldn’t find them). Tutorials on modern OpenGL are scarce, and those that do exist focus on getting things done, rather than giving the reader a good understanding of OpenGL. In my case, the later is vital, as I want to use OpenGL for anything except what it is normally used for: rendering triangles.
What I little I did found on OpenGL 4 either focused too much on non-OpenGL stuff, lacked any explanation whatsoever or wasn’t cross platform, but despite that, they are still useful resources. Because of the lack of good tutorials, I decided to write about my experiences with learning OpenGL 4. It is not intended as a complete OpenGL tutorial, but it should still be useful for learning OpenGL. I will assume some basic knowledge of C/C++, SDL, CMake, graphics programming and linear algebra though.
Starting from version 3.3, OpenGL no longer provides methods for manipulating the camera and projection matrices. Hence, we should use an external linear algebra library for that. Almost everyone uses GLM, which is an OpenGL oriented library that provides replacements for all the camera and projection matrix manipulation methods used prior to OpenGL 3.3 and also follows the GLSL naming convention. I’m already using it in my voxel-engine and will keep using it now.
Another library that I’m already using in my voxel-engine project, is SDL2. We will use it for window creation; OpenGL context creation; and, as we will soon figure out, for looking up OpenGL functions.
Last, we need to pick an extension loader. As I’m running Linux, my first choice was using glext.h, whose headers can be included using:
#define GL_GLEXT_PROTOTYPES #include "GL/gl.h"
Unfortunately, some of the functions where initialized as null-pointers. Those functions should have been available, because I selected the 4.5 core profile. My next choice was glew.h, with which I had some good experiences in the past while porting OpenGL code to windows. However that only worked if I used the hackish
glewExperimental = GL_TRUE;. Worse, it isn’t a standalone solution as it did not provide
glGetString, so I still had to include
GL/gl.h. And as if that wasn’t enough, my Linux distribution provided an old version of glew that only supported OpenGL up to version 4.4. In the end, I settled for glad, an OpenGL loader generator. I selected OpenGL 4.5; core profile; no extensions; and no loader. Downloaded the package; added the source files to my source folder; initialized it with
gladLoadGLLoader(SDL_GL_GetProcAddress); and I’m good to go.