What does it take to build an iPhone app with stunning 3D graphics? This book will show you how to apply OpenGL graphics programming techniques to any device running the iPhone OS -- including the iPad and iPod Touch -- with no iPhone development or 3D graphics experience required. iPhone 3D Programming provides clear step-by-step instructions, as well as lots of practical advice, for using the iPhone SDK and OpenGL.You'll build several graphics programs -- progressing from simple to more complex examples -- that focus on lighting, textures, blending, augmented reality, optimization for performance and speed, and much more. All you need to get started is a solid understanding of C++ and a great idea for an app.Learn fundamental graphics concepts, including transformation matrices, quaternions, and more Get set up for iPhone development with the Xcode environment Become familiar with versions 1.1 and 2.0 of the OpenGL ES API, and learn to use vertex buffer objects, lighting, texturing, and shaders Use the iPhone's touch screen, compass, and accelerometer to build interactivity into graphics applications Build iPhone graphics applications such as a 3D wireframe viewer, a simple augmented reality application, a spring system simulation, and more This book received valuable community input through O'Reilly's Open Feedback Publishing System (OFPS). Learn more at http://labs.oreilly.com/ofps.html. O'Reilly Table of Contents 9 Foreword 15 Preface 17 How to Read This Book 17 Conventions Used in This Book 18 Using Code Examples 18 Safari® Books Online 19 How to Contact Us 19 Acknowledgments 19 Chapter 1. Quick-Start Guide 21 Transitioning to Apple Technology 23 Objective-C 23 A Brief History of OpenGL ES 24 Choosing the Appropriate Version of OpenGL ES 24 Getting Started 25 Installing the iPhone SDK 26 Building the OpenGL Template Application with Xcode 26 Deploying to Your Real iPhone 28 HelloArrow with Fixed Function 29 Layering Your 3D Application 29 Starting from Scratch 31 Linking in the OpenGL and Quartz Libraries 32 Subclassing UIView 33 Hooking Up the Application Delegate 39 Setting Up the Icons and Launch Image 41 Dealing with the Status Bar 42 Defining and Consuming the Rendering Engine Interface 42 Implementing the Rendering Engine 46 Handling Device Orientation 50 Animating the Rotation 51 HelloArrow with Shaders 54 Shaders 54 Frameworks 56 GLView 56 RenderingEngine Implementation 57 Wrapping Up 62 Chapter 2. Math and Metaphors 63 The Assembly Line Metaphor 63 Assembling Primitives from Vertices 64 Associating Properties with Vertices 67 The Life of a Vertex 69 The Photography Metaphor 71 Setting the Model Matrix 75 Scale 75 Translation 75 Rotation 76 Setting the View Transform 78 Setting the Projection Transform 79 Saving and Restoring Transforms with Matrix Stacks 82 Animation 84 Interpolation Techniques 84 Animating Rotation with Quaternions 85 Vector Beautification with C++ 86 HelloCone with Fixed Function 89 RenderingEngine Declaration 89 OpenGL Initialization and Cone Tessellation 91 Smooth Rotation in Three Dimensions 95 Render Method 97 HelloCone with Shaders 98 Wrapping Up 101 Chapter 3. Vertices and Touch Points 103 Reading the Touchscreen 103 Saving Memory with Vertex Indexing 109 Boosting Performance with Vertex Buffer Objects 114 Creating a Wireframe Viewer 118 Parametric Surfaces for Fun 120 Designing the Interfaces 124 Handling Trackball Rotation 126 Implementing the Rendering Engine 128 Poor Man’s Tab Bar 131 Animating the Transition 135 Wrapping Up 137 Chapter 4. Adding Depth and Realism 139 Examining the Depth Buffer 139 Beware the Scourge of Depth Artifacts 141 Creating and Using the Depth Buffer 143 Filling the Wireframe with Triangles 145 Surface Normals 148 Feeding OpenGL with Normals 148 The Math Behind Normals 149 Normal Transforms Aren’t Normal 151 Generating Normals from Parametric Surfaces 153 Lighting Up 155 Ho-Hum Ambiance 156 Matte Paint with Diffuse Lighting 156 Give It a Shine with Specular 158 Adding Light to ModelViewer 159 Using Light Properties 162 Shaders Demystified 164 Adding Shaders to ModelViewer 167 New Rendering Engine 169 Per-Pixel Lighting 172 Toon Shading 174 Better Wireframes Using Polygon Offset 175 Loading Geometry from OBJ Files 177 Managing Resource Files 179 Implementing ISurface 182 Wrapping Up 183 Chapter 5. Textures and Image Capture 185 Adding Textures to ModelViewer 185 Enhancing IResourceManager 187 Generating Texture Coordinates 189 Enabling Textures with ES1::RenderingEngine 192 Enabling Textures with ES2::RenderingEngine 195 Texture Coordinates Revisited 200 Fight Aliasing with Filtering 201 Boosting Quality and Performance with Mipmaps 204 Modifying ModelViewer to Support Mipmaps 206 Texture Formats and Types 207 Hands-On: Loading Various Formats 208 Texture Compression with PVRTC 211 The PowerVR SDK and Low-Precision Textures 218 Generating and Transforming OpenGL Textures with Quartz 221 Dealing with Size Constraints 224 Scaling to POT 226 Creating Textures with the Camera 228 CameraTexture: Rendering Engine Implementation 234 Wrapping Up 239 Chapter 6. Blending and Augmented Reality 241 Blending Recipe 242 Wrangle Premultiplied Alpha 244 Blending Caveats 246 Blending Extensions and Their Uses 247 Why Is Blending Configuration Useful? 248 Shifting Texture Color with Per-Vertex Color 249 Poor Man’s Reflection with the Stencil Buffer 250 Rendering the Disk to Stencil Only 252 Rendering the Reflected Object with Stencil Testing 255 Rendering the “Real” Object 256 Rendering the Disk with Front-to-Back Blending 256 Stencil Alternatives for Older iPhones 256 Anti-Aliasing Tricks with Offscreen FBOs 258 A Super Simple Sample App for Supersampling 259 Jittering 267 Other FBO Effects 272 Rendering Anti-Aliased Lines with Textures 272 Holodeck Sample 276 Application Skeleton 277 Rendering the Dome, Clouds, and Text 280 Handling the Heads-Up Display 284 Replacing Buttons with Orientation Sensors 289 Adding accelerometer support 289 Adding compass support 292 Overlaying with a Live Camera Image 295 Wrapping Up 299 Chapter 7. Sprites and Text 301 Text Rendering 101: Drawing an FPS Counter 302 Generating a Glyphs Texture with Python 304 Rendering the FPS Text 308 Stabilizing the counter with a low-pass filter 309 Fleshing out the FpsRenderer class 310 Simplify with glDrawTexOES 313 Crisper Text with Distance Fields 315 Generating Distance Fields with Python 316 Use Distance Fields Under ES 1.1 with Alpha Testing 320 Adding Text Effects with Fragment Shaders 320 Smoothing and Derivatives 321 Implementing Outline, Glow, and Shadow Effects 324 Animation with Sprite Sheets 326 Image Composition and a Taste of Multitexturing 328 Mixing OpenGL ES and UIKit 330 Rendering Confetti, Fireworks, and More: Point Sprites 333 Chapter Finale: SpringyStars 334 Physics Diversion: Mass-Spring System 334 C++ Interfaces and GLView 338 ApplicationEngine Implementation 339 OpenGL ES 1.1 Rendering Engine and Additive Blending 342 Point Sprites with OpenGL ES 2.0 344 Wrapping Up 347 Chapter 8. Advanced Lighting and Texturing 349 Texture Environments under OpenGL ES 1.1 350 Texture Combiners 352 Bump Mapping and DOT3 Lighting 355 Another Foray into Linear Algebra 357 Generating Basis Vectors 358 Normal Mapping with OpenGL ES 2.0 360 Normal Mapping with OpenGL ES 1.1 363 Generating Object-Space Normal Maps 365 Reflections with Cube Maps 367 Render to Cube Map 371 Anisotropic Filtering: Textures on Steroids 372 Image-Processing Example: Bloom 374 Better Performance with a Hybrid Approach 378 Sample Code for Gaussian Bloom 378 Wrapping Up 384 Chapter 9. Optimizing 385 Instruments 385 Understand the CPU/GPU Split 386 Vertex Submission: Above and Beyond VBOs 387 Batch, Batch, Batch 387 Interleaved Vertex Attributes 389 Optimize Your Vertex Format 390 Use the Best Topology and Indexing 391 Lighting Optimizations 392 Object-Space Lighting 392 DOT3 Lighting Revisited 392 Baked Lighting 393 Texturing Optimizations 393 Culling and Clipping 394 Polygon Winding 394 User Clip Planes 395 CPU-Based Clipping 396 Shader Performance 396 Conditionals 396 Fragment Killing 397 Texture Lookups Can Hurt! 397 Optimizing Animation with Vertex Skinning 397 Skinning: Common Code 399 Skinning with OpenGL ES 2.0 401 Skinning with OpenGL ES 1.1 403 Generating Weights and Indices 406 Watch Out for Pinching 407 Further Reading 407 Appendix. C++ Vector Library 409 Disclaimer Regarding Performance 409 Vector.hpp 410 Matrix.hpp 414 Quaternion.hpp 419 Index 423 ISBN-13:,9780596804824 What does it take to build an iPhone app with stunning 3D graphics? This book will show you how to apply OpenGL graphics programming techniques to any device running the iPhone OS -- including the iPad and iPod Touch -- with no iPhone development or 3D graphics experience required. iPhone 3D Programming provides clear step-by-step instructions, as well as lots of practical advice, for using the iPhone SDK and OpenGL. You'll build several graphics programs -- progressing from simple to more complex examples -- that focus on lighting, textures, blending, augmented reality, optimization for performance and speed, and much more. All you need to get started is a solid understanding of C++ and a great idea for an app.