www.it-ebooks.info www.it-ebooks.info iPhone 3D Programming www.it-ebooks.info www.it-ebooks.info iPhone 3D Programming Developing Graphical Applications with OpenGL ES Philip Rideout foreword by Serban Porumbescu Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info iPhone 3D Programming by Philip Rideout Copyright © 2010 Philip Rideout. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Brian Jepson Production Editor: Loranah Dimant Copyeditor: Kim Wimpsett Proofreader: Teresa Barensfeld Production Services: Molly Sharp Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: May 2010: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. iPhone 3D Programming, the image of a grasshopper, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. TM This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-80482-4 [M] 1272645927 www.it-ebooks.info Frank and Doris Rideout 1916–1998 and 1919–2007 www.it-ebooks.info www.it-ebooks.info Table of Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv 1. Quick-Start Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Transitioning to Apple Technology 3 Objective-C 3 A Brief History of OpenGL ES 4 Choosing the Appropriate Version of OpenGL ES 4 Getting Started 5 Installing the iPhone SDK 6 Building the OpenGL Template Application with Xcode 6 Deploying to Your Real iPhone 8 HelloArrow with Fixed Function 9 Layering Your 3D Application 9 Starting from Scratch 11 Linking in the OpenGL and Quartz Libraries 12 Subclassing UIView 13 Hooking Up the Application Delegate 19 Setting Up the Icons and Launch Image 21 Dealing with the Status Bar 22 Defining and Consuming the Rendering Engine Interface 22 Implementing the Rendering Engine 26 Handling Device Orientation 30 Animating the Rotation 31 HelloArrow with Shaders 34 Shaders 34 Frameworks 36 GLView 36 RenderingEngine Implementation 37 Wrapping Up 42 vii www.it-ebooks.info 2. Math and Metaphors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 The Assembly Line Metaphor 43 Assembling Primitives from Vertices 44 Associating Properties with Vertices 47 The Life of a Vertex 49 The Photography Metaphor 51 Setting the Model Matrix 55 Setting the View Transform 58 Setting the Projection Transform 59 Saving and Restoring Transforms with Matrix Stacks 62 Animation 64 Interpolation Techniques 64 Animating Rotation with Quaternions 65 Vector Beautification with C++ 66 HelloCone with Fixed Function 69 RenderingEngine Declaration 69 OpenGL Initialization and Cone Tessellation 71 Smooth Rotation in Three Dimensions 75 Render Method 77 HelloCone with Shaders 78 Wrapping Up 81 3. Vertices and Touch Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Reading the Touchscreen 83 Saving Memory with Vertex Indexing 89 Boosting Performance with Vertex Buffer Objects 94 Creating a Wireframe Viewer 98 Parametric Surfaces for Fun 100 Designing the Interfaces 104 Handling Trackball Rotation 106 Implementing the Rendering Engine 108 Poor Man’s Tab Bar 111 Animating the Transition 115 Wrapping Up 117 4. Adding Depth and Realism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Examining the Depth Buffer 119 Beware the Scourge of Depth Artifacts 121 Creating and Using the Depth Buffer 123 Filling the Wireframe with Triangles 125 Surface Normals 128 Feeding OpenGL with Normals 128 The Math Behind Normals 129 viii | Table of Contents www.it-ebooks.info [...]... newfangled programming language or platform But I honestly think there’s no better aphorism to describe iPhone graphics programming Whether you’re a professional or a hobbyist, I hope this book can play a small role in helping you rediscover the joy of programming This book is not an OpenGL manual, but it does teach many basic OpenGL concepts as a means to an end, namely, 3D graphics programming on the iPhone. .. way If you’re already familiar with 3D graphics but haven’t done much with the iPhone, you can still learn a thing or two from this book There are certain sections that you can probably skip over Much of Chapter 2 is an overview of general 3D graphics concepts; I won’t be offended if you just skim through it Conversely, if you have iPhone experience but are new to 3D graphics, you can gloss over some... www.it-ebooks.info Figure 1-1 Mac OS X and iPhone programming stacks 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... head over to Apple’s iPhone developer site and download the SDK With only the free SDK in hand, you have the tools at your disposal to develop complex applications and even test them on the iPhone Simulator The iPhone Simulator cannot emulate certain features such as the accelerometer, nor does it perfectly reflect the iPhone s implementation of OpenGL ES For example, a physical iPhone cannot render... 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... book into your product’s documentation does require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: iPhone 3D Programming by Philip Rideout Copyright 2010 Philip Rideout, 978-0-596-80482-4.” If you feel your use of the code examples falls outside fair use or the permission given here, feel free to contact... Apple’s newer handheld devices, such as the iPhone 3GS and iPad, have graphics hardware that supports both ES 1.1 and 2.0; these devices are said to have a programmable graphics pipeline because the graphics processor executes instructions rather than performing fixed mathematical operations Older devices like the first-generation iPod touch, iPhone, and iPhone 3G are said to have a fixed-function... 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... least fails gracefully in the rare case where it leverages a feature not supported on the simulator If you do own an iPhone and are willing to cough up a reasonable fee ($100 at the time of this writing), you can join Apple’s iPhone Developer Program to enable deployment to a physical iPhone When I did this, it was not a painful process, and Apple granted me approval almost immediately If the approval... within the Xcode UI; for example, a menu might be renamed or shifted in future versions However, the actual sample code is relatively future-proof Installing the iPhone SDK You can download the iPhone SDK from here: http://developer.apple.com /iphone/ It’s packaged as a dmg file, Apple’s standard disk image format After you download it, it should automatically open in a Finder window—if it doesn’t, you . www.it-ebooks.info www.it-ebooks.info iPhone 3D Programming www.it-ebooks.info www.it-ebooks.info iPhone 3D Programming Developing Graphical Applications with. Porumbescu Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info iPhone 3D Programming by Philip Rideout Copyright © 2010 Philip Rideout. All rights