【问题标题】:Failed to create GLFW window?创建 GLFW 窗口失败?
【发布时间】:2021-05-15 01:56:04
【问题描述】:

我尝试用最简单的代码创建一个窗口:

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

int main(void) {
    glfwInit();
    glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
    glfwWindowHint(GLFW_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac

    GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window

    if (window == NULL) {
        fprintf(stderr, "Failed to create window\n");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    return 0;
} 

这是我的 Makefile:

game:
    g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2

当我编译我的代码时没有错误,但是当我尝试播放我的代码时出现这个错误:

Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above

有人可以帮助我吗?我不知道为什么我的窗口不想被创建...

【问题讨论】:

  • 请注意,与代码中的注释相反,您实际上是在请求 4.4 版本的上下文,而不是 4.1。
  • 试了很多版本还是不行

标签: c++ macos opengl glfw


【解决方案1】:

GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR 不是 glfwWindowHint 的有效参数:

glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
               ^^^^^^^^^^^^^^^^^^ nope
glfwWindowHint(GLFW_VERSION_MINOR, 3);
               ^^^^^^^^^^^^^^^^^^ also nope

请改用GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR

根据the docs,强调我的:

GLFW_CONTEXT_VERSION_MAJORGLFW_CONTEXT_VERSION_MINOR 指定创建的上下文必须兼容的客户端 API 版本。这些提示的确切行为取决于请求的客户端 API。

注意:不要将这些提示与提供 GLFW 标头 API 版本的 GLFW_VERSION_MAJORGLFW_VERSION_MINOR 混淆。

【讨论】:

  • 谢谢老兄!它现在工作正常......我很愚蠢抱歉。
  • @miishuriinu:你打赌!并感谢 提供修订版 1 minimal reproducible example!这些帮助极大地
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 2014-06-03
  • 2012-07-22
  • 2021-03-17
  • 2021-12-10
相关资源
最近更新 更多