www.it-ebooks.info ffirs.indd iffirs.indd i 12/30/11 12:05:23 PM12/30/11 12:05:23 PM www.it-ebooks.info GAME AND GRAPHICS PROGRAMMING FOR IOS AND ANDROID ® WITH OPENGL ® ES 2.0 INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 CHAPTER 2 Setting Up Your Graphic Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 CHAPTER 3 Dealing with Complex Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 CHAPTER 4 Building a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 5 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 CHAPTER 6 Real-Time Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 CHAPTER 7 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 CHAPTER 8 Pathfi nding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 CHAPTER 9 Audio and Other Cool Game Programming Stu . . . . . . . . . . . . . . . . . . . 171 CHAPTER 10 Advanced Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 CHAPTER 11 Advanced FX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 CHAPTER 12 Skeletal Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ffirs.indd iffirs.indd i 1/11/12 8:20:58 AM1/11/12 8:20:58 AM www.it-ebooks.info ffirs.indd iiffirs.indd ii 1/11/12 8:20:59 AM1/11/12 8:20:59 AM www.it-ebooks.info Game and Graphics Programming for iOS and Android ® with OpenGL ® ES 2.0 ffirs.indd iiiffirs.indd iii 1/11/12 8:21:00 AM1/11/12 8:21:00 AM www.it-ebooks.info ffirs.indd ivffirs.indd iv 1/11/12 8:21:00 AM1/11/12 8:21:00 AM www.it-ebooks.info Game and Graphics Programming for iOS and Android ® with OpenGL ® ES 2.0 Romain Marucchi-Foino John Wiley & Sons, Inc. ffirs.indd vffirs.indd v 1/11/12 8:21:00 AM1/11/12 8:21:00 AM www.it-ebooks.info Game and Graphics Programming for iOS and Android® with OpenGL® ES 2.0 This edition fi rst published 2012 Copyright ©2012 Romain Marucchi-Foino Registered offi ce John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom For details of our global editorial offi ces, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our web site at www.wiley.com. The right of the author to be identifi ed as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. The publisher is not associated with any product or vendor mentioned in this book. This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold on the understanding that the publisher is not engaged in rendering professional services. If professional advice or other expert assistance is required, the services of a competent professional should be sought. 978-1-119-97591-5 978-1-119-97626-4 (ebk) 978-1-119-97627-1 (ebk) 978-1-119-97628-8 (ebk) A catalogue record for this book is available from the British Library. ffirs.indd viffirs.indd vi 1/11/12 8:21:02 AM1/11/12 8:21:02 AM www.it-ebooks.info ABOUT THE AUTHOR ROMAIN MARUCCHI-FOINO is the original author and founder of the popular mobile game engine SIO2 ( http://sio2interactive.com). Formerly a game engine developer, Romain has dedicated his efforts to build a state-of-the-art game engine for mobile devices using OpenGL ES since the arrival of the iPhone. He is currently working as the lead 3D programmer for sio2interactive .com , the offi cial developer of the SIO2 Engine, which powers thousands of games and 3D applications throughout the App Store and the Android market. His experience in the mobile game industry has led him to contribute his work to many online communities, publications, and blogs. ABOUT THE TECHNICAL EDITOR EFFIE C. LEE is a self-employed game and graphics designer who has been working in the game industry for the last 4 years. With a bachelor’s degree in Computer Science and a real passion for electronic games and computer graphics, she has been involved in multiple mobile game productions. With her wide range of knowledge in the game development process, she has been professionally working as a 2D and 3D graphics designer, game programmer (scripting), and web designer for game sites, as well as managing game quality assurance and localization. If you would like to reach her, send an email to effiecl@gmail.com. ffirs.indd viiffirs.indd vii 1/11/12 8:21:02 AM1/11/12 8:21:02 AM www.it-ebooks.info ffirs.indd viiiffirs.indd viii 1/11/12 8:21:02 AM1/11/12 8:21:02 AM www.it-ebooks.info [...]... 12/31/11 9:40:22 AM INTRODUCTION WELCOME TO Game and Graphics Programming for iOS and Android with OpenGL ES 2.0 This book is not your usual OpenGL Hello Triangle” book — it’s not meant to explain the “why” (Google is there for that), but rather, to show you the “how.” This book will strictly teach you what works and what doesn’t when it comes to game and graphics programming Using more than 50 unique tutorials... Touch and iPad simulator, which you can use to develop and test your application And it is fully compatible with everything contained in this book If you are planning to develop for Android, what you need is a Mac or a PC with an operating system that is supported by the Android SDK (for more information, visit http://developer .android com) Also, you will need an Android device with OpenGL ES 2.0 support,... can post new messages and respond to messages other users post You can read messages at any time on the Web If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software... an official iOS Developer Certification from Apple, you can still make full use of this book For Android Developers To set up your environment for Android, it is unfortunately not as easy as for iOS First go to http://developer .android. com/sdk/installing.html and follow the instructions to install the Android SDK, Eclipse, and the ADT plug-in Please note that the Android SDK version used for this book... location of the Android NDK www.it-ebooks.info c01.indd 3 12/31/11 8:53:33 AM 4 ❘ CHAPTER 1 GETTING STARTED Congratulations — your Android development environment is now all set! However, please note that in order to use this book with Android you will need an actual device with OpenGL ES 2.0 support The emulator provided by the Android SDK supports only OpenGL ES 1.x, not OpenGL ES 2.0 So local deployment... how to work with this book’s template project and walk you through three progressive exercises In these exercises, you will learn how to manipulate the most common types of graphic projections and draw simple geometry onscreen; handle vertex and fragment shaders and link them to a shader program; manipulate vertex attributes and uniform variables; translate, rotate, and scale basic geometry; and create... while programming games or 3D applications There is plenty enough content in here to get you started with real game and graphics programming in no time! WHAT YOU NEED TO USE THIS BOOK If you are planning to develop for iOS, all you need is a Mac that can support the latest version of the iOS SDK (for more information, visit http://developer.apple.com) An iDevice is optional since the iOS SDK provides... book’s errata page and fi x the problem in subsequent editions of the book P2P.WROX.COM For author and peer discussion, join the P2P forums at p2p.wrox.com The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users The forums offer a subscription feature to e-mail you topics of interest of your choosing... dream game, this book is for you! WHO THIS BOOK IS FOR Be aware that this is not a beginner book It is an intermediate-level book that assumes you are familiar with linear algebra (matrices, vectors, and quaternions), you have a strong C/C++ programming background, you have at least touched base with OpenGL or OpenGL ES, and you basically know how computer graphics work in general If you have this necessary... nal result of a tutorial, open this directory Inside the directory, you can then fi nd at the root the source fi les used by the tutorial (respectively named templateApp.cpp and templateApp.h) and two directories that contain the project fi les for iOS and Android You can then load the project into your IDE and rebuild it from scratch ➤ common: Contains the free and open source GFX 3D engine (the mini game . for iOS and Android ® with OpenGL ® ES 2. 0 ffirs.indd iiiffirs.indd iii 1/11/ 12 8 :21 :00 AM1/11/ 12 8 :21 :00 AM www.it-ebooks.info ffirs.indd ivffirs.indd iv 1/11/ 12 8 :21 :00 AM1/11/ 12 8 :21 :00 . www.it-ebooks.info ffirs.indd iffirs.indd i 12/ 30/ 11 12: 05 :23 PM 12/ 30/ 11 12: 05 :23 PM www.it-ebooks.info GAME AND GRAPHICS PROGRAMMING FOR IOS AND ANDROID ® WITH OPENGL ® ES 2. 0 INTRODUCTION . . . . . . AM www.it-ebooks.info Game and Graphics Programming for iOS and Android ® with OpenGL ® ES 2. 0 Romain Marucchi-Foino John Wiley & Sons, Inc. ffirs.indd vffirs.indd v 1/11/ 12 8 :21 :00 AM1/11/ 12 8 :21 :00 AM www.it-ebooks.info Game