CHAPTER 9: Performance ’n’ Stuff (^291)
if(m_UseNormals)
{
vboBuffer += totalXYZBytes;
memcpy(vboBuffer,m_NormalData,totalNormalBytes);
}
if(m_UseTexture)
{
vboBuffer += totalNormalBytes;
memcpy(vboBuffer,m_TexCoordsData,totalTexCoordinateBytes);
}
glUnmapBufferOES(GL_ARRAY_BUFFER); //7
m_TotalXYZBytes=totalXYZBytes;
m_TotalNormalBytes=totalNormalBytes;
}
Here’s what’s going on:
First we need to generate a name for this VBO in line 1, in a manner
similar to textures.
Next, tally up the size of each vertex in the lines 2ff. And in this case a
vertex is the summation of its coordinates, texture coordinates, and
the normal vector, as required. The total is now multiplied by the total
number of vertices.
Line 3 actually allocates the memory on the GPU. This can be deleted
by using glDeleteBuffer() when your object is no longer needed. The
final parameter is a hint to the driver saying that the data is never
expected to change. If you expect to update it, then use
GL_DYNAMIC_DRAW. Don’t be surprised if you see no change between the
two, because the current driver could very well ignore it.
Line 4 starts the process of uploading the data to the cache. This can
be done a couple of ways: via memory mapping or a direct upload.
Here we use memory mapping by calling glMapBufferOES(). This
returns a pointer to a memory-mapped portion of the GPU’s data
storage inside the application’s address space. The other method uses
the more traditional glBufferData(). The advantage of the former is
that it can prevent an extra memory copy on part of your application
that would otherwise be necessary to join multiple components
together into one big array for uploading.
Now calculate the total number of bytes for each data type. Both the
normals and the xyz coordinates require the same memory each, while
we’re using only 2D texture coordinates.
The magic starts in lines 6ff where it is possible to copy the individual
buffers one at a time by merely using memcpy().
glUnmapBufferOES() in line 7 forces the actual copy action to execute.
singke
(singke)
#1