Inquiry about attribute locations in glsl

I have the following code in my application:

positionLocation = glGetAttribLocation(pid, "position");
normalLocation = glGetAttribLocation(pid, "normal");
texCoordsLocation = glGetAttribLocation(pid, "texCoords");

positionLocation = 2
normalLocation = 0
texCoordsLocation = 1

Can someone explain the logic to how these locations are assigned their values? I would think they would be assigned chronologically… but I guess not.

The short answer is that the OpenGL graphics driver picks these values when it compiles the shader.
The long answer, buried somewhere in the Graphics driver, could relate to any number of variables including how the variables are used in the shader, what data types they refer to, which appear first in shader, their alphabetical names, etc… :clue:

I believe it’s random, but you can always assign the location in GLSL like so.


layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;

only in modern GSLS version, I don’t know atm 3.3 maybe.

GLSL 1.40 which was released with openGL 3.1 .

Good point

And pre GLSL 1.4 you can assign it in the program with glBindAttribLocation(): http://www.opengl.org/sdk/docs/man2/xhtml/glBindAttribLocation.xml