I've read in tutorials that the bare minimum a vertex shader needs is define below:
attribute vec3 v3_variable;
attribute vec3 v3Pos;
void main()
{
gl_Position = v3Pos;
}
OpenglES passes to the shader vertex data to v3Pos
but the name of this variable can actually be anything, other tutorials name it a_Position
, a_Pos
, whatever~.
So my question is, how does OpenglES know that when I call glVertexAttribPointer()
it knows that it should put that data into v3Pos
instead of v3_variable
?
There are two methods of identifying which attributes in your shaders get 'paired' with the data you provide to glVertexAttribPointer
. The only identifier you get with glVertexAttribPointer
is the index
(the first parameter), so it must be done by index somehow.
In the first method, you obtain the index of the attribute from your shader after it is compiled and linked using glGetAttribLocation, and use that as the first parameter, eg.
GLint program = ...;
GLint v3PosAttributeIndex = glGetAttribLocation(program, "v3Pos");
GLint v3varAttributeIndex = glGetAttribLocation(program, "v3_variable");
glVertexAttribPointer(v3PosAttributeIndex, ...);
glVertexAttribPointer(v3varAttributeIndex, ...);
You can also explicitly set the layout of the attributes when authoring the shader (as @j-p's comment suggests), so you would modify your shader like so:
layout(location = 1) in vec3 v3_variable;
layout(location = 0) in vec3 v3Pos;
And then in code you would set them explicitly:
glVertexAttribPointer(0, ...); // v3Pos data
glVertexAttribPointer(1, ...); // v3_variable data
This second method only works in GLES 3.0+ (and OpenGL 3.0+), so it may not be applicable to your case if you are only using GLES 2.0. Also, note that you can use both of these methods together. Eg. even if you explicitly define the layouts, it doesn't restrict you from querying them later.