Over a million developers have joined DZone.
Gold Partner

Loading Images into OpenGL in iPhone

· Mobile Zone

The Mobile Zone is brought to you in partnership with Apptimize.  Discover the 5 little know reasons your app is underperforming and how to better understand your customers mobile needs.

So you can’t make a game without images, right? Well, actually you can but that’s another story.

But how can you load a jpg or a png and use then on OpenGLES?

First let’s make a simple class Texture2D

class Texture2D 
Texture2D(int texture id, int width, int height)
_textureId = id; _width = width; _height = height;

virtual ~Texture2D(void)
// Delete Texture from HGL Memory
glDeleteTextures(1, ((GLuint*)&_textureId));
int getTextureId() {return _textureId; }
int _textureId; // The reference ID of the texture in OpenGL memory
int _width;
int _height;

 Now for the code to actually load the image

Texture2D* IPhoneOperativeSystem::LoadImage(std::string imagefile)
// Id for texture
GLuint texture;
// Generate textures
glGenTextures(1, &texture);
// Bind it
glBindTexture(GL_TEXTURE_2D, texture);
// Set a few parameters to handle drawing the image at lower and higher sizes than original

NSString *path = [[NSString alloc] initWithUTF8String:imagefile.c_str()];

NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
return NULL;
// Get Image size
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Allocate memory for image
void *imageData = malloc( height * width * 4 );
CGContextRef imgcontext = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( imgcontext, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( imgcontext, 0, height - height );
CGContextDrawImage( imgcontext, CGRectMake( 0, 0, width, height ), image.CGImage );

// Generate texture in opengl
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
// Release context
// Free Stuff
[image release];
[texData release];

// Create and return texture
Texture2D* tex = new Texture2D(texture,width, height);
return tex;

 So right now you can do something like

Texture2D* tree_image = IPhoneOperativeSystem("tree_image.jpg");

For drawing, before sending your primitives just do something like:

For drawing, before sending your primitives just do something like:

This code should work on all iOS devices

The Mobile Zone is brought to you in partnership with Apptimize.  Learn more about app optimization methods that help teams identify user preferences, decide the next iterative changes to make, and validate them through testing.


Published at DZone with permission of David Amador , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}