0

I'm trying to load a screenshot into an OpenGLES Texture, but when i display it, it is just black.

Here is how I get the screenshot:

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

and here is how I'm trying to load the image into the Texture:

GLuint setupTextureFromImage(UIImage* image) {
    CGImageRef spriteImage = image.CGImage;
    if (!spriteImage) {
        NSLog(@"Failed to load image");
        exit(1);
    }

    size_t width = CGImageGetWidth(spriteImage);
    size_t height = CGImageGetHeight(spriteImage);

    GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));

    CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Host);

    CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);

    CGContextRelease(spriteContext);

    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int) width, (int) height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

    free(spriteData);
    return texName;
}

if I load an image from the resources with [UIImage imageNamed: @"FileName.png"] it works and if I'm setting the screenshot as the background color it also works, so both parts work separately

trandang
  • 23
  • 7
  • 1
    How big is it? I've had textures quietly not load in the past because they were over a certain size. That was on Android, but from what I remember it's more about the GPU than the platform... Someone seems to have [a handy table for iOS max texture sizes here](http://www.uchidacoonga.com/2012/04/quick-tip-max-texture-size/). – Matt Gibson Aug 13 '14 at 22:31
  • ... though at runtime, on a specific device, you should use `glGet` with `GL_MAX_TEXTURE_SIZE` to figure out if your images are too large and either split it up or shrink it if necessary. – Tommy Aug 13 '14 at 22:43
  • it is the size of an iphone 5s – trandang Aug 13 '14 at 22:44
  • Okay, so you should be fine [up to 4096x4096](http://stackoverflow.com/questions/25226653/glerror-0x0501-when-loading-a-large-texture-with-opengl-es-on-the-iphone4), so it's probably not that if you're just doing it with a screenshot. Might be worth just checking width and height to see if they're what you expect, though. – Matt Gibson Aug 13 '14 at 23:03
  • yeah the size is 320x568 like it should be... – trandang Aug 13 '14 at 23:13
  • Is there data in `spriteData` after the `CGContextDrawImage` call? Since you allocated it zero filled, it should be easy to see in a debugger if the image data was successfully written to it. – Reto Koradi Aug 14 '14 at 02:27
  • yes it is filled with bytes – trandang Aug 14 '14 at 15:59

0 Answers0