PNG画像からテクスチャ作成

mTexture = [self loadTexture:@"back.png"];

の中身振れてなかったんで、今回はテクスチャの作り方について。
画像を読み込んで、CGContextを使ってビットマップデータを作成して、テクスチャにします。

- (GLuint) loadTexture:(NSString*)fileName
{
    GLuint texture;
    CGImageRef img = [UIImage imageNamed:fileName].CGImage;
    int w, h;
    w = CGImageGetWidth(img);
    h = CGImageGetHeight(img);
    GLubyte* data = (GLubyte*)malloc(w * h * 4);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, w * 4, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextClearRect(ctx,CGRectMake(0, 0, w, h));
    
    CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), img);
    
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    free(data);
    
    return texture;
}

こんな感じ。ただ、これは、画像サイズでテクスチャ作ってるので、1つのテクスチャに複数の画像を貼る場合は、そういう画像をあらかじめ用意するか、このloadTexture関数を工夫してくださいな。