在 C++ 中创建 GLFW OpenGL Window 时遇到问题

Trouble Creating a GLFW OpenGL Window in C++

这是我正在处理的当前代码:

int main() {

    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", NULL, NULL);

    if (nullptr == window) {
        std::cout << "Failed to create GLFW Window" << std::endl;
        glfwTerminate();

        return EXIT_FAILURE;
    }

在运行时,window 未创建,我收到失败消息。当我定义它时,我不明白为什么 window 是 nullptr。我错过了什么吗?感谢您的帮助。

根据 GLFW 文档,如果请求的 OpenGL 上下文版本是 GLFW_OPENGL_PROFILE window 提示 必须 的值小于 3.2(并且 GLFW 有 a platform-independent check built-in whenever glfwCreateWindow is called)。

参见:https://www.glfw.org/docs/3.3/window_guide.html#GLFW_OPENGL_PROFILE_hint

GLFW_OPENGL_PROFILE specifies which OpenGL profile to create the context for. Possible values are one of GLFW_OPENGL_CORE_PROFILE or GLFW_OPENGL_COMPAT_PROFILE, or GLFW_OPENGL_ANY_PROFILE to not request a specific profile. If requesting an OpenGL version below 3.2, GLFW_OPENGL_ANY_PROFILE must be used. If OpenGL ES is requested, this hint is ignored.

特别是以下部分:“如果请求 OpenGL 版本低于 3.2,则必须使用 GLFW_OPENGL_ANY_PROFILE。”

在您的案例中,您将遇到 GLFW 错误。特别是 exactly this one - regardless of the platform/OS, which you would see if you had setup a GLFW error handler function via glfwSetErrorCallback().