简体   繁体   English

OpenGL Ubuntu深度

[英]OpenGL Ubuntu Depth

I'm writing a simple OpenGL program on Ubuntu which draws two square (one in front of the other) using vertex array. 我正在Ubuntu上编写一个简单的OpenGL程序,它使用顶点数组绘制两个正方形(一个在另一个前面)。 For some reason, the GL_DEPTH_TEST does not seem to work. 由于某些原因,GL_DEPTH_TEST似乎不起作用。 The object in the back appear in front of the one in the front. 后面的对象显示在前面的对象的前面。 Depth buffer is enabled by 深度缓冲区通过以下方式启用

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

GL_DEPTH_TEST is enabled by GL_DEPTH_TEST已启用

glEnable(GL_DEPTH_TEST);

and depth buffer is cleared before drawing by 并在绘制之前清除深度缓冲区

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

The complete code is shown below: 完整的代码如下所示:

#define BUFFER_OFFSET( offset )   ((GLvoid*) (offset))

#define NUM_VERTICES 8

vec4 vertices[8] =
{{0.0, 0.0, -0.25, 1.0},    // front
 {0.0, 0.5, -0.25, 1.0},
 {0.5, 0.5, -0.25, 1.0},
 {0.5, 0.0, -0.25, 1.0}, 
 {0.25, 0.25, -0.75, 1.0},  // back
 {0.25, 0.75, -0.75, 1.0},
 {0.75, 0.75, -0.75, 1.0},
 {0.75, 0.25, -0.75, 1.0}};

vec4 vertex_colors[8] =
{{1.0, 0.0, 0.0, 1.0},  // red
 {1.0, 0.0, 0.0, 1.0},
 {1.0, 0.0, 0.0, 1.0},
 {1.0, 0.0, 0.0, 1.0},
 {0.0, 1.0, 0.0, 1.0},  // green
 {0.0, 1.0, 0.0, 1.0},
 {0.0, 1.0, 0.0, 1.0},
 {0.0, 1.0, 0.0, 1.0}};

void init(void)
{
    GLuint program = initShader("vshader_td.glsl", "fshader_td.glsl");
    glUseProgram(program);

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(vertex_colors), vertex_colors);

    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

    GLuint vColor = glGetAttribLocation(program, "vColor");
    glEnableVertexAttribArray(vColor);
    glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)));

    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_QUADS, 0, NUM_VERTICES);
    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(512, 512);
    glutCreateWindow("Test Depth");
    glewInit();
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

The vertex shader is shown below: #version 130 顶点着色器如下所示:#version 130

in vec4 vPosition;
in vec4 vColor;
out vec4 color;

void main()
{
    color = vColor;
    gl_Position = vPosition;
}

and the fragment shader is shown below: #version 130 片段着色器如下所示:#version 130

in vec4 color;
out vec4 fColor;

void main()
{
    fColor = color;
}

Why object in the front is blocked by the object in the back? 为什么前面的物体被后面的物体阻挡?

In the vertex shader, you do 在顶点着色器中,您可以

 in vec4 vPosition; [....] gl_Position = vPosition; 

without applying any transformations. 无需进行任何转换。 Since gl_Position is the clip space position of the vertex, this means that your vertex coordinates are interpreted as being specified in clip space. 由于gl_Position是顶点的裁剪空间位置,因此这意味着您的顶点坐标被解释为在裁剪空间中指定。

And your vertex coordinates 和你的顶点坐标

 vec4 vertices[8] = {{0.0, 0.0, -0.25, 1.0}, // front [...] {0.25, 0.25, -0.75, 1.0}, // back 

just imply that the points you marked as "front" lie behind the ones you marked as "back". 只是意味着你标记为您标记为“回”的那些背后的 “前”谎言之分。

I don't know how you came to the assumption that it should be the other way round, I can only guess that you confused this with classical, left-handed OpenGL eye space convention where the "camera: looks into -z direction - but that is totally irrelevant here, since you never established an eye space at all, and draw directly in clip space. 我不知道您是如何认为应该相反的,我只能猜测您将其与经典的左手OpenGL眼睛空间约定混淆了,其中“相机:朝-z方向看-这在这里完全无关紧要,因为您根本没有建立眼睛空间,而是直接在剪辑空间中绘制。

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

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