Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

opengl

glfw

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

like image 588
Bob5421 Avatar asked Oct 01 '17 09:10

Bob5421


1 Answers

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);
like image 195
Rabbid76 Avatar answered Nov 15 '22 14:11

Rabbid76