I want to draw a cube with the following code, and I find the procedure A work well , the procedure B doesn’t. I can’t find any problem, if anyone find the problem with Procedure B, please tell me.
GLfloat vertex_listss[][3] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
GLfloat normalize[][3] = {
0, 0, -1.0f,
0, 0, -1.0f,
0, 0, -1.0f,
0, 0, -1.0f,
-1.0f, 0, 0,
-1.0f, 0, 0,
-1.0f, 0, 0,
-1.0f, 0, 0,
0, -1.0f, 0,
0, -1.0f, 0,
0, -1.0f, 0,
0, -1.0f, 0,
0, 0, 1.0f,
0, 0, 1.0f,
0, 0, 1.0f,
0, 0, 1.0f,
1.0f, 0, 0,
1.0f, 0, 0,
1.0f, 0, 0,
1.0f, 0, 0,
0, 1.0f, 0,
0, 1.0f, 0,
0, 1.0f, 0,
0, 1.0f, 0,
};
// the index of the vertexs
static const GLint index_list[] = {
0, 2, 3, 1,
0, 4, 6, 2,
0, 1, 5, 4,
4, 5, 7, 6,
1, 3, 7, 5,
2, 6, 7, 3,
};
void cubedraw(){
#if 0 // Procedure A
for(int i = 0 ; i < sizeof(index_list)/sizeof(index_list[0]); i += 4) {
glBegin(GL_QUADS);
glNormal3f(normalize[i][0],normalize[i][1],normalize[i][2]);
for(int j = i; j < i + 4; j ++) {
int index = index_list[j];
glVertex3f(vertex_listss[index][0],
vertex_listss[index][1],
vertex_listss[index][2]);
}
glEnd();
}
#else // Procedure B
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, normalize);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertex_listss);
glDrawElements(GL_QUADS,
sizeof(index_list)/sizeof(index_list[0]),
GL_UNSIGNED_INT, index_list);
#endif
};