Game Development Reference
In-Depth Information
C H A P T E R 5
■ ■ ■
Hybrid 3D Graphics
with OpenGL and JNI
The classic Asteroids arcade game presented in the previous chapter provided a great introduction to
drawing techniques in Android, using polygons and user-defined layouts. Now it's time to ramp things
up a notch.
In this chapter, you will learn a neat trick to mix OpenGL code in Java and C. This is a key step in
reusing large portions of OpenGL C code along with Java code, thus using the best features of each
language for maximum savings in time and costs.
Any game developer knows that OpenGL is the holy grail of advanced game development. You won't
find any powerful games that are not written with this API, because it takes advantage of hardware
acceleration, which is infinitely superior to any kind of software renderer.
OpenGL can be a scary subject to the newcomer due to its complexity. But you don't have to be an
OpenGL guru to understand what it does and how to draw elements with this API. All you need is the
desire to learn a powerful and exciting tool for gaming.
The goal of this chapter is not to teach you OpenGL (a whole topic wouldn't be enough for that), but
to show you how you can take the Android OpenGL sample provided by Google and modify it in a
completely different way by mixing OpenGL API calls in both Java and native C for maximum reusability.
Some may say this is simply another OpenGL chapter for a mobile device (dime a dozen, right?).
Well, it is not. This chapter presents a technique for OpenGL in Android that is unique, and at the time of
this writing, not available anywhere in the Android sphere (on the Web). This is a technique I stumbled
on by accident when thinking about porting the game Quake to Android. In a nutshell, the technique
consists of creating the OpenGL context, display, and surface objects in Java, and performing all drawing
operations natively in C. At the end of the rendering cycle, a JNI callback is used by the C engine to tell
the Java side to swap the buffers (render the image). The cycle then repeats itself. This technique is
extremely useful when you have a 200,000-line code game like Quake, and rewriting this code in Java is
simply not feasible.
The chapter starts by examining the OpenGL tumbling cubes sample to expose how OpenGL works
in Java. Next, we will look at how sections of the rendering process can be implemented in the native
layer, and how everything is bound by JNI. The final section discusses some of the limitations of the
OpenGL OpenGL Embedded System when it comes to advanced 3D games.
Let's get started.
 
Search WWH ::




Custom Search