0
votes

I have some example code that calls glVertexAttribPointer() in 2 places. Is this necessary or can it just be done once?

First time - associating the vertex buffer data:

glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof( COLVERTEX ), 0 );
glBufferData( GL_ARRAY_BUFFER, sizeof( v ), v, GL_STATIC_DRAW );

Second time - in the rendering callback function:

glEnableVertexAttribArray(0);
glBindBuffer( GL_ARRAY_BUFFER, vboQuad );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof( COLVERTEX ), 0 );
glDrawArrays( GL_QUADS, 0, 4 );
glDisableVertexAttribArray(0);

Is it really necessary to describe the vertex attributes twice?

1
glVertexAttribPointer associates the currently bound buffer to the currently bound Vertex Array Object. You can the bind/undbind the VAO, which keeps the set of attribute buffers. - Colonel Thirty Two
@ColonelThirtyTwo, thanks - would that mean that it isn't necessary to call this before glBufferData? ..that I should only be calling it after glBindBuffer? - SparkyNZ
@SparkyNZ BufferData fills VBO with raw data. At this point it doesn't matter how data is supposed to be interpreted when drawing (e.g. the same data may be interpreted as vertex positions at one draw but as normals in another). So yes, you can remove this first call. - keltar
@keltar: Cool thanks Move your comment to an answer and I'll accept it. - SparkyNZ

1 Answers

1
votes

BufferData fills VBO with raw data. At this point it doesn't matter how data is supposed to be interpreted when drawing (e.g. the same data may be interpreted as vertex positions at one draw but as normals in another). So yes, you can remove this first call.

If you use vertex array objects, you could set vertex attribute pointers only once (via binding VBO, enabling vertex attibute, and setting vertex attribute pointer) and then just call glBindVertexArray before drawing and have all recorded vertex attrubtes set up (you don't even need to bind VBO containing vertex attributes before draw call).