Files and Resources
The last loose end is file loading. At the very least, you will want to load textures. To demonstrate the opposite of what we have just done, the GLTextureHelper class has a C++ style header that can be included in any C++ code. The implementation of the class is, however, in an Objective C .mm file. While the class itself is a C++ class, it makes use of Cocoa to load any Quartz-supported image file format and load it as an OpenGL texture. This class is implemented in the source code files GLTextureHelper.h and GLTextureHelper.mm. Using the class is simple. First declare an instance of the class:
GLTextureHelper helper(1024, 1024);
The only parameters to the constructor are the maximum expected size in width and height of any textures you plan to load. Here, I use the maximum size supported by the iPhone. Rather than allocate memory repeatedly for each texture loaded, this class reuses a single block of memory to avoid repeated malloc/free cycles.
The LoadTexture method then loads the specified texture file and calls glTexImage2D with the appropriate parameters. For example, to initialize a texture object and create mipmaps for the body.png texture:
// Load the body texture glBindTexture(GL_TEXTURE_2D, textureObjects[BODY_TEXTURE]); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); helper.LoadTexture("body.png"); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Textures are placed in the \Resources folder. Right-click and select "Add Existing Files" and you can bring in your own textures.
It's exciting that independent developers can now get their hands on OpenGL ES devices and immediately start writing code. iPhones represent only one exciting opportunity to take advantage of the growing prevalence of mobile 3D devices.
The example code accompanying this article is a great place to start with iPhone (and iPod Touch) development. While not a complete development framework, it brings within reach GLUT-style technology demos and proof-of-concept prototypes.