c++ - 我可以有多个 GL_ARRAY_BUFFER 缓冲区吗?

标签 c++ opengl vertex-buffer vertex-attributes vertex-array-object

所以我在看another SO question regarding the command glVertexAttribPointer我有点困惑。这个问题的公认答案解释说,

But there's an additional implied piece of state that is also stored away for attribute 0 when you make the call: the data is read from the buffer currently bound to GL_ARRAY_BUFFER

这对我来说很有意义,但是如果我有多个绑定(bind)为 GL_ARRAY_BUFFER 的缓冲区怎么办? glVertexAttribPointer() 方法如何知道要设置哪一个的属性?

例如,在我的代码中,我正在绘制一个渐变三角形。为此,我创建了 2 个 VBO,一个在数组中包含颜色数据,另一个包含顶点位置。

glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    static const GLfloat points[] = {
    //data here
    };
    static const GLfloat colors[] = {
      //data here
    };
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
    vs = glCreateShader(GL_VERTEX_SHADER);
    fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(vs, 1, &vertexShaderData, NULL);
    glShaderSource(fs, 1, &fragShaderData, NULL);
    glCompileShader(vs);
    glCompileShader(fs);
    sp=glCreateProgram();
    glBindFragDataLocation(sp, 0, "outColor");
    glAttachShader(sp, vs);
    glAttachShader(sp, fs);
    glLinkProgram(sp);
    glUseProgram(sp);
    glGenBuffers(1, &colorBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);
    glDrawArrays(GL_TRIANGLES, 0, 9);

当我调用命令时,我在哪里指定使用哪个缓冲区?

最佳答案

This makes sense to me, but what if I have multiple buffers that are bound as GL_ARRAY_BUFFER?

那是不可能的。当您将缓冲区(或任何 OpenGL 对象)绑定(bind)到目标时,它会自动取消绑定(bind)之前存在的任何内容。

OpenGL object targets就像全局变量。当您将全局整数设置为 3 时会发生什么?它的旧值消失了。

glVertexAttribPointer总是使用当前绑定(bind)到GL_ARRAY_BUFFER的任何缓冲区.所以在你的第一次通话中,它将使用 buffer .在您的第二个电话中,它将使用 colorBuffer .

这就是为什么记住 glVertexAttribPointer 很重要的原因对当前 绑定(bind)的内容进行操作。因为即使你绑定(bind)了 colorBuffer , 并将其设置为由属性 1 使用,属性 0 仍然buffer 接收其顶点数据.

因此虽然您不能将多个缓冲区绑定(bind)到 GL_ARRAY_BUFFER ,您可以使用多个缓冲区作为顶点数据的来源。

关于c++ - 我可以有多个 GL_ARRAY_BUFFER 缓冲区吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37951641/

相关文章:

opengl - 如何修复以下 gcc 警告?

c++ - 条件分支

c++ - 当在 QTableWidget 中拖动一行时,如何找出它被拖动的行索引 FROM 和 TO?

c++ - LNK1201 在 Visual C++ 2003 上

c++ - 纹理openGl。 C++、qt

qt - 如何使用 opengl dekstop 选项配置 Qt?

c - 使用 glfw 和 vsync 时桌面感觉迟钝

c++ - VBO : Array not drawn

performance - 什么时候VBO比 “simple” OpenGL原语(glBegin())快?

python ctypes 和 C++ 指针