Your SlideShare is downloading. ×
Game and graphics programming for ios and android with opengl
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Game and graphics programming for ios and android with opengl

2,145
views

Published on

Game and graphics programming for ios and android with opengl, game iphone, game ios, game android,game ios

Game and graphics programming for ios and android with opengl, game iphone, game ios, game android,game ios


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,145
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. www.it-ebooks.info
  • 2. www.it-ebooks.info ffirs.indd i 12/30/11 12:05:23 PM
  • 3. 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 Pathfinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 CHAPTER 9 Audio and Other Cool Game Programming Stuff . . . . . . . . . . . . . . . . . . . 171 CHAPTER 10 Advanced Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 CHAPTER 11 Advanced FX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 CHAPTER 12 Skeletal Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 www.it-ebooks.info ffirs.indd i 1/11/12 8:20:58 AM
  • 4. www.it-ebooks.info ffirs.indd ii 1/11/12 8:20:59 AM
  • 5. Game and Graphics Programming for iOS and Android® with OpenGL® ES 2.0 www.it-ebooks.info ffirs.indd iii 1/11/12 8:21:00 AM
  • 6. www.it-ebooks.info ffirs.indd iv 1/11/12 8:21:00 AM
  • 7. Game and Graphics Programming for iOS and Android® with OpenGL® ES 2.0 Romain Marucchi-Foino John Wiley & Sons, Inc. www.it-ebooks.info ffirs.indd v 1/11/12 8:21:00 AM
  • 8. 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 offices, 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 identified 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. www.it-ebooks.info ffirs.indd vi 1/11/12 8:21:02 AM
  • 9. 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 official 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. www.it-ebooks.info ffirs.indd vii 1/11/12 8:21:02 AM
  • 10. www.it-ebooks.info ffirs.indd viii 1/11/12 8:21:02 AM
  • 11. CREDITS VP CONSUMER AND TECHNOLOGY PUBLISHING DIRECTOR SENIOR PROJECT EDITOR Sara Shlaer Michelle Leete ASSOCIATE DIRECTOR—BOOK CONTENT MANAGEMENT DEVELOPMENT AND COPY EDITOR Kathryn Duggan Martin Tribe TECHNICAL EDITOR ASSOCIATE PUBLISHER Effie C. Lee Chris Webb SENIOR PRODUCTION EDITOR SENIOR ACQUISITIONS EDITOR Chris Katsaropoulos Debra Banninger PROOFREADER ASSISTANT EDITOR Nancy Carrasco Ellie Scott ASSOCIATE MARKETING DIRECTOR INDEXER Louise Breinholt Robert Swanson SENIOR MARKETING EXECUTIVE COVER DESIGNER Kate Parrett LeAndra Young EDITORIAL MANAGER COVER IMAGE Jodi Jensen © iStock / Vladislav Ociacia www.it-ebooks.info ffirs.indd ix 1/11/12 8:21:02 AM
  • 12. www.it-ebooks.info ffirs.indd x 1/11/12 8:21:03 AM
  • 13. CONTENTS INTRODUCTION xvii CHAPTER 1: GETTING STARTED 1 Software Requirements 2 For iOS Developers For Android Developers 2 2 Downloading the Book’s SDK Importing Projects For iOS Developers For Android Developers 4 5 5 5 The Template Summary 7 8 CHAPTER 2: SETTING UP YOUR GRAPHIC PROJECTIONS The Three Basic Types of Projections Orthographic 2D Projection Program and Project Initialization Vertex and Fragment Shader Linking a Shader Program The Drawing Code Orthographic Projection 9 10 11 12 14 17 19 23 Getting Orthographic 23 Perspective Projection Summary 26 27 CHAPTER 3: DEALING WITH COMPLEX GEOMETRY The Wavefront File Format 29 29 Cube.obj Cube.mtl 30 31 Preparing the OBJ Viewer Code Loading an OBJ Building the Shaders The Vertex Shader The Fragment Shader Vertex Buffer Object Storing the Vertex Data 31 32 35 35 36 36 37 www.it-ebooks.info ftoc.indd xi 12/30/11 8:28:31 AM
  • 14. CONTENTS Building the Vertex Data Array VBO Building the Element Array VBO Building the VAO Rendering Momo Handling Touche Per-Vertex Lighting 38 39 40 42 44 46 Vertex Shader Light Calculation Modifying the Fragment Shader More Uniforms Making Momo Furrier 46 47 48 50 Loading the Texture Adjusting the Vertex Data Adding UV Support to the Vertex Shader Adding Texture Support to Your Fragment Shader Binding the Texture Summary 50 51 52 53 53 54 CHAPTER 4: BUILDING A SCENE Handling Multiple Objects The Code Structure Loading and Drawing the Scene The Shaders Code The Different Object Types The Drawing Sequence Fixing the Scene Uber Shader Using Your Uber Shader Render Loop Objects Categorization Double-Sided Per-Pixel Lighting 57 58 58 59 63 64 64 65 65 66 69 71 73 Making the Vertex Shader Even Fatter Getting the Fragment Shader More Uber Wrapping Up the Implementation Summary 73 74 76 79 CHAPTER 5: OPTIMIZATION The Base App Triangles to Triangle Strips Building Triangle Strips Texture Optimization Adding 16-Bit Texture Conversion 81 82 82 83 84 85 xii www.it-ebooks.info ftoc.indd xii 12/30/11 8:28:32 AM
  • 15. CONTENTS PVR Texture Compression Faking Details 86 87 Bump Mapping Implementation Precision Qualifiers Optimization The Normal Map Lighting Calculation Adding Specularity 87 88 90 91 Geometry and Shaders LOD Texture Atlas Managing States in Software Automatic Shader Optimization Summary 92 93 94 94 95 CHAPTER 6: REAL-TIME PHYSICS 97 Types of Physical Objects Physics Shapes Using Bullet Hello Physics Collision Callbacks, Triggers, and Contacts Contact-Added Callback Near Callback Contact Points 98 98 100 100 105 105 107 108 2D Physics 110 More Shapes! Building the Physical Objects Camera Tracking User Interactions The Game Logic 3D Physics 110 113 114 116 117 120 The Bullet File Format 3D Pinball Game 120 122 Summary 127 CHAPTER 7: CAMERA 129 Touch and Go! The Camera Frustum 130 132 How to Build the Frustum Frustum Clipping Implementation More Clipping Functions Camera Fly Mode First-Person Camera with Collision Detection 3D Camera Tracking 133 134 135 136 140 143 xiii www.it-ebooks.info ftoc.indd xiii 12/30/11 8:28:33 AM
  • 16. CONTENTS Third-Person Camera with Collision Summary CHAPTER 8: PATHFINDING Recast and Detour Navigation Creating the Navigation Mesh 3D Physics Picking Player’s Auto Drive Visualizing the Way Points Catch Me If You Can! Know Your Enemy Game State Logic Summary CHAPTER 9: AUDIO AND OTHER COOL GAME PROGRAMMING STUFF OpenAL OGG Vorbis Hello World OpenAL Style Initializing OpenAL Static In-Memory Sound Playback Positional Sound Source Piano Game Loading a Static and Streamed Sound Color Picking Piano Game Logic Final Adjustments Rolling Ball Game 145 149 151 151 152 153 155 159 161 163 165 167 170 171 172 173 174 174 175 176 178 178 182 185 188 190 GFX Shaders Linking the Positional Sound Sources Accelerometer-Driven Camera Cheap FX Game Logic and Tweaks Summary 190 191 195 198 200 205 CHAPTER 10: ADVANCED LIGHTING Types of Lamps Let There Be Light 207 208 208 Directional Lamp Shader Struct as Uniforms 211 214 xiv www.it-ebooks.info ftoc.indd xiv 12/30/11 8:28:33 AM
  • 17. CONTENTS Point Light 217 Point Light Shader Code Light Attenuation Point Light with Attenuation Code The Attenuation Uniforms Spherical Point Light Tweaking the Point Light Code Spot Light Spot Light Shader Code Multiple Lights Making the Shader Program Dynamic Summary 218 221 222 223 224 225 227 229 231 233 234 CHAPTER 11: ADVANCED FX 237 Render to Texture Post-Processing Effects 238 238 First Rendering Pass Second Pass Fullscreen Pass and Blur Shader Projected Texture Projector Shader Projected Real-Time Shadows Casting Shadows Using the Depth Texture A Few More Words about the Frame Buffer Object Particles Summary CHAPTER 12: SKELETAL ANIMATION Traditional vs. Modern Animation Systems The MD5 File Format Loading an MD5 Mesh Animating the Mesh LERP SLERP 241 242 243 246 249 250 253 254 255 257 259 260 261 261 264 266 267 Blending Animation Additive Blending Summary 267 269 271 INDEX 273 xv www.it-ebooks.info ftoc.indd xv 12/30/11 8:28:33 AM
  • 18. www.it-ebooks.info flast.indd xvi 12/31/11 9:40:22 AM
  • 19. 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 (which also include some full game frameworks), this book adopts a straightforward practical approach (probably never seen before) that only focuses on what you need to learn to insure that you will be able to create a game. You will learn all the necessary elements in order to create a full-fledged game with rich 3D graphics. If you are looking for an aggressive teaching method that enables you to quickly move forward to create your 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 knowledge, and want to make lightning-fast progress in game and graphics programming, then you have found the right publication. This book is written for people who want to learn the hardcore stuff fast in order to be able to create and push a full-fledged game on the App Store and the Android Market. WHAT THIS BOOK COVERS Basically everything you need to know in order to create a full game is included in this book. You will learn about all aspects of creating a game, such as loading 3D geometries and textures; how to handle materials, shaders, sounds, cameras, clipping, physics, AI, pathfi nding, skeletal animations; and a lot more. By the end of this book you will be able to apply the knowledge you’ve learned and combine the different tutorials you’ve completed to create your own state-of-the-art game. HOW THIS BOOK IS STRUCTURED This book is structured in such a way that pretty much all chapters depend on each other. Each chapter shows you progressively how to master the necessary techniques to be able to handle the next chapter. www.it-ebooks.info flast.indd xvii 12/31/11 9:40:23 AM
  • 20. INTRODUCTION Here is a list of all the chapters and what will be covered in each of them: ➤ Chapter 1, “Getting Started” — You will learn how to set up your development environment, download this book’s SDK, import and re-compile the tutorials, and deal with the template project that you will be using throughout this publication. ➤ Chapter 2, “Setting Up Your Graphic Projections” — Since you have a running template, I will show you how to set up the necessary projection matrix to be able to handle 2D, 2.5D, or 3D. You will also learn how to draw simple geometry onscreen and handle a camera matrix. ➤ Chapter 3, “Dealing with Complex Geometry” — You will create a Wavefront OBJ viewer that will allow you to learn how to load complex geometry from disk. You will also learn how to load and create textures, deal with basic lighting, and respond to touch events. ➤ Chapter 4, “Building a Scene” — This chapter will extend the knowledge that you’ve learned in Chapter 3 and will explain how to handle a more complex scene. You will learn about drawing sequences and how to create reusable shaders. ➤ Chapter 5, “Optimization” — In this chapter, I will show you techniques that will allow you to optimize the performance of your drawing. You will touch base with texture compression and shader optimization, and learn how to convert triangles to triangle strips as well as other tips and tricks to get better FPS. ➤ Chapter 6, “Real-Time Physics” — Since you will know by now how to handle a scene properly, this chapter is about adding real-time physics behaviors to your scene using Bullet. I will show you how to create a physical world and add physical entities to it. You will then learn how to handle in code different techniques that will allow you to add logic upon collision callbacks or based on the contact points between two or more physical entities. ➤ Chapter 7, “Camera” — This chapter will focus entirely on cameras. You will learn to build frustum planes and will be able to determine the visibility of each object of your scene in the field of view of a camera. I will then show you how to implement multiple types of cameras, including a full-fledged fi rst- and third-person camera with collision, ready to be used in your own apps. ➤ Chapter 8, “Pathfinding” — Artificial intelligence (AI) and pathfinding will play an important role inside your games, and that’s what this chapter is all about. You will learn how to use the Recast and Detour libraries to build a navigation mesh and have entities moving automatically in the scene. In this chapter, I will also demonstrate how to use True Type Font to generate a font texture and draw dynamic text onscreen. ➤ Chapter 9, “Audio and Other Cool Game Programming Stuff” — This one is all about audio using OpenAL. In this chapter, you will learn how to load OGG Vorbis sound fi les and either stream them from memory in real time or statically store them in audio memory. I will also introduce how to create 3D positional and ambient sound sources and will touch base on how to use the accelerometer, along with how to animate textures and create other miscellaneous effects. xviii www.it-ebooks.info flast.indd xviii 12/31/11 9:40:23 AM