GLES2 Is glBindAttribLocation() Necessary?
Asked Answered
P

1

11

This might be a noob question. As I understand it, glBindAttribLocation(..., AAA, ...) will bind an attribute within the program to the location ID of AAA, as long as AAA is unique. If I have the following code:

glBindAttribLocation(..., 0, "XXX");
glBindAttribLocation(..., 1, "YYY");

This will bind my two variables to location ID 0 and 1. I would then call:

glBindBuffer(GL_ARRAY_BUFFER, VBId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBId);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void *) 0 + sizeof(float) * 3);
glEnableVertexAttribArray(1);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

Up to this point, I understand that the IDs of 0 and 1 are passed into glVertexAttribPointer() as the first parameter.

Would it be safe to assume that if I obtained the location IDs of the attributes (as above) through glGetAttribLocation() calls, such that the IDs returned were 5 and 6 instead of 0 and 1, could I pass 5 and 6 into glVertexAttribPointer() and glEnableVertexAttribArray() instead of 0 and 1??

Perpetuate answered 28/9, 2011 at 1:34 Comment(0)
S
13

glGetAttribLocation() returns the correct index. So yes, it is safe to use those values in glVertexAttribPointer() and glEnableVertexAttribArray().

But calling glGetAttribLocation for each shader might be expensive if you don't cache it. Using glBindAttribLocation() allows to use a convention, position always in 0, normal in 1 and so on.

Stocking answered 4/10, 2011 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.