I’ve spent the past several weeks learning the ins and outs of the OpenGL system. It hasn’t felt very intuitive to me, which is probably because it has been designed by committee over the past several decades, but at the same time, as I got used to its style of ‘thinking’, I’ve been able to absorb things at a somewhat decent clip.
When a programmer wants to create anything in OpenGL, from a vertex array object to a shader program, the proper course of creation is to define a GLuint ( which is just a typedef for an unsigned int ) which will serve as a label that connects your code to the object inside the OpenGL state. This is done because many of these objects are kept on the GPU, and so loading the data to the GPU is a different process than actually operating on the objects.
You use a call to define the label, and then use that label to load data into the OpenGL object. Once you get to the point where the label is no longer needed, you can actually delete the label. Deletion of the label is NOT the same thing as deletion of the object – in fact, OpenGL will hold onto information that it needs at a later time, so you don’t technically know when it is deleted.
That sounds a little abstract and crazy, so here’s an example for creating a shader. You initially call glCreateShader() to create a shader label, you send in the source code for your shader, and then, immediately after checking everything is okay, you can call glDeleteShader() to delete the shader label. The compiled code is still inside the OpenGL object, and will be used to transform your data, but the label is gone.
Of course, if you need to switch from shader to shader in your program, you don’t want to remove your one link to it. But you can if you don’t need it anymore.
VAOs serve as a batch of drawing commands. You can define a VAO, send in some drawing commands to set a particular state, and then in the future instead of calling those commands you can just set the VAO again. In my demo code I use separate VAOs for separate types of objects – lines, points, and triangles – and then bounce from VAO to VAO to draw them all.
VBOs are the buffers where you keep vertices and indices. This is where the actual tuples are kept. Many VBOs can be kept inside a VAO, along with instructions on how to use them.
I use gDEbugger to peek into how OpenGL is operating on my program. It’s a complex program with a TON of information – more than I really need at this point, but it’s been very insightful in showing how OpenGL states work, what is being stored in the GPU, and when objects can be safely destroyed. Best of all, it was currently made 100% free!