简体   繁体   English

OpenGL-深度测试不起作用

[英]OpenGL - Depth test doesn't work

I am leanring OpenGL and at the moment I learn how to add the 3rd dimension. 我正在学习OpenGL,现在我学习如何添加第3维。 I want to make a Cube but only one of its sides is not transparent: 我想制作一个多维数据集,但其中只有一侧不透明:

当前正面是透明的

蓝色面不透明

My draw-function looks like this: 我的绘制函数如下所示:

void draw()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH);

    glUseProgram(program);

    // The attributes are like they have to be, nothing wrong here
    glBindBuffer(GL_ARRAY_BUFFER, vboCube);
    glEnableVertexAttribArray(attributePosition);
    glVertexAttribPointer(
        attributePosition,
        3,
        GL_FLOAT,
        GL_FALSE,
        sizeof(attributes),
        (GLvoid *)offsetof(attributes, position)
    );
    glEnableVertexAttribArray(attributeColor);
    glVertexAttribPointer(
        attributeColor,
        3,
        GL_FLOAT,
        GL_FALSE,
        sizeof(attributes),
        (GLvoid *)offsetof(attributes, color)
    );

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboCube);
    int size;
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

    glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(attributePosition);
    glDisableVertexAttribArray(attributeColor);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

and my tick-function (which is called 40 times per second) looks like this: 我的刻度函数(每秒被调用40次)如下所示:

void tick()
{
    glm::mat4 model = glm::translate(glm::mat4(1), glm::vec3(0, 0, -4));
    glm::mat4 view = glm::lookAt(glm::vec3(0, -2, 0), glm::vec3(0, 0, -4), glm::vec3(0, 1, 0));
    glm::mat4 projection = glm::perspective(45.0f, WIDTH / (float) HEIGHT, 0.1f, 10.0f);
    glm::mat4 daRealMvp = projection * view * model; // 9gaggers will get this

    // adding some rotation so i can see why the cube looks strange
    float angle = glfwGetTime();
    daRealMvp = daRealMvp * glm::rotate(glm::mat4(1), angle, glm::vec3(0, 1, 0));

    glUniformMatrix4fv(uniformMvp, 1, GL_FALSE, glm::value_ptr(daRealMvp));
}

If I remove glEnable(GL_DEPTH); 如果我删除glEnable(GL_DEPTH); (or move it to another line) or change glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); (或将其移至另一行)或更改glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); to glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); nothing changes, so I thought there is a problem with clearing the depth-buffer, but I don't know what it is. 没有任何变化,所以我认为清除深度缓冲区有问题,但我不知道它是什么。

So, what can I do to get a nice cube? 那么,我该怎么做才能得到一个漂亮的立方体? :) :)

EDIT1: EDIT1:

funny thing is, the blue face (which is not transparent) isnt the one on the front (where all vertices have positive Z), its the the one on the right (where all vertices have a positive X). 有趣的是,蓝色的表面(不透明)不是前面的一个(所有顶点都具有正Z),而是蓝色的右边(所有顶点都具有正X)。

you should use 你应该使用

glEnable(GL_DEPTH_TEST);

instead of 代替

glEnable(GL_DEPTH); 

at the third line. 在第三行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM