为什么使用此 OpenGL 上下文创建的 window 会以透明方式打开?

Why would the window created with this OpenGL context open as transparent?

执行后,背景应该呈现为深蓝色,但我显然漏掉了什么。 window 而是使用与紧随其后的图像相同的背景呈现(例如,其他打开的 windows 或桌面等)。我无法确定问题所在。

目前我在编译期间没有使用任何 -std,我正在使用以下与输出可执行文件的链接:

-lGL -lGLU -lGLEW -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXcursor -lXinerama

这是我的 .cpp 文件的内容:

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

#include <GL/glew.h>

#include <GLFW/glfw3.h>
GLFWwindow* window;

#include <glm/glm.hpp>
using namespace glm;

int main( void )
{

    //Initialize GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failes to intialize GLFW\n" );
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Open a Window and create it's OpenGL context
    window = glfwCreateWindow( 1024, 768, "playground", NULL, NULL);
    if( window == NULL) {
        fprintf( stderr, "Failed to open GLFW window.\n" );
        glfwTerminate();
        return -1;
    }

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

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    do{

        glfwSwapBuffers(window);
        glfwPollEvents();

    }

    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );

    glfwTerminate();

    return 0;

}

我是OpenGL新手。请随意评论整体结构,或者是否有任何 should/must 被完全重写。谢谢!

您刚刚忘记在渲染循环中清除 window:

do{

    glClear(GL_COLOR_BUFFER_BIT); // Clear background with clear color.
    glfwSwapBuffers(window);
    glfwPollEvents();

}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
       glfwWindowShouldClose(window) == 0 );

glClear 通常是使用 OpenGL 的每个渲染循环的第一个调用之一,为您提供一个新的框架来处理。