How can i know which opengl version is supported by my system
Asked Answered
J

2

8

Look at this very basic C++ code:

if(!glfwInit())
{
    return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
if (window==NULL)
{
    return -1;
}
glfwMakeContextCurrent(window);

std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl;

I do not understand how i can "detect" max opengl version i can set in the lines:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

This line cannot be placed before glfwMakeContextCurrent:

glGetString(GL_VERSION)

So my question is how can i detect the versions of opengl my system supports at the beginning of the program.

Thanks

Jeremy answered 1/10, 2017 at 9:9 Comment(1)
Know that versions are mostly a collection of required extensions for a vendor to implement. You are unlikely to use every function a version has to offer anyway so focus on checking the extensions for the functions you use.Madai
O
8

See GLFW guid - Window creation hints which clearly says:

GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.

OpenGL: GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR are not hard constraints, but creation will fail if the OpenGL version of the created context is less than the one requested. It is therefore perfectly safe to use the default of version 1.0 for legacy code and you will still get backwards-compatible contexts of version 3.0 and above when available.

While there is no way to ask the driver for a context of the highest supported version, GLFW will attempt to provide this when you ask for a version 1.0 context, which is the default for these hints.


This means, if you want to get the highest possible OpenGL context, then you can completely skip glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, ) and glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, ).

After you have created a context, you can ask for the context version, with glGetString(GL_VERSION).

But if your application requires a minimum OpenGL version, you need to tell that to GLFW, with:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, required_major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, required_minor);

glfwCreateWindow will fail, if the requirements cannot be fulfilled.


The answer to your question

How can i know which opengl version is supported by my system?

is:

You have to create an OpenGL context first, then you can ask for the version by glGetString(GL_VERSION).


Correction to the answer

As mentioned in the comment, this approach is going to fail when you try to create a core profile context.

This means you cannot use:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Ocrea answered 1/10, 2017 at 9:41 Comment(2)
In practice, this approach is going to fail when one is trying to create a core profile context.Chordate
Well, it is also not wrong, especially in the context given where the user doesn't create a core profile. With the remark from the update, I don't see any reason to delete it. Also note that the downvote is not from me.Chordate
C
4

Actually, trying to query the highest supported GL version is quite useless. You should just ask for the lowest GL version (and the most strict profile) your code can work with, and if that is not available, it is not going to work anyway.

If you want to optionally support some features, use of the extension mechanism is the way to go.

If you really want some higher version context on some machines, and a lower one on others, the only reliable way will be to iteratively try to create the context, backwards from the highest version your code can make use of, to the lowest, and just stop when the creation succeeds. You won't get any unknown newer version that way, but since the rest of your code won't make use of any newer features, it is a moot point.

The only reason I can see why you might want some "unknown" newer GL version would be the case of a library, where you create the context and do not know what is to be done with it - like it's the case with GLFW itself. But GLFW has the same issue. There is simply no reliable API to query the highest supported GL version. The recommendation in the GLFW docs, mentioned in @Rabbid76's answer will only provide you the highest available legacy GL version (which limits you to 2.1 on OSX and 3.0 on mesa/linux). It won't work for core profiles, but core profiles are the only reliable way to get modern GL on all platforms.

Having said that all that, I have to admit that I did see code before, which actually iteratively tried the contexts, but started with 5.something, while at the time of writing this answer, the highest existing GL spec was 4.6. Not sure what they were trying to do there, and also no idea which x for 4.x they did start with, after 5.0 failed.

Chordate answered 1/10, 2017 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.