CHAPTER 9: Performance ’n’ Stuff (^293)
Rendering the VBOs is pretty straightforward, with only a single ‘‘huh?’’ in the process.
Line 1 binds it, in the same way a texture is bound. This simply makes
it the current object in use, until another is bound or this one is
unbound with glBindBuffer(GL_ARRAY_BUFFER, 0);.
Face culling is disabled in line 2 for testing purposes, so we know all
faces are being rendered and not just the front-facing ones.
Lines 3ff enable the various data buffers as has been done in previous
execute() methods.
Lines 4ff are the different ones. When using VBOs, the various pointers
to the data blocks are the offset from the first element, which always
starts at ‘‘address’’ of zero instead one in the app’s own address
space. So, the vertex pointer starts at address of 0, while the normals
are right after the vertices, and the texture coordinates are right after
the normals.
The array is now drawn in line 5 just as before.
When it comes to optimizing code, I am one who needs to be convinced that a specific
trick will work. Otherwise, I might spend a lot of time doing something that increases the
frame rate by .23 percent. A game programmer might find that a badge of honor, but I
feel that it steals an optional new feature, or bug fix away from my users by diverting my
attention on something that would never be noticed. So, I developed a simple test
program to try the various techniques described here. The previous two listings are from
that effort.
Once you’re sure that the VBO operates as planned, have the program draw multiple
earths on top of each other. The following lines will render 10 additional planets while
rotating them at the same time:
for(i=0;i<10;i++)
{
glTranslatef(0.0, 0.01, 0.0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (m_Slices+1)2(m_Stacks-1)+2);
}
And generate the spheres with 200 stacks and 200 slices, giving them a total of 80,400
vertices, which yields more than 2.5MB of data. I use the same instance, so I have to
load the GPU only once at program startup. Without VBOs, the same model would be
loaded 11 times. See Figure 9-1.
singke
(singke)
#1