I have a few VERY basic questions about indexed drawing…
How does a GL_ELEMENT_ARRAY_BUFFER “know” which GL_ARRAY_BUFFER it is associated with? I.e. how does OpenGL know which vertices are referenced by the indices? E.g. in something similar to Example 3.5 from the Red Book:
// Four vertices
static const GLfloat vertex_positions[] =
{
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 1.0f,
};
// Three indices (we’re going to draw one triangle at a time
static const GLushort vertex_indices[] =
{
0, 1, 2
};
// Set up the element array buffer
glGenBuffers(1, ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
// Put indices into element array buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);
// Set up the vertex attributes
glGenVertexArrays(1, vao); // Does this vao "know" about the ebo created above?
glBindVertexArray(vao[0]);
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
// Put vertices into
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertex_positions),
vertex_positions, GL_STATIC_DRAW);
/>
When the code later draws the vertices, what should it “bind” to? Should it bind to ebo or to vao? Should it do this?
glBindVertexArray(vao[0]); // Bind to VAO
glDrawElements(GL_TRIANGLES, 3, GL_SHORT, 0);
Or should it do this?
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]); // Bind to EBO
glDrawElements(GL_TRIANGLES, 3, GL_SHORT, 0);
Thanks
Tom