• 11
• 9
• 10
• 9
• 10

# Why must I recall glVertexAttribPointer every draw (Shouldn't it be in my VAO?)

This topic is 700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Something is slightly confusing me about some of my rendering code. My understanding is that a VAO retains all of the glVertexAttribPointer and glEnableVertexAttribArray calls so that when it comes time to render I don't have to explicitly do that again but my program crashes if I don't repeat those calls every draw. Otherwise the program runs fine.

Here is where I set up the VAO initially:

void Render_Engine::Generate_VAOs(){
std::cout << "Generating a vao" << std::endl;
glGenVertexArrays(1, &position_color_vao);
glBindVertexArray(position_color_vao);
uint8_t position_color_vertex_stride = (3 * sizeof(GLfloat)) + (4 * sizeof(GLfloat)) + (3 * sizeof(GLfloat));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)(3 * sizeof(GLfloat)));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)(7 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}


And here is my rendering call for each mesh:

Shader* shader = mesh->Get_Shader();
glBindVertexArray(position_color_vao);
glBindBuffer(GL_ARRAY_BUFFER, vbos[mesh]);
uint8_t position_color_vertex_stride = (3 * sizeof(GLfloat)) + (4 * sizeof(GLfloat)) + (3 * sizeof(GLfloat));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)(3 * sizeof(GLfloat)));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, position_color_vertex_stride, (GLvoid*)(7 * sizeof(GLfloat)));
glDrawArrays(GL_TRIANGLES, 0, mesh->Get_Vertex_Count());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);


If I remove the indented lines the program will crash. If I leave them in it runs just fine. So why do I have to recall those? I thought the rebinding of the vao would do that for me?

##### Share on other sites

You also need your glBindBuffer call in your VAO creation, before any glVertexAttribPointer calls.  glVertexAttribPointer uses the currently bound buffer object and it's illegal to not have a buffer object bound when creating VAOs.

If your buffer is not created yet, then either restructure some code or consider switching to GL_ARB_vertex_attrib_binding (which allows vertex specification without a buffer being bound).

Incidentally, your code will be much cleaner if you use a struct for your vertex format rather than an array of floats; you get to use sizeof (struct) for specifying stride and offsetof (struct, member) for specifying offsets in your glVertexAttribPointer calls.

Edited by mhagain