How VAOs work in OpenGL

0

I have tried to look through lots of tutorials and online documentation about HOVs because they cost me a lot to understand them, but it seems impossible.

From what I understand, it is assumed that if you call glBindVertexArray, from there when you call the method glBindBuffer the associated buffer will be stored in the HOV. So if you do this:

glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,2*sizeof(GLfloat),(void*)0);

Then from here every time you do this: Code glBindVertexArray (VAOs [0]);

Automatically it would be as if you were doing this: (Even if you do not "do it" because it's done, you only restore the buffer that is "done")

glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,2*sizeof(GLfloat),(void*)0);

But when I do several VAOs and each VAO I put a VBO, for some reason when I put the first, second or third VAO and check what is the current VBO, I stay in the last VBO with which I call the glBindBuffer method . Why? I say something like this:

glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBindVertexArray(VAOs[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBindVertexArray(VAOs[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[2]);

glBindVertexArray(VAOs[0]);
GLint n;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &n);
std::cout << "VBO ACTUAL: " << n << std::endl;//Imprime VBOs[2] pero deberia imprimir VBOs[0]
    
asked by 4dr14n31t0r Th3 G4m3r 28.01.2017 в 19:35
source

1 answer

0

OpenGL is a state machine, this means that to use an object, in your case a VAO, you must first activate it, to activate a VAO you use the function glBindVertexArray (vao_id) , all the operations that you perform from the activation of the VAO will be executed on said object, so calling the function glGetIntegerv (GL_ARRAY_BUFFER_BINDING, & n) will return the ID of the VAO that is active, in your if the last one, when you finish using it you can deactivate it with glBindVertexArray (0) .

You can see more tutorials at: link

GLuint VAOs[3], VBOs[3];

glGenVertexArrays(3, VAOs);
glGenBuffers(3, VBOs);

glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBindVertexArray(0);

glBindVertexArray(VAOs[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBindVertexArray(0);

glBindVertexArray(VAOs[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[2]);
glBindVertexArray(0);

glBindVertexArray(VAOs[2]);

GLint n;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &n);
std::cout << "VBO ACTUAL: " << n << std::endl;     
    
answered by 14.02.2017 в 16:24