我看到其他人的代码只在初始化vao时调用glVertexAttribPointer()。当我这样做时,场景中只有第一个对象被渲染,但如果我每帧*每个对象都调用它,一切都会正常渲染...这是否意味着我必须在绘制之前为每个对象设置glVertexAttribPointer()?或者我还有什么遗漏?!
glBindVertexArray(mesh->getVao());
glBindBuffer(GL_ARRAY_BUFFER, mesh->getVbo());
for(int i = 0; i < 5; i++)
glEnableVertexAttribArray(i);
// Vertex Positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)*offset);
offset++;
// Vertex UVs
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)*offset);
offset++;
// Vertex Normals
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)*offset);
offset++;
// Vertex Tangents
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)*offset);
offset++;
// Vertex Binormals
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, (void*)*offset);
// Draw
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getVbo());
glDrawElements(GL_TRIANGLES, mesh->getNumberOfIndices(), GL_UNSIGNED_SHORT, (void*)0);
for (int i = 0; i < 5; i++)
glDisableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
这是每个对象一个绘制调用。它可以完美地工作,但我真的需要为每个对象在每帧中调用glVertexAttribPointer()吗?