Jump to content
  • Advertisement
Sign in to follow this  
Funkyjive

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

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

If you intended to correct an error in the post then please contact us.

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();
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?

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!