290 CHAPTER 9: Performance ’n’ Stuff^
(GPU). Each data transfer takes a finite amount of time, and performance could be
increased if, say, some of the data could be cached on the GPU itself. Vertex buffer
objects are a means of allocating commonly used data on the GPU that can then be
called to be displayed without having to resubmit it each time.
The process of creating and using VBOs should be familiar to you because it mimics the
process used for textures: first generate a ‘‘name,’’ allocate space for the data, load the
data, and then use glBindBuffer() whenever you want to use it.
Listing 9-1 shows how I created a VBO out of the planetary data. Since most planets are
generally the same shape, that is, roundish, it is possible to cache one model of the
sphere on the CPU and use it for any planet or moon, short of Phobos or Deimos or
Hyperion or Nix or Miranda....
Listing 9-1. Creating a VBO for the Planet Model
-(void)createVBO
{
int numXYZElements=3;
int numNormalElements=3;
int numColorElements=4;
int numTextureCoordElements=2;
long totalXYZBytes;
long totalNormalBytes;
long totalTexCoordinateBytes;
int numBytesPerVertex;
glGenBuffers(1, &m_VBO_SphereDataName); //1
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_SphereDataName);
numBytesPerVertex=numXYZElements; //2
if(m_UseNormals)
numBytesPerVertex+=numNormalElements;
if(m_UseTexture)
numBytesPerVertex+=numTextureCoordElements;
numBytesPerVertex*=sizeof(GLfloat);
//3
glBufferData(GL_ARRAY_BUFFER, numBytesPerVertex*m_NumVertices, 0, GL_STATIC_DRAW);
//4
GLubyte *vboBuffer=(GLubyte *)glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
//5
totalXYZBytes=numXYZElements*m_NumVertices*sizeof(GLfloat);
totalNormalBytes=numNormalElements*m_NumVertices*sizeof(GLfloat);
totalTexCoordinateBytes=numTextureCoordElements*m_NumVertices*sizeof(GLfloat);
memcpy(vboBuffer,m_VertexData,totalXYZBytes); //6