Using multiple VAO for separated buffers

Hi,

I want to set a VAO for multiple buffer as shown here:

for (size_t i = 0; i < myMeshes.size(); i++)
{
        glGenVertexArrays(1, &VAOArray[i]);
		glBindVertexArray(VAOArray[i]);

		/* GENERATE THE BUFFERS */
		glGenBuffers(1, &bufferArray[i]);

		/* SELECT THAT BUFFER TO WORK WITH */
		glBindBuffer(GL_ARRAY_BUFFER, bufferArray[i]);
		glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realPositions.size() * sizeof(float), (GLfloat*)RealPos, GL_STATIC_DRAW);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
		glEnableVertexAttribArray(0);


		


			glGenBuffers(1, &colArray[i]);
			glBindBuffer(GL_ARRAY_BUFFER, colArray[colorIndex]);
			glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realTextures.size() * sizeof(float), (GLfloat*)vertex_color, GL_STATIC_DRAW);
			glEnableVertexAttribArray(1);


			glGenTextures(1, &textureArray[i]);
			glBindTexture(GL_TEXTURE_2D, textureArray[textureIndex]);
			glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realTextures.size() * sizeof(float), (GLfloat*)vertex_textcoord, GL_STATIC_DRAW);
			glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
			glEnableVertexAttribArray(2);
			

		glGenBuffers(1, &normalArray[i]);
		glBindBuffer(GL_ARRAY_BUFFER, normalArray[i]);
		glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realNormals.size()*sizeof(float), (GLfloat*)RealNor, GL_STATIC_DRAW);
		glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);
		glEnableVertexAttribArray(3);

		glBindVertexArray(0);

	}

is it corret? or I have to set a VAO per buffer, I mean a VAO for the position, a VAO for the color, a VAO for the texture and a VAO for the normal?

Thanks

Yes.

No. There is only one bound VAO at any given time. The VAO which is bound when executing a draw call determines the source for all of the attribute arrays, as well as the element array.

A VAO is a state container which holds all state related to attribute arrays. For each array it stores the buffer, offset, stride, size (component count), format, divisor, and enabled state. It also stores the GL_ELEMENT_ARRAY_BUFFER binding. Any call which modifies that state (glVertexAttribPointer, glVertexAttribDivisor, gl{Enable,Disable}VertexAttribArray, and similar) modifies the state in the bound VAO. Any call which uses that state (i.e. draw calls plus buffer-related functions which use the GL_ELEMENT_ARRAY_BUFFER target) uses the state from the bound VAO.

VAOs just provide an efficient way to change all attribute array state with a single call. For a C analogy, it’s like having a pointer to a struct that can be changed to point to a different struct rather than having a single struct and having to change all of its members.

1 Like

I don’t see a matching call for glVertexAttribPointer(1.

It does exist in my code, just when I copied and pasted it I forgot to take it with the code

Thank you for this detailed answer

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.