When I started iPhone development, it didn’t take very long to realize that if you are doing any kind of complex drawing, and by complex I mean more than a dozen shapes moving around at the same time, you need to dive into OpenGL.
The problem is, the iPhone uses OpenGL ES, version 1.1. And the best place to learn about that is… um… nowhere really. A few people have posted a tutorial here and there. The Apple developer site has minimal data. The Khronos site has a nice reference document, but it’s just that, a reference. Doesn’t really teach you how to get started so much. Just, “this function is for this, this one is for that, etc.” Invaluable, but not useful for noobs like me.
Even if you can find a decent bit of material on OpenGL ES 1.1, it’s not geared towards the iPhone specifically.
So, what to do? What to do?
So, there are various flavors of GL, and various implementations. I’m not going to find anything iPhone specific, so I figured I’d jump out one level and just learn OpenGL. Then, when I actually understand OpenGL itself, it shouldn’t be too hard to adapt what I know to the iPhone. And anyway, I might find it useful in other spheres.
So where to start? Well, following multiple advices from all over, I got myself a copy of “the Red Book” on Amazon, used for something like $4 + shipping. The Red Book is what we OpenGL gurus call The OpenGL Programming Guide:
I think it’s important that you do buy a used, older version if you’re learning OpenGL with the eventual aim of using it on the iPhone. The newest version is for OpenGL 2.0, which has quite a few changes. The version I wound up with is for 1.4.
So, I got the book today and this evening cracked it open and started digging through it. I actually got all the way through chapter 1 and was even able to compile and run all the examples! I’m so proud. But I thought I’d share a few tips in case you try the same.
The first thing I did was go to Apple’s dev site and figure out how to set up an OpenGL project in Cocoa. I got this working, but if your intention is to follow along with the book, you don’t want to do this. Instead, you want to create a command line C project. If you do this, you can follow along with the code in the book, line for line, and it works great. At least through Chapter 1.
So, in XCode: New Project, Mac OS X, Command Line Utility, Standard Tool.
This gives you a straight up C program that Kernighan and Ritchie would be proud of. Even sets up a main function that prints out “Hello world”. Yay.
Now you need to add in OpenGL and GLUT. Glut is the GL utility library that the book makes use of. Turns out that GL has very limited functionality – it really restricts itself to the act of rendering the 3D scene. If you want to create a window, animate, load models, use primitives, etc. you either code all that yourself, or use a library like GLUT.
So in your Groups and Files Panel on the left, right click on the folder that contains all the project files and choose Add, Existing Frameworks… This should open a file dialog that points to your frameworks directory. Go into that and select the OpenGL and GLUT frameworks. This will add them to your project. If you want, you can make a frameworks group (folder) in the panel to consolidate them.
Now your #includes. These vary system to system, so the book doesn’t include the #includes for each program listing. What I’ve found that works is:
[c]#include <stdio.h>
#include <openGL/OpenGL.h>
#include <glut/GLUT.h>[/c]
After that, you are good to go. I typed the programs from Chapter 1, line for line with this setup, and they ran perfectly. Hope this helps others on the same path as me.
On to Chapter 2!
Oh, if you’ve actually made it this far, I’ll let you in on a little secret. You can actually read the Red Book on line for free. I like having real books on hand. But if you can’t wait go get started, dig around and you’ll find it rather easily. I’m not talking about some torrenting it either. It’s available free and legally. Again, I think you want to look for an older version, not 2.0. But I’m not entirely sure how important that is.