c++ - 无法启动 OpenGL 窗口

标签 c++ opengl

据我所知,我已经正确安装了所有库,但由于某些原因,glfwWindowCreate 最终返回 NULL。我目前使用的是戴尔 XPS 15,所以我想知道这是否与我可能在集成显卡上运行它有关,因为它的要求不足以启动 1050ti。总的来说,我是 OpenGL 的新手,所以我不确定我的代码是否正确编写,所以我也会在这里发布:

glewExperimental = true;
if (!glewInit())
{
    fprintf(stderr, "Failed to initialize GLEW!\n");
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow* window;
window = glfwCreateWindow(1920, 1080, "Test Window", NULL, NULL);
if (window == NULL)
{
    fprintf(stderr, "Failed to initialize the window.");
    std::cin.ignore();
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
    fprintf(stderr, "Failed to initialize GLEW!");
    return -1;
}
std::cin.ignore();
std::cin.ignore();

我刚刚将我的 NVIDIA 驱动程序更新到最新更新,所以(可能)不是我希望的那样。不幸的是,我似乎无法让它打开一个窗口。

最佳答案

您错过了初始化 GLFW libraray . GLFW 必须由 glfwInit 初始化, 在使用之前。

GLEW libraray必须在创建有效的 OpenGL 上下文并成为当前上下文后进行初始化。参见 Initializing GLEW .

像这样更改您的代码,以解决您的问题:

if ( glfwInit() != GLFW_TRUE ) // intialize GLFW
{
    // error handling 
}

glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow* window;
window = glfwCreateWindow(1920, 1080, "Test Window", NULL, NULL);
if (window == NULL)
{
    // error handling
}
glfwMakeContextCurrent(window);

// now the OpenGL context is valid and current

glewExperimental = true;
if (glewInit() != GLEW_OK) // initialize GLEW
{
    // error handling
}

关于c++ - 无法启动 OpenGL 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52350511/

相关文章:

c++ - 线程池C++实现问题

c++ - 派生类虚函数未执行

c++ - AcceptEx 返回 1022 (WSAEINVAL)...我做错了什么?

c++ - 读取(简单)程序集以查找使用 C++ 模板产生的编译时计算

opengl - 如何在 GLSL 1.3 和 OpenGL 2.1 中使用位操作

OpenGL:计算着色器工作组是否并行执行?

c# - OpenGL Quad 仅与背景混合

c++ - 如何检测一个类是否在 C++ 中具有隐式构造函数和原始成员?

opengl - glBufferData 中的提示标志实际上有什么作用吗?

opengl - 使用 OpenGL/GLSL 的降尺度(下采样)技术实现光晕效果