You can use multiple shaders, but to switch between them can be quite costly so the recommended practise is to draw every object of a shader, then switch to the next shader and draw all the objects using that one and so on.
To switch between shaders, you call glUseProgram()
.
"Vertex Array Object" is brought to you by the OpenGL ARB Subcommittee for Silly Names.
Think of it as a geometry object. (As an old time SGI Performer programmer, I call them geosets.) The instance variables/members of the object are your vertex pointer, normal pointer, color pointer, attrib N pointer, ...
When a VAO is first bound, you assign these members by calling
glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer...;
glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer...;
and so on. Which attributes are enabled and the pointers you supply are stored in the VAO.
After that when you bind the VAO again, all the those attributes and pointers also become current. So one glBindVertexArray
call is equivalent to all the code previously needed to set up all the attributes. It's handy for passing geometry around between functions or methods without having to create your own structs or objects.
(One time setup, multiple use is the easiest way to use VAOs, but you can also change attributes just by binding it and doing more enable/pointer calls. VAOs are not constants.)
More info in response to Patrick's questions:
The default for a newly created VAO is that it's empty (AFAIK). No geometry at all, not even vertexes, so if you try to draw it, you'll get an OpenGL error. This is reasonably sane, as in "initialize everything to False/NULL/zero".
You only need to glEnableClientState
when you set things up. The VAO remembers the enable/disable state for each pointer.
Yes the VAO will store glEnableVertexAttribArray
and glVertexAttrib
. The old vertex, normal, color, ... arrays are the same as attribute arrays, vertex == #0 and so on.
Best Answer
Sure, this is pretty simple actually, your attribute has a location, and vertex data is fed with
glVertexAttribPointer()
for plain Vertex Arrays, like this:For VBOs, it's the same, but you have to bind the buffer to the
GL_ARRAY_BUFFER
target, and the last parameter ofglVertexAttribPointer()
is now an offset into the buffer memory storage. The pointer value itself is interpreted as a offset:In this case the offset is 0, assuming the vertex data is uploaded at the start of the buffer. The offset is measures in bytes.
The drawing is then performed with
glDrawArrays()
/glDrawElements()
. Hope this helps!