I know one good reason to prefer an explicit location.
Note that geometry data is stored in vertex array objects. For this object, you create a VAO so that the indices match, for example:
- index 0 : position,
- index 1 : normals,
- index 2 : texcoords
Now consider that you want to draw a single object using two different shaders . For one shader, a position and normal data are required, and the rest - positions and texture coordinates .
If you compile these shaders, you will notice that the first shader expects the positions in the attribute index 0 and normals to be 1. Others will expect positions at 0, but texture coordinates at 1.
Quote https://www.opengl.org/wiki/Vertex_Shader :
Automatic assignment
If neither of the two previous methods assigns an input to the attribute index, then the index is automatically assigned to OpenGL when the program is connected. The specified index is completely arbitrary and may be different for different related programs, even if they use the exact vertex shader code.
This means that you cannot use your VAO with both shaders. Instead of having one VAO on, say, an object, you need - in the worst case - a separate VAO for each object per shader .
Forcing shaders to use your attribute numbering convention through glBindAttribLocation can solve this problem easily - all you have to do is keep a consistent relationship between the attributes and their set identifiers and force the shaders to use this convention when stitching.
(This is not a very big problem if you are not using separate VAOs, but you can still make the code more understandable.)
BTW:
When setting the location of attributes for the OpenGL shader program, you come across two options
There is a third option in OpenGL / GLSL 3.3: Indicate the location directly in the shader code . It looks like this:
layout(location=0) in vec4 position;
But this is not in the shader language GLSL ES.