O'Reilly logo

iPhone 3D Programming by Philip Rideout

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Quick-Start Guide

Rumors of my assimilation are greatly exaggerated.

Captain Picard, Star Trek: First Contact

In this chapter, you’ll plunge in and develop your first application from scratch. The goal is to write a HelloArrow program that draws an arrow and rotates it in response to an orientation change.

You’ll be using the OpenGL ES API to render the arrow, but OpenGL is only one of many graphics technologies supported on the iPhone. At first, it can be confusing which of these technologies is most appropriate for your requirements. It’s also not always obvious which technologies are iPhone-specific and which cross over into general Mac OS X development.

Apple neatly organizes all of the iPhone’s public APIs into four layers: Cocoa Touch, Media Services, Core Services, and Core OS. Mac OS X is a bit more sprawling, but it too can be roughly organized into four layers, as shown in Figure 1-1.

Mac OS X and iPhone programming stacks

Figure 1-1. Mac OS X and iPhone programming stacks

At the very bottom layer, Mac OS X and the iPhone share their kernel architecture and core operating system; these shared components are collectively known as Darwin.

Despite the similarities between the two platforms, they diverge quite a bit in their handling of OpenGL. Figure 1-1 includes some OpenGL-related classes, shown in bold. The NSOpenGLView class in Mac OS X does not exist on the iPhone, and the iPhone’s EAGLContext and CAEGLLayer classes are absent on Mac OS X. The OpenGL API itself is also quite different in the two platforms, because Mac OS X supports full-blown OpenGL while the iPhone relies on the more svelte OpenGL ES.

The iPhone graphics technologies include the following:

Quartz 2D rendering engine

Vector-based graphics library that supports alpha blending, layers, and anti-aliasing. This is also available on Mac OS X. Applications that leverage Quartz technology must reference a framework (Apple’s term for a bundle of resources and libraries) known as Quartz Core.

Core Graphics

Vanilla C interface to Quartz. This is also available on Mac OS X.

UIKit

Native windowing framework for iPhone. Among other things, UIKit wraps Quartz primitives into Objective-C classes. This has a Mac OS X counterpart called AppKit, which is a component of Cocoa.

Cocoa Touch

Conceptual layer in the iPhone programming stack that contains UIKit along with a few other frameworks.

Core Animation

Objective-C framework that facilitates complex animations.

OpenGL ES

Low-level hardware-accelerated C API for rendering 2D or 3D graphics.

EAGL

Tiny glue API between OpenGL ES and UIKit. Some EAGL classes (such as CAEGLLayer) are defined in Quartz Core framework, while others (such as EAGLContext) are defined in the OpenGL ES framework.

This book chiefly deals with OpenGL ES, the only technology in the previous list that isn’t Apple-specific. The OpenGL ES specification is controlled by a consortium of companies called the Khronos Group. Different implementations of OpenGL ES all support the same core API, making it easy to write portable code. Vendors can pick and choose from a formally defined set of extensions to the API, and the iPhone supports a rich set of these extensions. We’ll cover many of these extensions throughout this book.

Transitioning to Apple Technology

Yes, you do need a Mac to develop applications for the iPhone App Store! Developers with a PC background should quell their fear; my own experience was that the PC-to-Apple transition was quite painless, aside from some initial frustration with a different keyboard.

Xcode serves as Apple’s preferred development environment for Mac OS X. If you are new to Xcode, it might initially strike you as resembling an email client more than an IDE. This layout is actually quite intuitive; after learning the keyboard shortcuts, I found Xcode to be a productive environment. It’s also fun to work with. For example, after typing in a closing delimiter such as ), the corresponding ( momentarily glows and seems to push itself out from the screen. This effect is pleasant and subtle; the only thing missing is a kitten-purr sound effect. Maybe Apple will add that to the next version of Xcode.

Objective-C

Now we come to the elephant in the room. At some point, you’ve probably heard that Objective-C is a requirement for iPhone development. You can actually use pure C or C++ for much of your application logic, if it does not make extensive use of UIKit. This is especially true for OpenGL development because it is a C API. Most of this book uses C++; Objective-C is used only for the bridge code between the iPhone operating system and OpenGL ES.

The origin of Apple’s usage of Objective-C lies with NeXT, which was another Steve Jobs company whose technology was ahead of its time in many ways—perhaps too far ahead. NeXT failed to survive on its own, and Apple purchased it in 1997. To this day, you can still find the NS prefix in many of Apple’s APIs, including those for the iPhone.

Some would say that Objective-C is not as complex or feature-rich as C++, which isn’t necessarily a bad thing. In many cases, Objective-C is the right tool for the right job. It’s a fairly simple superset of C, making it quite easy to learn.

However, for 3D graphics, I find that certain C++ features are indispensable. Operator overloading makes it possible to perform vector math in a syntactically natural way. Templates allow the reuse of vector and matrix types using a variety of underlying numerical representations. Most importantly, C++ is widely used on many platforms, and in many ways, it’s the lingua franca of game developers.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required