Starting with OpenGL 4.5

When I started 3D programming, OpenGL 2 did not yet exist. I grew up using glBegin and 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.

OpenGL Tutorials

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.

Picking libraries

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:

#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.


3 thoughts on “Starting with OpenGL 4.5

  1. I’m so glad someone’s finally documenting how to get a modern version of OpenGL set up for development! Last time I looked, I couldn’t find anything useful – the latest tutorials were OpenGL 3.0 tutorials that relied functions that were removed in OpenGL 3.1.

    I also came to the same conclusion – there are no up-to-date header files anywhere. Using a loader generator is the only way to get recent Core functions.

    • There are some 3.3 tutorials, which I suppose work equally well for 4.0, since they’re quite similar. However, in 4.1 through 4.5 quite some important functionality has been added. Including the debug layer I already mentioned in my post, but also AZDO (approaching zero driver overhead) functionality, such as direct state access and persistently mapped buffers. There is also AZDO functionality that didn’t make it into core (yet), such as bindless textures and sparse textures and buffers. The downside of al this great new functionality is that the OpenGL API has become even more cluttered than it already was. The GPU hardware has changed a lot in the 23 years that OpenGL already exists. For Khronos, the group behind OpenGL, this was a reason to redevelop a new graphics API from scratch, called Vulkan, to be released somewhere later this year. Until then I’ll be using AZDO-OpenGL to get some experience with modern GPU programming.

      I’ve already a short list of things I want to write about: context creation, debugging, shader compilation, buffer creation/use and benchmarking.

  2. Pingback: Creating an OpenGL 4.5 context using SDL2 and Glad | Voxel-Engine

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s