为什么我需要glClear(GL_DEPTH_BUFFER_BIT)

时间:2015-01-25 13:39:30

标签: opengl

这是我的代码:

#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>

int main () {
    // start GL context and O/S window using the GLFW helper library
    if (!glfwInit ()) {
        fprintf (stderr, "ERROR: could not start GLFW3\n");
        return 1;
    }

    // uncomment these lines if on Apple OS X
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
     glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
     glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
     glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
    if (!window) {
        fprintf (stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent (window);

    // start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit ();

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf ("Renderer: %s\n", renderer);
    printf ("OpenGL version supported %s\n", version);

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable (GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

    /* OTHER STUFF GOES HERE NEXT */

    float points[] = {
        0.0f,  0.5f,  0.0f,
        0.5f, -0.5f,  0.0f,
        -0.5f, -0.5f,  0.0f
    };
    GLuint vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
    GLuint vao = 0;
    glGenVertexArrays (1, &vao);
    glBindVertexArray (vao);
    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    const char* vertex_shader =
    "#version 410\n"
    "layout(location = 0) in vec4 vPosition;"
    "void main () {"
    "  gl_Position = vPosition;"
    "}";
    const char* fragment_shader =
    "#version 410\n"
    "out vec4 frag_colour;"
    "void main () {"
    "  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
    "}";
    GLuint vs = glCreateShader (GL_VERTEX_SHADER);
    glShaderSource (vs, 1, &vertex_shader, NULL);
    glCompileShader (vs);
    GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
    glShaderSource (fs, 1, &fragment_shader, NULL);
    glCompileShader (fs);
    GLuint shader_programme = glCreateProgram ();
    glAttachShader (shader_programme, fs);
    glAttachShader (shader_programme, vs);
    glLinkProgram (shader_programme);
    while (!glfwWindowShouldClose (window)) {
        // wipe the drawing surface clear
        glClear (GL_DEPTH_BUFFER_BIT);
        const GLfloat color[]={0.0,0.2,0.0,1.0};
        //glClearBufferfv(GL_COLOR,0,color);
        glUseProgram (shader_programme);
        glBindVertexArray (vao);
        // draw points 0-3 from the currently bound VAO with current in-use shader
        glDrawArrays (GL_TRIANGLES, 0, 3);
        // update other events like input handling
        glfwPollEvents ();
        // put the stuff we've been drawing onto the display
        glfwSwapBuffers (window);
    }    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}

当我评论行glClear(GL_DEPTH_BUFFER_BIT)时,显示的窗口没有显示任何内容,此例程是否重要? 我正在使用Xcode和Mac OS X 10.1.2,请帮助我,谢谢

2 个答案:

答案 0 :(得分:9)

深度缓冲区用于确定渲染的几何体是否比先前渲染的几何体更接近于查看器。这样可以消除隐藏的几何体。

每个片段(像素)执行该测试。每次渲染片段时,都会将其深度与深度缓冲区中的相应值进行比较。如果新深度更大,则通过深度测试消除片段。否则,将片段写入颜色缓冲区,并使用新片段的深度更新深度缓冲区中的值。功能由您在设置期间进行的这些调用控制:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

这样,如果一个片段被多个三角形覆盖,最终像素的颜色将由最接近观察者的几何体给出。

现在,如果您没有在每个帧的开头清除深度缓冲区,则与上述深度缓冲区中的值的比较将使用在深度缓冲区中发生的任何值。这可以是前一帧的值,也可以是未初始化的垃圾值。因此,即使当前帧中没有片段被绘制在之前的相同位置,也可以通过深度测试来消除片段。在极端情况下,深度测试会消除所有碎片,你什么也看不见。

除非您确定要对窗口中的所有像素进行渲染,否则您还需要在帧的开头清除颜色缓冲区。所以你明确的电话应该是:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

答案 1 :(得分:0)

//清除绘图表面         ,glClear(GL_DEPTH_BUFFER_BIT);

该注释意味着代码意味着它清除了深度缓冲区。深度缓冲区是帧缓冲区的一部分,它使对象被其前面的其他对象阻挡。在不清除深度缓冲区的情况下,您将绘制上一个绘图的深度结构。