Related Topics: Vertex Buffer Object , Display List
Download: vertexArray.zip
Instead you specify individual vertex data in immediate mode (between glBegin() and glEnd() pairs), you can store vertex data in a set of arrays including vertex coordinates, normals, texture coordinates and color information. And you can draw geometric primitives by dereferencing the array elements with array indices.
Take a look the following code to draw a cube with immediate mode.
Each face needs 4 times of glVertex*() calls to make a quad, for example, the quad at front is v0-v1-v2-v3. A cube has 6 faces, so the total number of glVertex*() calls is 24. If you also specify normals and colors to the corresponding vertices, the number of function calls increases to 3 times more; 24 of glColor*() and 24 of glNormal*().
The other thing that you should notice is the vertex "v0" is shared with 3 adjacent polygons; front, right and up face. In immediate mode, you have to provide the shared vertex 3 times, once for each face as shown in the code.
glBegin(GL_QUADS); // draw a cube with 6 quads glVertex3fv(v0); // front face glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v0); // right face glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glVertex3fv(v0); // up face glVertex3fv(v5); glVertex3fv(v6); glVertex3fv(v1); ... // draw other 3 faces glEnd();
Using vertex arrays reduces the number of function calls and redundant usage of shared vertices. Therefore, you may increase the performance of rendering. Here, 3 different OpenGL functions are explained to use vertex arrays; glDrawArrays() , glDrawEle