Reputation:
I'm trying to read some example of code our teacher gave us about the use of VAO and VBO in openGL, but I'm having a hard time understanding it. I commented each line to show what I understood. Can someone explain me what's happening here?
glGenVertexArrays( 2, vao ); // create a vao of size 2
glBindVertexArray( vao[0] ); // we state that we're going to work on the first element in the vao
// p0, p1, ... have been defined somewhere else
GLfloat sommetsCube[] = { p0, p4, p1, p5, p2, p6, p3, p7,
p1, p2, p0, p3, p4, p7, p5, p6 };
glGenBuffers( 1, &vboCube ); // we generate a buffer using data from vboCube (which have been defined somewhere else)
// I really don't get what those lines do
// though i think this has something to do with sending data to the GPU
glBindBuffer( GL_ARRAY_BUFFER, vboCube );
glBufferData( GL_ARRAY_BUFFER, sizeof(sommetsCube), sommetsCube, GL_STATIC_DRAW );
glVertexAttribPointer( locVertex, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray(locVertex);
glBindVertexArray(0); // we're no longer working with the first element in the vao
Also, I understand the nature of a VBO, but I'm not so sure about the nature of a VAO. Are they arrays of VBO? Are they something else?
Upvotes: 0
Views: 61
Reputation: 2516
Your understanding of the first line is wrong. It generates 2 VAO objects and stores them in the array. Compare with the glGenBuffers line which generates one VBO and stores it in a single variable. The &vboCube treats vboCube as an array[1]
Beyond that, a VAO can be thought of as a geometry node in the scene graph, a collection of vertices, texture coordinates, etc.
The two buffer calls do indeed send data to the GPU. The two attrib calls define what that data will be used for in the geometry.
Setting up VBO/VAO data is a bit repetitive and ugly in OpenGL. The good news is that these few lines are all you really need to know, and you'll soon be able to recognise them everywhere.
Oh and if you're serious about learning OpenGL, buy the OpenGL SuperBible.
Hope this helps.
Upvotes: 1