OpenGL深度不起作用,三角形消失

OpenGL Depth Not working, Triangles Disappear

本文关键字:三角形 消失 不起作用 深度 OpenGL      更新时间:2023-10-16

由于某种原因,我无法检测到我无法为任何三角形设置z索引。三角形将以相同的深度显示,从 1 到 -1(包括),但之后它会消失。我没有应用任何 MVP 矩阵(试图先弄清楚这一点),我希望能够看到我的三角形在我减小 z 值时变小。这是我到目前为止所拥有的:

//My main function
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("Window");
if (init_resources()) {
    glutDisplayFunc(onDisplay);
    glutIdleFunc(idle);
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}
//And in my display function
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

使用我的Vertext着色器:

gl_Position = vec4(coord2d, 0.0, 1.0);

glGetIntegerv(GL_DEPTH_BITS, &depth)也返回 24。

编辑:完整代码

Sprite *sprite1;
Sprite *sprite2;
GLuint program;
GLuint cameraAttribute;
int init_resources()
{   
    //Link shaders, create program and attach shaders
    GLint link_ok = GL_FALSE;
    GLuint vs, fs;
    if ((vs = create_shader("vertShader.sh", GL_VERTEX_SHADER))   == 0) return 0;
    if ((fs = create_shader("fragShader.sh", GL_FRAGMENT_SHADER)) == 0) return 0;
    program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
        fprintf(stderr, "glLinkProgram:");
        print_log(program);
        return 0;
    }
    //Create Attributes
    cameraAttribute = glGetUniformLocation(program, "camera");
    if(cameraAttribute == -1){
        cout << "Error!";
    }

    //Init sprites
    sprite1 = new Sprite();
    sprite2 = new Sprite();

    return 1;
}
void onDisplay()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glUseProgram(program);

    sprite1->render();
    sprite2->render();
    glutSwapBuffers();
}
void idle()
{
    //Create Projection Matrix to change clip space.
    mat4 projectionMatrix = perspective(60.0f, (float)glutGet(GLUT_WINDOW_WIDTH) / (float)glutGet(GLUT_WINDOW_HEIGHT), 0.1f, 100.f);
    glUniformMatrix4fv(cameraAttribute, 1, GL_FALSE, &projectionMatrix[0][0]);
}
void free_resources()
{
    glDeleteProgram(program);
}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My Second Triangle");
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    if (init_resources()) {
        glutDisplayFunc(onDisplay);
        glutIdleFunc(idle);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        //uses glGetError to check for how many errors there are.
        cout << CheckGLErrors() << "n";
        glutMainLoop();
    }

    free_resources();
    return 0;
}

还有我的精灵文件:

#include "Sprite.h"
Sprite::Sprite(){
    init();
}

void Sprite::init(){
    GLfloat triangle_vertices[] = {
        0.0,  0.8f,
        -0.8f, -0.8f,
        0.8f, -0.8f,
    };
    //Set attributes
    locationAttribute = glGetAttribLocation(program, "coord2d");
    if (locationAttribute == -1) {
        fprintf(stderr, "Could not bind attribute location");
    }
    translationUniform = glGetUniformLocation(program, "translationUniform");
    if(translationUniform == -1){
        fprintf(stderr, "Could not bind attribute transformation");
    }

    //Create buffer
    glGenBuffers(1, &mainBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, mainBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(
                          locationAttribute, // attribute
                          2,                 // number of elements per vertex, here (x,y)
                          GL_FLOAT,          // the type of each element
                          GL_FALSE,          // take our values as-is
                          0,                 // no extra data between each position
                          0                  // offset of first element
    );
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::render(){
    glBindBuffer(GL_ARRAY_BUFFER, mainBuffer);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

和顶点着色器:(注意,版本是由解析器添加的)

attribute vec2 coord2d;
uniform mat4 translationUniform;
uniform mat4 camera;

void main(void) {
    gl_Position = camera * vec4(coord2d, -1.1, 1.0);
}

glEnable(GL_BLEND | GL_DEPTH_TEST);

你不能像这样组合它们,你必须发出两个glEnable命令。这可能是在生成错误,或者启用完全不同的东西。

您将此与使用按位掩码的glClear混淆了。

开始在你的代码中使用glGetError,它会为你解决这个问题。

好的,我重读了这个问题,我想我现在明白了。

三角形在 -1 到 1 之外消失,因为没有任何投影矩阵,您将绘制归一化设备坐标,该坐标的近剪裁平面和远剪裁平面为 -1 到 1。这意味着任何 z 值在 -1 到 1 之外的顶点都将被剔除。

其次,你说你没有看到它随着距离而变小。这是透视投影的一个功能,您没有(因为您没有投影矩阵)。使用默认投影矩阵(正交),您将不会获得任何透视透视收缩,因此无论距离如何,三角形都将显示相同的大小。如果你想对场景应用透视,那么你需要一个透视矩阵:尝试应用gluPerspective(1, 45, 0.1, 100);,这应该给你更像你期望的东西(三角形会出现从 -0.1 到 -100 的 z,当它接近 z=-100 时会变小)。您可能需要调整这些内容以匹配场景的比例。