Touch and Go! 130
The Camera Frustum 132
How to Build the Frustum 133
Frustum Clipping Implementation 134
More Clipping Functions 135
Camera Fly Mode 136
First-Person Camera with Collision Detection 140
3D Camera Tracking 143
ftoc.indd xiii
ftoc.indd xiii 12/30/11 8:28:33 AM12/30/11 8:28:33 AM
www.it-ebooks.info
xiv CONTENTS
Third-Person Camera with Collision 145 Summary 149
CHAPTER 8: PATHFINDING 151
Recast and Detour 151
Navigation 152 Creating the Navigation Mesh 153
3D Physics Picking 155
Player’s Auto Drive 159
Visualizing the Way Points 161
Catch Me If You Can! 163
Know Your Enemy 165
Game State Logic 167
Summary 170
CHAPTER 9: AUDIO AND OTHER COOL GAME
PROGRAMMING STUFF 171
OpenAL 172
OGG Vorbis 173
Hello World OpenAL Style 174
Initializing OpenAL 174
Static In-Memory Sound Playback 175
Positional Sound Source 176
Piano Game 178
Loading a Static and Streamed Sound 178
Color Picking 182
Piano Game Logic 185
Final Adjustments 188
Rolling Ball Game 190
GFX Shaders 190
Linking the Positional Sound Sources 191
Accelerometer-Driven Camera 195
Cheap FX 198
Game Logic and Tweaks 200
Summary 205
CHAPTER 10: ADVANCED LIGHTING 207
Types of Lamps 208
Let There Be Light 208
Directional Lamp Shader 211
Struct as Uniforms 214
ftoc.indd xiv
ftoc.indd xiv 12/30/11 8:28:33 AM12/30/11 8:28:33 AM
www.it-ebooks.info
xv CONTENTS
Point Light 217
Point Light Shader Code 218
Light Attenuation 221
Point Light with Attenuation Code 222
The Attenuation Uniforms 223
Spherical Point Light 224
Tweaking the Point Light Code 225
Spot Light 227
Spot Light Shader Code 229
Multiple Lights 231
Making the Shader Program Dynamic 233 Summary 234
CHAPTER 11: ADVANCED FX 237
Render to Texture 238
Post-Processing Eff ects 238
First Rendering Pass 241
Second Pass 242
Fullscreen Pass and Blur Shader 243
Projected Texture 246
Projector Shader 249
Projected Real-Time Shadows 250 Casting Shadows Using the Depth Texture 253 A Few More Words about the Frame Buff er Object 254 Particles 255 Summary 257
CHAPTER 12: SKELETAL ANIMATION 259
Traditional vs. Modern Animation Systems 260
The MD5 File Format 261
Loading an MD5 Mesh 261
Animating the Mesh 264
LERP 266 SLERP 267
Blending Animation 267
Additive Blending 269
Summary 271 INDEX 273
ftoc.indd xv
ftoc.indd xv 12/30/11 8:28:33 AM12/30/11 8:28:33 AM
www.it-ebooks.info
flast.indd xvi
flast.indd xvi 12/31/11 9:40:22 AM12/31/11 9:40:22 AM
www.it-ebooks.info
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-fl edged 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-fl edged 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.
flast.indd xvii
flast.indd xvii 12/31/11 9:40:23 AM12/31/11 9:40:23 AM
www.it-ebooks.info
xviii
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 fi eld of view of a camera. I will then show you how to implement multiple types of cameras, including a full-fl edged fi rst- and third-person camera with collision, ready to be used in your own apps.
Chapter 8, “Pathfi nding” — Artifi cial intelligence (AI) and pathfi nding 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.
INTRODUCTION
xix Chapter 10, “Advanced Lighting” — This chapter will teach you how to apply dynamic lighting, probably one of the hardest things to master in game and graphics programming.
You will create multiple types of lights from directional to spot lights and will learn how to handle them in real time.
Chapter 11, “Advanced FX” — This chapter is all about special effects. You will learn how to create fullscreen post-processing effects, project textures, and real-time shadows, as well as how to handle particles.
Chapter 12, “Skeletal Animation” — Last but not least, you will learn how to handle skeletal animation using the MD5 fi le format. I will teach you how to load and draw a mesh attached to a skeleton. You will then load action fi les and learn how to mix them using different types of blending methods.
You will fi nd that this book will not simply show you the theory, but it will also show you how to apply the knowledge that you gain in each chapter to real game scenarios.
As you can see, this book is packed full of useful knowledge that you will need on a daily basis 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 out-of-the-box an iPhone/iPod 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, because the simulator bundled with the Android SDK only supports OpenGL ES 1.0.
In addition, this book uses Blender as its 3D modeling software (because it is free and open source).
So to be able to test, tweak, and re-export all the test scenes used in the book’s SDK, go grab a copy at http://blender.org.
CONVENTIONS
To help you get the most from the text and keep track of what’s happening, the following conventions are used throughout this book:
New terms and important words are in italics.
File names, URLs, variables, and code within text are shown like this: templateApp.cpp.
➤
➤
➤
➤
➤
flast.indd xix
flast.indd xix 12/31/11 9:40:24 AM12/31/11 9:40:24 AM
www.it-ebooks.info
xx
INTRODUCTION
Code blocks are shown like this:
#include “templateApp.h”
TEMPLATEAPP templateApp = { templateAppInit, templateAppDraw };
SDK SOURCE CODE
The offi cial SDK used in this book is available at www.wrox.com for download (packaged as a .zip fi le). The SDK contains the fi nal end results of all the tutorials covered in this publication. It also includes the full source code of the SDK and all the original assets used in the tutorials, so you will have access to the 2D/3D scenes and can recompile them from scratch freely.
➤
Because many books have similar titles, you may fi nd it easiest to search by ISBN;
this book’s ISBN is 978-1-119-97591-5.
In addition, I also personally maintain the book’s SDK (using GIT version control system) on the offi cial website of the GFX 3D Engine (the free and open source mini 3D game and graphics engine that you’ll be using in this book, featured in Figure 1), which is available at the following address: http://gfx.sio2interactive.com. The latest SDK revisions of the book along with all quick bug fi xes can be found at the GFX 3D Engine site instantly since it’s easier for me to just update the source code using version control.
It might take a bit more time for the offi cial SDK at www.wrox.com to be updated since this offi cial
SDK version is carefully maintained by the publisher, but you could just be patient and wait for the offi cial release. It’s up to you.
It is also worth mentioning that on the GFX 3D Engine site (http://gfx.sio2interactive.com), you can fi nd support forums for this book’s SDK as well as the latest version of the GFX 3D engine.
This website also provides other 3D game and graphics-related demos, tutorials, and other materials that are fully compatible with this book’s SDK.
FIGURE 1: GFX 3D Engine
Models and textures generously provided by Ken Beyer (http://www.katsbits.com) and David Radford (http://dmradford.com).
flast.indd xx
flast.indd xx 12/31/11 9:40:24 AM12/31/11 9:40:24 AM
www.it-ebooks.info
INTRODUCTION
xxi
ERRATA
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you fi nd an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.
To fi nd the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/
booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/
techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the 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 when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will fi nd a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1.
Go to p2p.wrox.com and click the Register link.2.
Read the terms of use and click Agree.3.
Complete the required information to join as well as any optional information you wish to provide and click Submit.4.
You will receive an e-mail with information describing how to verify your account and complete the joining process.You can read messages in the forums without joining P2P but in order to post your own messages, you must join.
flast.indd xxi
flast.indd xxi 12/31/11 9:40:44 AM12/31/11 9:40:44 AM
www.it-ebooks.info
xxii
INTRODUCTION
Once you join, you 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 works as well as many common questions specifi c to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
flast.indd xxii
flast.indd xxii 12/31/11 9:40:50 AM12/31/11 9:40:50 AM
www.it-ebooks.info
Getting Started
WHAT’S IN THIS CHAPTER?
Learning about the software used in this book Downloading the book’s SDK
Understanding the SDK architecture Importing projects into your IDE
Understanding this book’s template application Learning how to work with the template code structure
In this chapter, you will fi rst start by setting up your development environment to be able to work with this book’s tutorials and examples.
You will then receive a quick introduction about this book’s SDK and where to download it, and learn about the different directories it contains. Then you will learn how to import this book’s existing SDK projects and templates into your favorite IDE, as you will do throughout this book when following the different tutorials.
Moving on to the last section of this chapter, you will learn about this book’s cross-platform template project. And fi nally, this chapter concludes with a quick tutorial that will help you to get familiar with the events of the template, as well as with the tone that will be used for all the tutorials in this book.
➤
➤
➤
➤
➤
➤
1
c01.indd 1
c01.indd 1 12/31/11 8:53:28 AM12/31/11 8:53:28 AM
www.it-ebooks.info
2 ❘ CHAPTER 1 GETTING STARTED
SOFTWARE REQUIREMENTS
This book’s content is built to run on iOS 5.x+ as well as for Android 2.x+, the latest and most stable versions of these two mobile operating systems at the time this book was written.
For iOS Developers
To use this book for iOS, all you have to do is to grab a copy of the latest iOS SDK available at http://developer.apple.com, and install it on your Mac.
Out-of-the-box the iOS SDK provides a simulator with full GLES v2 support, so even if you do not have an iOS device, or do not have an offi cial iOS Developer Certifi cation 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 was v2.3.4, but later versions should also work as well.
All the code in this book uses C/C++, which means that you will have to install Android Native Code support. To fi nalize the installation of your development environment, follow these steps: