Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of using glBindAttributeLocation in GLSL? [duplicate]

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]

like image 204
Pythagoras of Samos Avatar asked Feb 25 '23 08:02

Pythagoras of Samos


1 Answers

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.

like image 152
Justin Meiners Avatar answered Apr 28 '23 01:04

Justin Meiners