Reputation: 503
I have a simple question. Is it right, that glVertexAttribPointer
operations have to be called once for a GL_ARRAY_BUFFER
to save the attribute states until I want to change them?
Or do I need to call glVertexAttribPointer
each time in between glBindBuffer(GL_ARRAY_BUFFER, ...);
and glDrawArrays(...);
?
Upvotes: 3
Views: 1901
Reputation: 210978
Is it right, that glVertexAttribPointer operations have to be called once for a GL_ARRAY_BUFFER to save the attribute states until I want to change them? Or do I need to call glVertexAttribPointer each time.
glVertexAttribPointer
specifies the location and data format of the array of generic vertex attribute at the specified index. This information is stored in the Vertex Array Object. Note, in OpenGL there is always a vertex array object bound. Either the default vertex array object (0), which can't be deleted, or a non-zero named vertex array object.
The specification of the vertex attribute arrays has to be done before an object is drawn:
glBindBuffer( ARRAY_BUFFER, posBufObj );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, .... );
glBindBuffer( nvBufObj );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, .... );
glDrawArrays( .... )
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );
glBindBuffer( ARRAY_BUFFER, 0 );
Yes, it is sufficient to call glVertexAttribPointer
once per vertex attribute. The Vertex Specification is kept until it is not redefined, because it is stated in the Vertex Array Object.
Of course, the buffer object, where glVertexAttribPointer
refers to must not be deleted. Also the state, whether the vertex attribute is enabled (glEnableVertexAttribArray
) or not is kept until the vertex attribute is disabled again (glDisableVertexAttribArray
).
The Khronos OpenGL wiki about Vertex Specification clearly says:
The
glVertexAttribPointer
functions state where an attribute index gets its array data from.
This state can be retrieved with glGetVertexAttrib
.
More information about vertex attributes can be found in the OpenGL CoreProfile specification from Chapter 10.2 to 10.6
To handle different vertex attribute pointers and not to specify and enable or disable them alternately, different Vertex Array Objectx can be generated (glGenVertexArrays
), which stores all the information about data format, state, attribute index and the name of the vertex buffer object:
generated and specify vertex array object
vaoObj = glGenVertexArrays();
glBindVertexArray( vaoObj );
glBindBuffer( ARRAY_BUFFER, posBufObj );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, .... );
glBindBuffer( nvBufObj );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, .... );
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray( 0 );
bind vertex array object
glBindVertexArray( vaoObj );
glDrawArrays( .... )
glBindVertexArray( 0 );
Upvotes: 3
Reputation: 4078
I do not understand your question well. But using glVertexAttribPointer must be used on your currently bound vbo :
glBindVertexArray(vao);
glBindBuffer(ARRAY_BUFFER, position);
glEnableVertexAttribArray(0);
glVertexAttribPointer...
glBindBuffer(normal);
glEnableVertex...
glVertexAttribPointer...
....
glBindVertexArray(vao);
glDrawArrays...
Upvotes: 0