
- •Contents
- •Acknowledgments
- •Preface
- •What Makes Android Special?
- •Who Should Read This Book?
- •Online Resources
- •Fast-Forward >>
- •Introducing Android
- •Quick Start
- •Installing the Tools
- •Creating Your First Program
- •Running on the Emulator
- •Running on a Real Phone
- •Key Concepts
- •The Big Picture
- •Building Blocks
- •Using Resources
- •Safe and Secure
- •Android Basics
- •Designing the User Interface
- •Introducing the Sudoku Example
- •Designing by Declaration
- •Creating the Opening Screen
- •Using Alternate Resources
- •Implementing an About Box
- •Applying a Theme
- •Adding a Menu
- •Adding Settings
- •Starting a New Game
- •Debugging
- •Exiting the Game
- •Exploring 2D Graphics
- •Learning the Basics
- •Adding Graphics to Sudoku
- •Handling Input
- •The Rest of the Story
- •Making More Improvements
- •Multimedia
- •Playing Audio
- •Playing Video
- •Adding Sounds to Sudoku
- •Storing Local Data
- •Adding Options to Sudoku
- •Continuing an Old Game
- •Remembering the Current Position
- •Accessing the Internal File System
- •Accessing SD Cards
- •Beyond the Basics
- •The Connected World
- •Browsing by Intent
- •Web with a View
- •From JavaScript to Java and Back
- •Using Web Services
- •Locating and Sensing
- •Location, Location, Location
- •Set Sensors to Maximum
- •Putting SQL to Work
- •Introducing SQLite
- •Hello, Database
- •Data Binding
- •Using a ContentProvider
- •Implementing a ContentProvider
- •3D Graphics in OpenGL
- •Understanding 3D Graphics
- •Introducing OpenGL
- •Building an OpenGL Program
- •Rendering the Scene
- •Building a Model
- •Lights, Camera, ...
- •Action!
- •Applying Texture
- •Peekaboo
- •Measuring Smoothness
- •Fast-Forward >>
- •The Next Generation
- •Multi-Touch
- •Building the Touch Example
- •Understanding Touch Events
- •Setting Up for Image Transformation
- •Implementing the Drag Gesture
- •Implementing the Pinch Zoom Gesture
- •Hello, Widget
- •Live Wallpaper
- •Write Once, Test Everywhere
- •Gentlemen, Start Your Emulators
- •Building for Multiple Versions
- •Evolving with Android APIs
- •Bug on Parade
- •All Screens Great and Small
- •Installing on the SD Card
- •Publishing to the Android Market
- •Preparing
- •Signing
- •Publishing
- •Updating
- •Closing Thoughts
- •Appendixes
- •Bibliography
- •Index

Chapter 10
3D Graphics in OpenGL
Two-dimensional graphics are great for most programs, but sometimes you need an extra level of depth, interactivity, or realism that isn’t possible in 2D. For these times, Android provides a three-dimensional graphics library based on the OpenGL ES standard. In this chapter, we’ll explore 3D concepts and build up a sample program that uses OpenGL.
10.1Understanding 3D Graphics
The world is three-dimensional, yet we routinely view it in two dimensions. When you watch television or look at a picture in a book, the 3D images are flattened out, or projected, onto a 2D surface (the TV panel or book page).
Try this simple experiment: cover one eye and look out the window. What do you see? Light from the sun bounces off objects outside, passes through the window, and travels to your eye so you can perceive it. In graphics terms, the scene outside is projected onto the window (or viewport). If someone replaced your window with a high-quality photograph, it would look the same until you moved.
Based on how close your eye is to the window and how big the window is, you can see a limited amount of the world outside. This is called your field of view. If you draw a line from your eye to the four corners of the window and beyond, you would get the pyramid in Figure 10.1, on the next page. This is called the view frustum (Latin for a “piece broken off”). For performance reasons, the frustum is usually bounded by near and far clipping planes as well. You can see everything inside the frustum but nothing outside of it.

INTRODUCING OPENGL 199
Figure 10.1: Viewing a three-dimensional scene
In 3D computer graphics, your computer screen acts as the viewport. Your job is to fool the user into thinking it’s a window into another world just on the other side of the glass. The OpenGL graphics library is the API you use to accomplish that.
10.2Introducing OpenGL
OpenGL1 was developed by Silicon Graphics in 1992. It provides a unified interface for programmers to take advantage of hardware from any manufacturer. At its core, OpenGL implements familiar concepts such as viewports and lighting and tries to hide most of the hardware layer from the developer.
Because it was designed for workstations, OpenGL is too large to fit on a mobile device. So, Android implements a subset of OpenGL called OpenGL for Embedded Systems (OpenGL ES).2 This standard was cre-
1. http://www.opengl.org
2.http://www.khronos.org/opengles

BUILDING AN OPENGL PROGRAM 200
Thank You, John Carmack
OpenGL has proven to be very successful, but it almost wasn’t. In 1995, Microsoft introduced a competitor called Direct3D. Owing to Microsoft’s dominant market position and significant R&D investments, for a while it looked like Direct3D was going to take over as a de facto industry standard for gaming. However, one man, John Carmack, cofounder of id Software, refused to comply. His wildly popular Doom and Quake games almost single-handedly forced hardware manufacturers to keep their OpenGL device drivers up-to-date on the PC. Today’s Linux, Mac OS X, and mobile device users can thank John and id Software for helping to keep the OpenGL standard relevant.
ated by the Khronos Group, an industry consortium of companies such as Intel, AMD, Nvidia, Nokia, Samsung, and Sony. The same library (with minor differences) is now available on major mobile platforms including Android, Symbian, and iPhone.
Every language has its own language bindings for OpenGL ES, and Java is no exception. Java’s language binding was defined by Java Specification Request (JSR) 239.3 Android implements this standard as closely as possible, so you can refer to a variety of books and documentation on JSR 239 and OpenGL ES for a full description of all its classes and methods.
Now let’s take a look at how to create a simple OpenGL program in
Android.
10.3Building an OpenGL Program
Begin by creating a new “Hello, Android” project as in Section 1.2, Creating Your First Program, on page 23, but this time supply the following parameters in the New Android Project dialog box:
Project name: OpenGL
Build Target: Android 2.2
Application name: OpenGL
Package name: org.example.opengl
Create Activity: OpenGL
Min SDK Version: 8
3. http://jcp.org/en/jsr/detail?id=239

BUILDING AN OPENGL PROGRAM 201
Joe Asks. . .
Will Every Phone Have 3D?
Yes and no. Some low-end devices running Android may not actually have 3D hardware. However, the OpenGL programming interface will still be there. All the 3D functions will be emulated in software. Your program will still run, but it will be much slower than a hardware-accelerated device. For this reason, it’s a good idea to provide options for users to turn off certain details and special effects that take time to draw but aren’t absolutely necessary for the program. That way, if the user is running your program on a slower device, they can disable some of your eye candy to get better performance.
This will create OpenGL.java to contain your main activity. Edit this, and change it to refer to a custom view named GLView, as shown here:
Download OpenGL/src/org/example/opengl/OpenGL.java
package org.example.opengl;
import android.app.Activity; import android.os.Bundle;
public class OpenGL extends Activity { GLView view;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
view = new GLView(this); setContentView(view);
}
@Override
protected void onPause() { super.onPause(); view.onPause();
}
@Override
protected void onResume() { super.onResume(); view.onResume();
}
}