View Full Version : reuse of vbo

10-23-2012, 06:07 AM
I don't understand the use in opengl of the
glVertexAttribPointer(glGetAttribLocation(shaderPr ogram1, "VertexPosition"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertexes), BUFFER_OFFSET(sizeof(float) * 3));
and of the
glEnableVertexAttribArray(glGetAttribLocation(shad erProgram1, "VertexPosition"));
what do they precisely?
are relative to the shader or to the vbo?
because I have a vboarray and i wish use it for many render call
i would do:

//1) first use of the vboarray with shaderProgram1

//1) second use of the vboarray with shaderProgram2
,i must call glVertexAttribPointer and glEnableVertexAttribArray for each different shader?

10-23-2012, 08:10 AM
GLint index = glGetAttribLocation(shad erProgram1, "VertexPosition")

This will return a location (or index) which associates a vertex attribute with a unique index at program link time. If no such attribute is active (i.e. is not only defined but also actually in use somewhere in the shader stage) or exists or if the name of the attribute starts with the prefix "gl_", -1 is returned.

glVertexAttribPointer(index, 3, GL_FLOAT, GL_FALSE, sizeof(Vertexes), BUFFER_OFFSET(sizeof(float) * 3));

This defines a generic vertex array by specifying the index of a generic vertex attribute which is to receive data stored in the array. This includes determining the number of components per attribute (1,2,3 or 4 or GL_BGRA), the component type, if values are supposed to be normalized when transfered, the size of the array and an offset into a buffer (i.e. a VBO) pointing to where the first attribute can be found in the buffer's data store.


This enables a specific attribute array, i.e. if you pass some index, rendering commands such as glDrawElements() will lead to data from the buffer being accessible in a invocation of shader shader stage defining the attribute associated with the above index. If the array the index is referring to is not enabled, no data will be transferred for the associated attribute. I will speculate that the attribute value are either undefined at that point, will assume a default value or carry the last value that was specified from an enabled array but I'm not 100% sure about that. Need to check the spec.

i must call glVertexAttribPointer and glEnableVertexAttribArray for each different shader?

No, the attrib pointers and enabled attributes aren't program state. If you're not using vertex array objects (VAOs) you have one set of state which is manipulated by the above commands which is current until changed. Every time you change the VBO might need to adapt the attrib pointers to match the data but only if the layout of the data is different than before. You don't need to change them if you have, say, two objects stored in a single VBO and the memory layout of both objects is the same. If you wanted to render only the second object you would determine that with a rendering command such as glDrawArrays() and determine the first element to start with.

If you use VAOs the state modified by glVertexAttribPointer and glEnableVertexAttribArray is stored in the VAO's state vector and can be reinstalled as the current state by simply binding the vertex array. This lets you determine multiple VAOs with data stores that have different memory layouts one time and reuse them as many times as you need to. Note that the VBO is not stored inside the VAO since VAOs are so called container objects which only store state and references to other objects such as VBOs.

I think that's it.