I’m looking at an example from apple how to use vertex arrays:
typedef struct _vertexStruct
{
GLfloat position[2];
GLubyte color[4];
} vertexStruct;
enum {
ATTRIB_POSITION,
ATTRIB_COLOR,
NUM_ATTRIBUTES };
void DrawModel()
{
const vertexStruct vertices[] = {...};
const GLubyte indices[] = {...};
glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertexStruct), &vertices[0].position);
glEnableVertexAttribArray(ATTRIB_POSITION);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE,
sizeof(vertexStruct), &vertices[0].color);
glEnableVertexAttribArray(ATTRIB_COLOR);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices)/sizeof(GLubyte),
GL_UNSIGNED_BYTE, indices);
}
(source)
How does OpenGL know which one is color and which one is vertex? ATTRIB_POSITION and ATTRIB_COLOR are user defined, so opengl shouldn’t know what it means. Specifically I try to use constant color and vertex/texture arrays. If I’ll change ATTRIB_COLOR to ATTRIB_TEXTURE OpenGL won’t notice a thing, how can I do that?
Note: the following assumes OpenGL ES 2.0.
OpenGL neither knows nor cares about what the meaning behind any particular vertex attribute is. All it wants is a number: an attribute index.
Your vertex shader defines a number of attributes that it takes as inputs, using the
attributekeyword. It is your job to connect these GLSL attributes to OpenGL attribute indices, usingglBindAttribLocationbefore linking the program. So attribute 0 is only the position if you bind the shader position attribute to attribute 0.