I’ve been a software developer for a long time now, over twenty years, but until the iPhone I had never seriously played with 3D graphics programming. As I usually do when I start trying to learn something new, I bought a few books and started searching the web for examples. After just a few hours I could tell this was going to be difficult.
Not necessarily because OpenGL ES itself is terribly complex, although parts of it certainly are, but more because most of the examples I could find were for OpenGL ES 1.1, and I wanted to learn 2.0. In OpenGL ES 1.1, many of the graphics manipulations were taken care of for you by setting values in certain functions before asking OpenGL to render your stuff. In OpenGL ES 2.0, those functions no longer exist, and the programmer is expected to figure out the math on their own.
The books I had bought for developing games on the iPhone all dealt with 1.1 as well, so I could only ‘think’ in OpenGL ES 1.1, and the ES 2.0 example generated by Xcode was a little too ‘bare-boned’ to be very helpful. So I started scrounging around and trying all kinds of strange things in the code, and even had to learn how to perform matrix math on my own.
So many of the OpenGL tutorials available on the web contain the caveat “you’ll need to know how to multiply matrices, but I don’t have time to get into that here.” Unfortunately, OpenGL ES 2.0 is all about the matrices, and trying to write a proper OpenGL ES 2.0 tutorial without covering the matrix math in detail is like trying to trying to teach someone to play solitaire with only one suit of cards – you can sort of see how it would work, but you won’t be able to do it yourself without that critical missing piece.
As my OpenGL ES 2.0 ‘Hello World’ program, I had simply wanted to write a small program to display a cube that the user could rotate. It didn’t seem like it would be too difficult to do, but for someone coming into OpenGL ES 2.0 on the iPhone as fresh as I was, it was quite a mental workout. Now that I’ve finally got it working, I thought I’d share what I’ve learned with two goals in mind: firstly, I know how difficult it is to find solid, complete OpenGL ES 2.0 examples for the iPhone out there, and secondly, I know I need to learn more, and may have mistakes in this tutorial, which I hope the more knowledgable readers will help me correct.
Throughout this tutorial, I will be working in the OpenGL ES Application template as generated by Xcode 4.0. We will start with a fresh project as generated by Xcode, then modify the code in steps until we have completed my version of ‘Hello World’, with each step introducing another piece of the puzzle needed to complete the code.