Drawing cube with OpenGL

Define an array, put all eight vertices in the array, and then specify each vertex using a pointer, rather than using direct data. This avoids the need to consider a lot of data when specifying vertices, thus reducing the possibility of code errors.
//the cube to save eight points to an array in
static const GLfloat vertex_list [] [3] = {
0.5 f, 0.5 f, 0.5 f,
0.5 f, 0.5 f, 0.5 f,
//…
};
glBegin(GL_LINE_STRIP);
glBegin(GL_LINE_STRIP);
glVertex3fv (vertex_list [0]).
glVertex3fv (vertex_list [2]).
glVertex3fv (vertex_list [3]).
glVertex3fv (vertex_list [1]).

//…
glEnd();

changes, although the code to get longer, but it is easy to read. It’s easy to see that the four vertices 0, 2, 3, and 1 form a square.
a little observation can be found, we use a lot of glVertex3fv function, each one is only one of the vertex sequence number is different, so we can define an array of serial number, put all the serial number is in. This makes the code much simpler.
//cube will save eight points to an array of
static const GLfloat vertex_list [] [3] = {
0.5 f, 0.5 f, 0.5 f,
0.5 f to 0.5 f, 0.5 f, f
– 0.5, 0.5 f to 0.5 f,
0.5 f, 0.5 f, 0.5 f,
– 0.5 f to 0.5 f, 0.5 f,
0.5 f to 0.5 f, 0.5 f,
0.5 f, f 0.5, 0.5 f,
0.5 f, f 0.5, 0.5 f,
};
static const GLint index_list[][4] = {
static const GLint index_list[][4] = {
static const GLint index_list[]
0, 2, 3, 1,
0, 4, 6, 2,
0, 1, 5, 4,
4, 6, 7, 5,
1, 3, 7, 3,
2, 6, 7, 7,};

int i, j;
glBegin(GL_LINE_STRIP)
glBegin(GL_LINE_STRIP)
glBegin(GL_LINE_STRIP);
for(i=0; i< 6;
for(j=0; j=0; j=0; j< 4. + + j)// each side has four vertices, cycle four times
glVertex3fv (vertex_list [index_list [I] [j]]).
glEnd();

This gives us a more mature version of how to draw a cube. The data and code are basically separate. All the vertices are put in one array, and the number of the vertices is put in another array, and the code to draw the cube from these two arrays is very simple.

faces counter clockwise, faces away from us clockwise, we have the index_list array above.

Read More: