Reputation: 16638
I want to switch between shader programs runtime, with a fixed VBO array.
I thik I need no more than the following functions with fixed vertex attributes index (I want 0 for position, 1 for texture coordinates)
glEnableVertexAttribArray
glVertexAttribPointer
glGetAttribLocation
glBindAttribLocation
I have wired up everything, but nothing has drawn to the FBO. If I use vertex attribute index (in glVertexAttribPointer) that has been located from the program, then it works fine, but I cannot use it with fixed attributes index.
Can somebody show me a brief sample code that shows me the right usage/execution order of the functions above?
Is there any missing step?
Upvotes: 0
Views: 1710
Reputation: 16638
The point is: glBindAttribLocation must be called before link the program.
glBindAttribLocation(program, ATTRIBUTE_POSITION, "position");
glBindAttribLocation(program, ATTRIBUTE_TEXTURE_COORDINATES, "textureCoordinates");
glLinkProgram(program);
Then you can use attribute array function with fixed indices, like:
glEnableVertexAttribArray(ATTRIBUTE_POSITION);
glEnableVertexAttribArray(ATTRIBUTE_TEXTURE_COORDINATES);
glVertexAttribPointer(ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, _positionStride, (void*)0);
glVertexAttribPointer(ATTRIBUTE_TEXTURE_COORDINATES, 2, GL_FLOAT, GL_FALSE, _positionStride, (void*)_textureCoordinatesOffset);
Where indices are just simple integers, as:
//Vertex attributes.
enum
{
ATTRIBUTE_POSITION, //0
ATTRIBUTE_TEXTURE_COORDINATES //1
};
It works now. So I don't have to call glVertexAttribPointer before every draw call.
Upvotes: 3
Reputation: 16638
As the doc says (http://www.opengl.org/sdk/docs/man/xhtml/glBindAttribLocation.xml):
Attribute variable name-to-generic attribute index bindings for a program object can be explicitly assigned at any time by calling glBindAttribLocation. Attribute bindings do not go into effect until glLinkProgram is called. After a program object has been linked successfully, the index values for generic attributes remain fixed (and their values can be queried) until the next link command occurs.
Applications are not allowed to bind any of the standard OpenGL vertex attributes using this command, as they are bound automatically when needed. Any attribute binding that occurs after the program object has been linked will not take effect until the next time the program object is linked.
Upvotes: 0