How to enable a vertex attribute array with VBOs?

I’m trying to set a vertex attribute array using VBOs.
The VBO is already set and now I’m with some doubts regarding how to enable my attributes array.

Let’s say that my VBO ID is bufferID and, to enable this array I do:


glBindBuffer( GL_ARRAY_BUFFER, bufferID );
GLint loc = glGetAttribLocation( shader.getShaderProgram(), "inTriangleID" );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 1, GL_FLOAT, GL_FALSE, 0, NULL );

My question is if after the call for glVertexAttribPointer I can bind another buffer to GL_ARRAY_BUFFER.

After the above calls comes the rendering calls. Something like


glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glBindBuffer( GL_ARRAY_BUFFER, mPositionVboID );
glVertexPointer( mVertexSize, GL_FLOAT, 0, NULL );
glEnableClientState( GL_VERTEX_ARRAY);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mEboID );
glDrawElements( GL_TRIANGLES, mIndices->size(), GL_UNSIGNED_INT, NULL );

As one can see, another buffer is bind to GL_ARRAY_BUFFER. This doesn’t unbind the previous buffer, which was the vertex attrib buffer or does glPushClientAttrib saves the previous buffer?

How is the correct way of set a vertex attribute array using VBOs?

You seem to be mixing old style and new style arrays which is not going to work.


glBindBuffer( GL_ARRAY_BUFFER, bufferID );
GLint loc = glGetAttribLocation( shader.getShaderProgram(), "inTriangleID" );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 1, GL_FLOAT, GL_FALSE, 0, NULL );

You can’t have this line in there
glEnableClientState( GL_VERTEX_ARRAY )
As that is the old style vertex array.

However your rendering code is correctly using the old style arrays so reuse those lines of code.
]

It is because I’m writting an application that needs to work both on Mac and PC. Since MacOSX does not support GLSL > 1.2 I need to use this old style method.

But this piece of code is working. But, you’re are saying that I just need to remove glEnableClientState?

Yes, you should remove glEnableClientState. As you can see in the documentation, glEnableVertexAttribArray was introduced in OpenGL 2.0, thus it will work fine on the MacOSX.

glEnableVertexAttribArray has been around since OpenGL 1.3 (with ARB_vertex_program.) You don’t need GLSL > 1.20.

You should read the part of the spec that talks about VERTEX_ATTRIB_ARRAY_BUFFER_BINDING (the binding is copied when setting the pointer.)

Ok, I’ll check.
Thanks a lot.