I would like to do this :
I want to specify the opengl version to use, if the version is not supported I want to check the greatest supported opengl version.
But there is a problem, I need to create a context to call the glGetString function, and I also need to pass the opengl version for creating the opengl context.
How should I do this ? Check the result of context creation and if it fails, trying to recreate the context with a lower version each times it fails ?
Mmm…, it wouldn’t be great if I must do this.
That’s the only guaranteed solution. Most (all?) existing implementations will use the highest version which is compatible with the requested version (and for 3.2+, the requested profile), but an implementation is allowed to use the requested version even if a higher version available.
This assumes that you’re using a recent context-creation function which supports requesting a specific version and profile, e.g. wglCreateContextAttribsARB(). If you use an older interface such as wglCreateContext(), you may get a version below that which the driver can support; in particular, you’ll always get something that’s compatible with 1.1 (i.e. you’ll never get 3.1 or a core profile context).
The simple approach is to request the lowest version which you can use then check whether you actually got something later. On most systems, that will work just as well as requesting a sequence of versions.
Bear in mind that on Mac, you have a choice between 2.1 or 3.2+ core profile. 3.2+ compatibility profile isn’t supported there; if you want newer features, you’ll have to stick to the core profile. In turn, that complicates supporting versions prior to 3.0, as you must create and bind a VAO when using the core profile.