问题描述
我刚刚通读了有关顶点数组对象"和顶点缓冲对象"的教程,但是我无法从以下代码中了解OpenGL如何知道第一个VBO(vertexBufferObjID[0]
)表示顶点坐标,而第二个VBO (vertexBufferObjID[1]
)代表颜色数据?
I've just read through a tutorial about Vertex Array Objects and Vertex Buffer Objects, and I can't work out from the following code how OpenGL knows the first VBO (vertexBufferObjID[0]
) represents vertex coordinates, and the second VBO (vertexBufferObjID[1]
) represents colour data?
glGenBuffers(2, vertexBufferObjID);
// VBO for vertex data
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
// VBO for colour data
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[1]);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), colours, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
编辑:感谢Peter的回答,我发现了以下两行代码将每个VBO与着色器连接在一起(索引0和1与VBO索引相关):
Thanks to Peter's answer, I found the following two lines of code which hook up each VBO with the shaders (indices 0 & 1 correlate to the VBO index):
glBindAttribLocation(programId, 0, "in_Position");
glBindAttribLocation(programId, 1, "in_Color");
推荐答案
不是,您必须告诉它着色器中的哪个.
It doesn't, you have to tell it which is which in the shader.
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
void main()
{
/* ... */
}
这篇关于OpenGL如何知道每个顶点缓冲区对象是什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!