4

Possible Duplicate:
Explicit vs Automatic attribute location binding for OpenGL shaders
Why should I use glBindAttribLocation?

I tried to call glGetAttribLocation without binding any attrib locations and it seemed to work. So I can always cache the attrib locations in array if I want to have instant access. What is the purpose of using glBindAttribLocation then ?

[OpenGL 2.0]

Community
  • 1
  • 1
Pythagoras of Samos
  • 3,051
  • 5
  • 29
  • 51
  • 1
    See also [Explicit vs Automatic attribute location binding for OpenGL shaders](http://stackoverflow.com/questions/4635913/explicit-vs-automatic-attribute-location-binding-for-opengl-shaders), which has a good discussion of this. – Brad Larson Jan 17 '11 at 19:04

1 Answers1

8

glBindAttribLocation "binds" an index to an attribute name. This allows you to use the same indices for the same attributes across different shaders. For example: vertex coordinates = 0, texture coordinates = 1, normals = 2. This simplifies drawing code, by conforming the shader to your code, rather than the other way around (requesting attrib locations).

In my code I create an enum for common vertex attributes:

enum
{
    GRAPHICS_ATTRIB_VERTEX = 0,
    GRAPHICS_ATTRIB_NORMAL,
    GRAPHICS_ATTRIB_TEXTURE,
};

Bind them using glBindAttribLocation, then I can use them like this:

    glVertexAttribPointer(GRAPHICS_ATTRIB_VERTEX, ....);

This will work with all my shaders with no calls to glGetAttribLocation.

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92