OpenGL ES 2 for Android
A Quick-Start Guide
by Kevin Brothaler
Version: P1.0 (June 2013)
Copyright 2013 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
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 The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
The Android robot is reproduced from work created and shared by Google and is used according to terms described in the Creative Commons 3.0 Attribution License (http://creativecommons.org/licenses/by/3.0/us/legalcode).
The unit circle image in ).
Day skybox and night skybox courtesy of Jockum Skoglund a.k.a. hipshot, hipshot@zfight.com, http://www.zfight.com.
The image of the trace capture button is created and shared by the Android Open Source Project and is used according to terms described in the Creative Commons 2.5 Attribution License.
To Anne and my Oma
You have brought so much joy and wonder into my life. Thank you for making it all possible.
Table of Contents
Compiling Shaders and Drawing to the Screen |
Adding Touch Feedback: Interacting with Our Air Hockey Game |
Copyright 2013, The Pragmatic Bookshelf.
Early praise for OpenGL ES 2.0 for Android
Kevins book is stuffed full of useful code and explanations, to the point of being inspirational. The math/matrix stuff is the best I have ever read on the topic. If you already know Java well and want to break into 3D graphics, this book is perfect.
John Horton |
HadronWebDesign.com and author of the Android math league app Math Legends |
Cuts out the academic fluff and teaches you what you need to become productive quickly. I wish I had access to such a clear, concise, and humorous book when I first learned OpenGL.
Mark F. Guerra |
Developer, GLWallpaperService library for OpenGL |
An amazing introduction to Android graphics programming, covering all the topics that give headaches to OpenGL beginners with such extensive detail that youll be perfectly prepared to implement your own ideas.
Carsten Haubold |
Maintainer, NeHe OpenGL tutorials |
I wish I had this book when I first started programming Android games. Its well written and up-to-date.
Owen Alanzo Hogarth |
President, Team Blubee, Inc. |
I am greatly impressed by this book and would gladly recommend it to any programming enthusiast or anyone whos ever dreamed of making a game.
Foreword
Games are visual experiences. As game developers, we want to create environments and characters that pull players into our games, be it through stunningly realistic 3D scenery or quirky, out-of-this-world experiences. We are longing for millions to play our games and experience our worlds, making their lives a tiny bit more fun. Android and its ecosystem provide us with an audience. OpenGL ES gives us the technological means to realize the games of our dreams.
OpenGL ES is the de facto standard in mobile graphics programming. Its the lean and mean brother of desktop OpenGL, removing a lot of the cruft kept for backward compatibility. OpenGL ES comes in three major versions: version 1.0, which gave us an inflexible fixed-function pipeline; version 2.0, which introduced a programmable pipeline with all the bells and whistles we can ask for; and finally, the very young and not-yet-widely-available version 3.0, which adds new features on top of the 2.0 standard.
While OpenGL ES has been in use for almost a decade, hands-on material for beginners is hard to come by, especially for version 2.0, which introduced shaders, an esoteric topic for newcomers. In addition to device- and platform-specific issues, this lack of material sets the entry barrier rather high.
This is where Kevins book comes in. He cuts through all the boilerplate talk and takes you on a pragmatic tour of OpenGL ES 2.0. Not only does Kevin cover the fundamental concepts behind 3D graphics, but he also documents all the Android-specific pitfalls you run into. Even experienced developers will find a few nuggets here. To paraphrase: Its dangerous to go it alone, so read this book!
Mario Zechner
Creator of libgdx,
Footnotes
https://code.google.com/p/libgdx/ |
Copyright 2013, The Pragmatic Bookshelf.
Acknowledgments
I am so grateful to the wonderful team over at The Pragmatic Programmers for giving me the chance to write this book and for doing such a great job in helping me bring it to completion. When I first started out, I wasnt quite sure what to expect, but they did an excellent job of teaching me the ropes. I owe a special debt of gratitude to my editor, Susannah Pfalzer, for guiding me so expertly through the process, and to Aron Hsiao for skillfully coaching a new and bewildered author to the Pragmatic writing style.
I am also thankful for all of my technical reviewers and for everyone else who provided invaluable feedback, including (in no particular order) Mario Zechner, Owen Alanzo Hogarth, Sam Rose, Mike Riley, Aaron Kalair, Rene van der Lende, John Horton, Ed Burnette, Mark Guerra, Maik Schmidt, Kevin Gisi, Brian Schau, Marius Marinescu, Stephen Wolff, Haress Das, Bill Yee, Chad Dumler-Montplaisir, Tibor Simic, Michael Hunter, Jonathan Mischo, and Stefan Turalski, as well as everyone who reported errata or sent in their feedback. Your feedback and advice helped to greatly improve the book, and your encouragement is much appreciated.
I couldnt have done it without the support of the greater community and the generosity of those willing to share their knowledge, including my wonderful readers over at Learn OpenGL ES, the guys at the Khronos Group, NeHe Productions, the Android team, John Carmack, and all of the other giants whom I may have missed and who are too numerous to mention. I stand on their shoulders, and this work wouldnt be possible without them.
Perhaps my greatest debt of gratitude goes to the two women in my life who have kept me grounded all this time: my Oma, for showing me that a little bit of toughness can go a long way, and Anne, my fiance, for letting me spend so many hours tucked away in my home office and for being so encouraging from beginning to end.