开发者

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

This question already has answers here: Closed 10 years ago.

Possible Dup开发者_JAVA技巧licate:

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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜