【发布时间】:2020-07-03 14:25:40
【问题描述】:
while following the lighting chapter in the learnopengl series,作者在创建多个VAOs(Vertex Array Objects)时提供了这种代码:
unsigned int VBO, cubeVAO;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(cubeVAO);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); //#this
glEnableVertexAttribArray(0);
// second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube)
unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
// we only need to bind to the VBO (to link it with glVertexAttribPointer), no need to fill it; the VBO's data already contains all we need (it's already bound, but we do it again for educational purposes)
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); //#this
glEnableVertexAttribArray(0);
这里我们使用相同的VBO 和多个VAO,但对glVertexAttribPointer 的调用使用相同的参数完成了两次。 Earlier in this lesson他提到:
每个顶点属性从由 VBO 管理的内存中获取其数据,从哪个 VBO 获取数据(您可以有多个 VBO)由调用 glVertexAttribPointer 时当前绑定到 GL_ARRAY_BUFFER 的 VBO 确定。由于之前定义的 VBO 在调用 glVertexAttribPointer 之前仍被绑定,因此顶点属性 0 现在与其顶点数据相关联。
那么,这是否意味着这两个调用是多余的,或者这是必要的,如果不这样做会导致问题?
【问题讨论】: