Checking OpenGL context version

References

Place the following code in your program. Other than the #include and typedef, it goes after the call to glMakeCurrent:


typedef union { struct { uint8_t major, minor; }; uint16_t full; } glversion_t;

This is a convenient type I made in order to be able to compare versions easily.

const char *gl_version_string = (const char *)glGetString (GL_VERSION); assert (gl_version_string); assert (strlen(gl_version_string) >= 3); printf ("OpenGL version string: %s\n", gl_version_string); const char major = gl_version_string[0]; assert (major >= '1' && major <= '4'); const char minor = gl_version_string[2]; assert (major >= '0' && major <= '6'); glversion_t glversion = {.major = major - '0', .minor = minor - '0'}; printf ("GL major.minor version: %d.%d\n", glversion.major, glversion.minor);

The GL_VERSION string has been available since OpenGL 1.0, so it's guaranteed to exist in any OpenGL context. You can use it to check the major.minor version of your context. If these values come back as anything from 1.0 to 3.0, you have all the information you need and your context supports all functionality from the given version and earlier.

if (glversion.full >= (glver_t){.major=3,.minor=2}.full) { // Verion 3.2 onward have core and compatibility profiles. Core removes deprecated functionality. int context_profile = 0; glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &context_profile); if (context_profile & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) printf ("Compatibility context\n"); else if (context_profile & GL_CONTEXT_CORE_PROFILE_BIT) printf ("Core context\n"); else printf ("Unable to retrieve GL_CONTEXT_PROFILE_MASK even though version is >= 3.2???\n"); }

In OpenGL 3.2, "profiles" were introduced. From 3.2 on, an OpenGL implementation is only required to implement the "core" profile - the main API with all deprecated funcionality removed. "Compatibility" profiles also include the older, deprecated functionality and effectively support the reported version and all previous API versions. These days, all modern drivers implement a 4.0+ compatibility profile so you can expect to practically always have the old APIs supported.

else if (glversion.full == (glver_t){.major=3,.minor=1}.full) { // Version 3.1 removed deprecated functionality, but did not have profiles yet. const char *extensions = (const char *)glGetString (GL_EXTENSIONS); assert (extensions); if (strstr (extensions, "GL_ARB_compatibility")) printf ("GL_ARB_compatibility is present\n"); else printf ("GL_ARB_compatibility NOT present\n"); }

OpenGL 3.1 is unique in that in removed the functionality that was deprecated in 3.0, but did not introduce profiles. Instead, old API support is reported through the GL_ARB_compatibility extension, which we search for in the GL_EXTENSIONS string. If your context version is 3.1, but GL_ARB_compatibility is not supported, the deprecated APIs are not supported. In practice, you will probably never encounter this.

When getting started with OpenGL, I recommend targetting one specific API version. Later, when you have more experience, you can expand into supporting multiple API versions in order to guarantee wider compatibility with your shipping software.