Vertex array object

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

[QUOTE=gwartney;1282270]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?
[/QUOTE]
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.

[QUOTE=GClements;1282272]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.[/QUOTE]

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.

That is correct.