Reputation: 2490
I am following the Ray Wenderlich OpenGL ES 2 tutorial for the iPhone.
I am confused about his usage of glVertexAttribPointer
and was hoping someone could explain why he set the parameters like he did. Specifically for the use of sizeof()
for the final parameter in glVertexAttribPointer
. How does sizeof
relate to data offset?
Here is the Vertex struct:
typedef struct{
float Position[3];
float Color[4];
}
Vertex;
And the Vertex and Indices data code:
const Vertex Vertices[] = {
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {1, 0, 0, 1}},
{{-1, 1, 0}, {0, 1, 0, 1}},
{{-1, -1, 0}, {0, 1, 0, 1}},
{{1, -1, -1}, {1, 0, 0, 1}},
{{1, 1, -1}, {1, 0, 0, 1}},
{{-1, 1, -1}, {0, 1, 0, 1}},
{{-1, -1, -1}, {0, 1, 0, 1}}
};
const GLubyte Indices[] = {
// Front
0, 1, 2,
2, 3, 0,
// Back
4, 6, 5,
4, 7, 6,
// Left
2, 7, 3,
7, 6, 2,
// Right
0, 4, 1,
4, 1, 5,
// Top
6, 2, 1,
1, 6, 5,
// Bottom
0, 3, 7,
0, 7, 4
};
Could please explain why the parameters are set like they are specially the whole sizeof
thing.
Thanks!!
Upvotes: 2
Views: 866
Reputation: 589
He's using vbo's. So, basically, passing the size of the vertex struct sets the stride. Ie: where the next vertex data can be found.
Think of the VBO containing a linear set of all the vertex structs. The first begins at 0 (within the VBO), the next at sizeof(Vertex), then sizeof(Vertex)*2, etc.
The "position" is at the beginning of the struct, so there is no offset into the structure required (hence, the 6th parameter is 0, but if you look at the color attribute, the stride is the same, but the offset is 3 floats - ie: after the position data).
Upvotes: 3