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
#include
#include
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.
I’ve put off reading this book in favor of other things for a long time, but I’ve still been meaning to pick it up at some point. It would be great if you continue to blog on how this goes (studying from this book, learning OpenGL in general, applying OpenGL 1.4 concepts to iPhone’s OpenGL ES implementation, etc).
Yes, I’m sure you’ll continue to hear from me as long as I am still pursuing it. So far I’m finding the book to be far more accessible than I expected it to be. Actually, the first part of Chapter is the roughest part so far. The authors try to give you a quick overview, and admit that it is like drinking water from a fire hose. So don’t fret that part too much. They slow down and take things at a much better pace after that.
I really appreciate the work and exploration you’re doing in these areas.
But as some interested in doing more “creative coding” I can’t help but feel we’re going backwards a bit. Granted it’s a new platform and these things need to be worked out before higher level libraries and tools are developed, but this does not seem like a very expressive coding environment.
Marcus, I hear what you are saying and think about the same things myself. However, I believe that OpenGL has some pretty major expressive possibilities, and I would love to see how that might be brought to something like the iPhone.
nothing about flash anymore now ?
The red book is a must have but as you say the info comes thick and fast. I recommend this one for dealing with OpenGL ES (very recently released I got it on Safari RoughCuts but it’s now out for sale and covers 1.1 too):
OpenGL ES 2.0 Programming Guide – Addison Wesley Professional
Given the difference between OpenGL, OpenGL ES and the iPhone’s implementation I just found this book came closer and required less modifications to the example code. e.g. the following OpenGL ES methods that draw an array of vertexes to screen:
glVertexAttribPointer(…)
glEnableVertexAttribArray(…)
glDrawArrays(…)
This is different from how OpenGL (and the red book) first introduces drawing vertices using loops of glVertex3fv(…) which are sandwiched between glBegin() and glEnd() calls (which are not supported by ES, along with immediate mode and display lists AFAIK). Felt like so much work to get anything done, may check out http://www.oolongengine.com/ seems he’s made good progress even includes the Bullet physics lib.
Thanks for sharing this Keith! I’ve been slowly picking up iPhone development and, while I was excited about making applications and games for the iphone, didn’t realise the enormity of the amount to actually learn…….. Objective-C, C, OpenGL *faint*
I’ve found this book online for £0.44 and getting it shipped now, fingers crossed this will be a light in the metaphorical darkness that is cocoatouch and opengl es 1.1 in cocoa touch!
|-| <– I’m about that far from going back to AS3 🙂
OpenGL is fantastic. It’s very easy to pick up and once you learn the basic quirks it’s pretty straight-forward to do the basics.
State machine programming can be a lot of fun. Good luck! 🙂
Marcus, I too know what you mean, when I was figuring it out it felt incredibly alien to me. But the sheer power of using hardware accelerated graphic doodads is breathtaking and will make you that much more expressive. Just wait till you translate your simple particle engine and switch on ‘additive blending’. You’ll be feeling like Robert Hodgin, chest all puffed out. It’s pretty tough though, and I’m really beginning to appreciate just how awesome videogame graphics programmers are.
It’s good to see you coding OpenGL things.
My final university work is about OpenGL + CUDA (a nVidia solution to use GPUs for general purposes http://www.nvidia.com/object/cuda_home.html).
I hope we could help each other!
By the way, here’s one link to an older version of the book:
http://web.archive.org/web/20071025050003/http://www.glprogramming.com/red/
thanks Mario.
http://www.opengl.org/documentation/red_book/
They keep a copy of the earlyer editions on the oficial site.
Just for anyone looking for the digital version.
You’ve probably all seen this, but for anyone interested in OpenGL… this link is a great resource…
http://nehe.gamedev.net/
Don’t bother with the red book, it’s really dated, like 1997 dated.
I’ve done a fair amount of OpenGL programming over the past decade and have just started my first iPhone app. Please note that the iPhone currently uses the OpenGL ES 1.1 spec (go to khronos.org/opengles), don’t waste time learning the 2.0 spec. OpenGL ES does not have immediate mode calls like glVertex and glNormal, it is all done using array buffers and glBindBuffer, glBufferData and glDrawElements calls (or glDrawArrays calls for indexed primitives).
To include OpenGL, the iPhone OpenGLES Framework in your project and use:
#import
Learning a new library is best done, as always, using example code. The commenter who mentioned the oolongengine is right, download and inspect that and perhaps build your first project or two from it. From there, I’d recommend making your own stripped down engine that fits your needs best.
Matt, thanks. I’m finding the Red Book extremely helpful. I disagree with your statement “Learning a new library is best done, as always, using example code.” That may work for you, but not for everyone. Wading through a fully fleshed out example from scratch is overwhelming sometimes. I like to take things in more bite sized chunks, with explanations. But to each his own.
Whoops, I forgot to escape my brackets, I meant:
#import <OpenGLES/ES1/gl.h>
What about this…
http://www.memo.tv/msashape3d_demo_immediate_mode_opengl_on_desktop_iphone
openframeworks on iphone!!!! wow!!
I didn’t think the iPhone with OpenGL ES 1.1 supported GLUT?
http://www.box2d.org/forum/viewtopic.php?f=7&t=1750
“I’m not sure I follow – are you thinking of trying to get the whole testbed to compile as-is on iPhone, as well? I think that might be difficult, as to my knowledge nobody has managed to get GLUT (even the ES version) working on the iPhone, and the testbed also has a significant glui dependency which will be impossible to translate, as well.”
no, glut is not on the iphone. i’m just trying to learn opengl in general now. get the concepts and flows down, not in an iphone environment, but just in general. when i have a clue of what it’s all about, then i’m sure i can understand it a whole lot better on the iphone itself.
Also, if you want to get games in 3d on the iphone quickly you can try Unity3D’s iPhone path. It will cost you but you can start making 3d games much faster.
Also checkout Google’s new engine for the iPhone/iPod touch http://code.google.com/p/oolongengine/
Box2D has also been ported to the iPhone as well http://sourceforge.net/projects/box2d
i have been reding this book too. found it at school between books bout excel and word97 in a library from the last decade.
it has been extremely helpful, i’ve learned so much that i could use for processing, flash and in general for everything. just to understand this stuff is key. besides that adding some zeros to the number of things you can move is always great 🙂