opengl - 我如何知道我的系统支持哪个opengl版本

标签 opengl glfw

看看这个非常基本的 C++ 代码:

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;

我不明白如何“检测”我可以在行中设置的最大 opengl 版本:

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

此行不能放在 glfwMakeContextCurrent 之前:

glGetString(GL_VERSION)

所以我的问题是如何在程序开始时检测我的系统支持的 opengl 版本。

谢谢

最佳答案

参见GLFW guid - Window creation hints其中清楚地表明:

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.


这意味着,如果你想获得尽可能高的OpenGL上下文,那么你可以完全跳过glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, )glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, ) .

创建上下文后,您可以询问上下文版本, glGetString(GL_VERSION) .

但是,如果您的应用程序需要最低 OpenGL 版本,则需要将其告知 GLFW,方法是:

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

glfwCreateWindow如果不能满足要求,就会失败。


回答你的问题

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

是:

您必须先创建OpenGL上下文,然后您可以通过glGetString(GL_VERSION)询问版本.


更正答案

正如评论中提到的,当您尝试创建核心配置文件上下文时,此方法将失败

这意味着您不能使用:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

关于opengl - 我如何知道我的系统支持哪个opengl版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46510889/

相关文章:

c - 使用 OpenGL 着色器语言编写一个简单的着色器

c++ - 这种模型失真的来源是什么

opengl - 使用 OpenGL/GLFW/FreeType 绘制文本会生成白色矩形而不是字形

java - 在 MAC OS 下使用 GLFW 遇到问题

c++ - Visual Studio 2012 C++ OpenGL GLFW 链接器错误

python - 了解 OpenGL 中的光与 Material 属性

c++ - 无法访问对象指针,即使它没有被删除

macos - 什么会阻止 OpenGL glDrawPixels 在某些视频卡上工作?

objective-c - CGContextRef 作为 OpenGL 上下文

c++ - Cygwin OpenGL 编译返回对 imp_iob 的 undefined reference