OpenGL added features from 1.0 through 3.0. At that point, it was decided that the older fixed-function API should be deprecated. In 3.1, many features were removed, and from 3.2 onward two profiles differentiated these differences: core and compatibility. Core profile only has the new API, and compatibility still supports the deprecated API. When you request a specific version, the graphics driver will give you a version compatible with the version you requested. These days that most often means a 4.6 compatibility or core profile context.
Build with:
gcc main.c -lopengl32 -lgdi32
const auto gl_context_temp = wglCreateContext (window_context); assert (gl_context_temp);
{ const auto result = wglMakeCurrent (window_context, gl_context_temp); assert (result); }
You may be surprised to see that we're creating a context without specifying the version! In order to use the GL function required to create a specific context version, we need to already have a valid OpenGL context, so we create one the non-specific way initially.
typedef HGLRC (*wglCreateContextAttribsARB_t) (HDC hDC, HGLRC hshareContext, const int *attribList); wglCreateContextAttribsARB_t wglCreateContextAttribsARB;
wglCreateContextAttribsARB = (wglCreateContextAttribsARB_t)wglGetProcAddress ((LPCSTR)"wglCreateContextAttribsARB"); assert (wglCreateContextAttribsARB);
We can't link to wglCreateContextAttribsARB in the normal way, since it may or may not be present depending on the GPU driver. Instead, we have to retrieve a pointer to the function with glXGetProcAddress. When retrieving OpenGL functions, I like to create a typedef of the function pointer type.
const int context_attributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};
const auto gl_context = wglCreateContextAttribsARB (window_context, 0, context_attributes);
assert (gl_context);
Context attributes are created as a NULL-terminated array of ints. Here I've set the major.minor version to 3.2 and specified the core profile. All available attributes are below.
WGL_CONTEXT_MAJOR_VERSION_ARB
WGL_CONTEXT_MINOR_VERSION_ARB
These together must specify a valid OpenGL version, all of which are: 1.0-1.5, 2.0-2.1, 3.0-3.3, 4.0-4.6.
WGL_CONTEXT_PROFILE_MASK_ARB
WGL_CONTEXT_FLAGS_ARB
WGL_CONTEXT_LAYER_PLANE_ARB
The layer plane that the rendering context is bound to. The same as the second parameter of wglCreateLayerContext().