【问题标题】:OpenGL glfw black windowOpenGL glfw黑色窗口
【发布时间】:2016-02-09 17:48:48
【问题描述】:

glfw 有个小问题。

我的代码很简单,我只想创建一个空窗口。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main(void) {


// initialise the windows
GLFWwindow *window;

if (!glfwInit()) {

    return -1;
}

// create  a windows

window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
if (!window) {
    fprintf(stderr, "Failed to initialize GLFW\n");
    glfwTerminate();
    return -1;
}


// make the window's current context
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// loop until the window close

while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // swap back and front buffers

    glfwSwapBuffers(window);


    // poll the events

    glfwPollEvents();
}

std::cout << "finished ";
glfwTerminate();

return 0;

}

这段代码可以编译,但是当我运行它时,我只有一个白色的窗口。窗口的标题是正确的,但里面的一切都是白色的...... 我尝试像这样使用 glClearColor

while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1, 1, 0, 0);

但我的窗户仍然是白色的...... 我使用 Visual Studio 2015。

如何获得黑窗?

编辑:

我忘了添加这个: glfwMakeContextCurrent(window);

【问题讨论】:

  • glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 替换glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 可能吗?
  • 您可以将其放在答案中并关闭主题。 @Skarwim

标签: opengl glfw glew


【解决方案1】:

对于未来的访问者,我将发布编辑作为正式答案。

这里缺少的是通过调用glfwMakeContextCurrent(window);将新创建的窗口设置为当前的OpenGL上下文

有趣的是,代码中的 cmets 说你正在这样做

// make the window's current context

但是你没有调用上面的方法,而是在之后立即设置背景颜色。如果您在此注释之后和设置背景颜色之前添加上述方法调用,那么当我运行您的代码时它可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多