Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Funkyjive May 22, 2016 at 11:13 PM 0 replies 1.6k views
Original Post
Funkyjive
Funkyjive

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();
shader->Bind();
glBindVertexArray(position_color_vao);
glBindBuffer(GL_ARRAY_BUFFER, vbos[mesh]);
glUniformMatrix4fv(shader->Get_Model_Transform_Location(), 1, GL_FALSE, glm::value_ptr(parent_transformation.transformation));
glUniformMatrix4fv(shader->Get_View_Transform_Location(), 1, GL_FALSE, glm::value_ptr(view_mat));
glUniformMatrix4fv(shader->Get_Projection_Transform_Location(), 1, GL_FALSE, glm::value_ptr(projection));
    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?

"I would rather be in the boat with a drink on the rocks, than in the drink with a boat on the rocks" -Unknown
21st Century Moose
21st Century Moose

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.