Game Development Reference
In-Depth Information
Reducing Calls to OpenGL ES/JNI Methods
The first suspects are the many OpenGL ES calls we issue per frame when we render the model
for each Bob. First of all, we have four matrix operations per Bob. If we don't need rotation or
scaling, we can bring that down to two calls. Here are the FPS numbers for each device when
we only use glLoadIdentity() and glTranslatef() in the inner loop:
Hero:
12-10 04:57:49.610: DEBUG/FPSCounter(766): fps: 27
12-10 04:57:49.610: DEBUG/FPSCounter(766): fps: 27
12-10 04:57:50.650: DEBUG/FPSCounter(766): fps: 28
12-10 04:57:50.650: DEBUG/FPSCounter(766): fps: 28
12-10 04:57:51.530: DEBUG/dalvikvm(766): GC freed 22910 objects / 568904 bytes in 128ms
Droid:
12-10 05:08:38.604: DEBUG/FPSCounter(1702): fps: 56
12-10 05:08:39.620: DEBUG/FPSCounter(1702): fps: 57
12-10 05:08:40.628: DEBUG/FPSCounter(1702): fps: 58
12-10 05:08:41.644: DEBUG/FPSCounter(1702): fps: 57
Nexus One:
12-10 04:58:01.277: DEBUG/FPSCounter(2509): fps: 54
12-10 04:58:02.287: DEBUG/FPSCounter(2509): fps: 54
12-10 04:58:03.307: DEBUG/FPSCounter(2509): fps: 55
12-10 04:58:04.317: DEBUG/FPSCounter(2509): fps: 55
Well, it improved the performance on the Hero quite a bit, and the Droid and Nexus One also
benefitted a little from removing the two matrix operations. Of course, there's a little bit of
cheating involved: if we need to rotate and scale our Bobs, there's no way around issuing those
two additional calls. However, when all we do is 2D rendering, there's a neat little trick we can
use that will get rid of all matrix operations (we'll look into this trick in the next chapter).
OpenGL ES is a C API provided to Java via a JNI wrapper. This means that any OpenGL ES
method we call has to cross that JNI wrapper to call the actual C native function. This was
somewhat costly on earlier Android versions, but has gotten better with more recent versions. As
shown, the impact is not all that huge, especially if the actual operations take up more time than
issuing the call itself.
The Concept of Binding Vertices
So, is there anything else we can improve? Let's look at our current present() method one more
time [with removed glRotatef() and glScalef() ]:
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glClear(GL10. GL_COLOR_BUFFER_BIT );
gl.glMatrixMode(GL10. GL_MODELVIEW );
for ( int i = 0; i < NUM_BOBS ; i++) {
 
Search WWH ::




Custom Search