PDA

View Full Version : Vertex array object



gwartney
04-23-2016, 11:52 PM
I have been wondering something since am new to opengl. When you are using a vao to store information for your buffer objects why do you have to call the bindvertexArray twice? I know the first time its to initialize the vao. Like below but latter on in my code when i go to draw the object i call it again. Could some one elaborate on why this is?



GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// 2. Copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 3. Then set our vertex attributes pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//4. Unbind the VAO
glBindVertexArray(0);



glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);

GClements
04-24-2016, 12:53 AM
I have been wondering something since am new to opengl. When you are using a vao to store information for your buffer objects why do you have to call the bindvertexArray twice? I know the first time its to initialize the vao. Like below but latter on in my code when i go to draw the object i call it again. Could some one elaborate on why this is?

If you only ever use one VAO, you can bind it once and forget about it. If you use more than one, you need to ensure that the correct VAO is bound before any calls which modify or use the currently-bound VAO.

To avoid confusing errors caused by neglecting to bind the correct VAO, it's common to unbind the current VAO (i.e. bind VAO zero) whenever you've finished using it, so that such mistakes result in more obvious errors.

gwartney
04-24-2016, 01:48 AM
If you only ever use one VAO, you can bind it once and forget about it. If you use more than one, you need to ensure that the correct VAO is bound before any calls which modify or use the currently-bound VAO.

To avoid confusing errors caused by neglecting to bind the correct VAO, it's common to unbind the current VAO (i.e. bind VAO zero) whenever you've finished using it, so that such mistakes result in more obvious errors.


Ah ok so just to make sure i fully understand what you are saying and from what i have found is that the first call binds the vao and then unbinds it. The second time around it rebinds it and then once again unbinds.

GClements
04-24-2016, 02:51 AM
the first call binds the vao and then unbinds it. The second time around it rebinds it and then once again unbinds.
That is correct.