向使用多个顶点数组对象的渲染器引入深度缓冲区

时间:2013-01-18 20:33:45

标签: opengl-es opengl-es-2.0 depth-buffer vertex-array

我有一个使用多个顶点数组对象渲染复杂场景的渲染基础结构。每个顶点数组对象负责维护自己的绑定缓冲区,指针和属性集(正如它们设计的那样)。

给定多个负责渲染的数组对象,如何引入深度缓冲,使每个单独的数组对象在渲染过程中使用相同的深度缓冲?换句话说,我如何统一顶点数组对象(及其良好的封装属性)和深度缓冲的概念,这似乎是一个更全球化的概念。

我发现的所有示例都描述了在帧缓冲区的上下文中使用深度缓冲区。 Apple描述了这种技术here。那么在帧缓冲级别实现深度缓冲然后让顶点数组对象写入该帧缓冲的技术是什么?我可以遵循一个使用VAO和深度缓冲的例子吗?

我有一个封装顶点数组对象的类,这是它的绑定方法(负责设置各种缓冲区,指针和属性)。

void scene_GLBuffer::BindTriangles()
{
    glBindVertexArrayOES(_mVertexArrayObject);
    glGenVertexArraysOES(1, &_mVertexArrayObject);

    // generate the buffer and configure the gl pointers for position and normal data
    glGenBuffers(1, &_mVertexPositionNormalTriangles);
    /* Bind and set up vertex position and normal data */
    glBindBuffer(GL_ARRAY_BUFFER, _mVertexPositionNormalTriangles);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(crVertexPN)*_mPositionNormalTriangleData->size(),
                 _mPositionNormalTriangleData->data(),
                 GL_STATIC_DRAW);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE,
                          sizeof(crVertexPN), (void*)offsetof(crVertexPN,Position));
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE,
                          sizeof(crVertexPN), (void*)offsetof(crVertexPN,Normal));
    glEnableVertexAttribArray(GLKVertexAttribNormal);

    // generate the buffer and configure the gl pointers for color and alpha data
    glGenBuffers(1, &_mVertexColorTriangles);
    glBindBuffer(GL_ARRAY_BUFFER, _mVertexColorTriangles);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(crVertexC)*_mColorTriangleData->size(),
                 _mColorTriangleData->data(),
                 GL_DYNAMIC_DRAW);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_TRUE,
                          sizeof(crVertexC), (void*)offsetof(crVertexC,Color));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glBindBuffer(GL_ARRAY_BUFFER,0);

    // generate the buffer and configure the gl pointers for triangle index data
    glGenBuffers(1, &_mVertexIndexTriangles);
    /* Bind and set up triangle index data */
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _mVertexIndexTriangles);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bits32)*_mIndexTriangleData->size(),_mIndexTriangleData->data(), GL_STATIC_DRAW);
    //        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

    glBindVertexArrayOES(0);
}

绑定后,draw方法实现如下(并且在每个顶点缓冲区对象上调用此draw方法):

void scene_GLBuffer::Draw()
{
    glBindVertexArrayOES(_mVertexArrayObject);

    glBindBuffer(GL_ARRAY_BUFFER, _mVertexPositionNormalTriangles);
    glDrawElements(GL_TRIANGLES, _mIndexTriangleData->size(), GL_UNSIGNED_INT, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArrayOES(0);
}

所有这一切都很顺利,但是我无法看到深度缓冲适合这种情况。

1 个答案:

答案 0 :(得分:1)

  

我无法看到深度缓冲适合这种情况。

那是因为 不适合其中任何一个。

顶点数组对象是封装顶点数组状态的方法。此状态与其他状态无关,如当前程序对象,当前绑定纹理或帧缓冲。最后一个是深度缓冲区的来源。

如果要将具有VAO的多个对象渲染到同一帧缓冲区,则只需不要更改帧缓冲区。这就是你将它们渲染到同一图像的方式(为了简单起见,我们将它称为屏幕)。深度缓冲区是帧缓冲区的一部分,因此如果要渲染到相同的深度缓冲区,只需不要更改不同对象之间的帧缓冲区。

简而言之,对象的Draw函数既不知道也不关心当前使用的深度缓冲区。就像Draw函数不知道或不关心正在使用什么程序一样,绑定了什么纹理,以及当前视口是什么。