Graphics Reference
In-Depth Information
glDrawArrays is great if you have a primitive described by a sequence
of element indices and if vertices of geometry are not shared. However,
typical objects used by games or other 3D applications are made up of
multiple triangle meshes where element indices may not necessarily be in
sequence and vertices will typically be shared between triangles of a mesh.
v 6
v 5
v 1
v 0
v 7
v 4
v 3
v 2
Figure 7-4
Cube
Consider the cube shown in Figure 7-4. If we were to draw this using
glDrawArrays , the code would be as follows:
#define VERTEX_POS_INDX 0
#define NUM_FACES 6
GLfloat vertices[] = { … }; // (x, y, z) per vertex
glEnableVertexAttribArray ( VERTEX_POS_INDX );
glVertexAttribPointer ( VERTEX_POS_INDX, 3, GL_FLOAT,
GL_FALSE, 0, vertices );
for (int i=0; i<NUM_FACES; i++)
{
glDrawArrays ( GL_TRIANGLE_FAN, i*4, 4 );
}
Or
glDrawArrays ( GL_TRIANGLES, 0, 36 );
To draw this cube with glDrawArrays , we would call glDrawArrays for
each face of the cube. Vertices that are shared would need to be replicated,
which means that instead of having 8 vertices, we would now need
to allocate 24 vertices (if we draw each face as a GL_TRIANGLE_FAN ) or
36 vertices (if we use GL_TRIANGLES ). This is not an efficient approach.
Search WWH ::




Custom Search