I am using two shaders to render all the items in my scene. Each item is drawn by one shader only.
Every shader has different attributes. In my case, one shader has only vertex coordinates and color, while the second shader has vertex coordinates, texture coordinates and color. Each shader has a VAO corresponding with these attributes.
What I do to render the scene is loop through all shaders. For each shader, I call glUseProgram, I call glBindVertexArray with the VAO associated with the shader and also initialize the attributes. Then once the shader is active I loop through all the items in my scene to render them with the current shader.
Now to simplify each item has a draw method which contains the following:
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
//s->setupVertices();
glDrawElements(m_primitive, m_elementSize, GL_UNSIGNED_INT, (GLvoid*)(sizeof(GLuint) * 0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
If I run the program like this, the items are not rendered correctly, but I noticed that if I uncomment the setupVertices() line (this is where the glVertexAttribPointer for the current shader are called), then everything is rendered fine.
Now this is something I don't understand. From what I understood on VAO, VAO store information on the vertex attributes, so I was expecting that once I call glBindVertexArray with the current VAO, the data for the vertex layout are remembered correctly and there's no need to re-specify the vertices layout for each buffer object; but it looks like this is necessary otherwise all items are rendered wrongly.
Am I doing something wrong/missing? Or I didn't understand a bit about VAO/VBO work?