0

I am taking a picture trough the camera and I want to change the format of it to make it OpenGL ES 2.0 texture compatible.

First I get the picture and I resize it (making it a power of 2 just in case):

UIImage *newImage = [self imageWithImage:imgName scaledToSize:CGSizeMake(100, 144)];

Then I change the format from jpeg (which i think its the iphone's default format) to png:

NSData *test = UIImagePNGRepresentation(newImage);

UIImage *converted = [[UIImage alloc] initWithData:test];

Last I create my context and draw into it:

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

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



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

CGContextRelease(spriteContext);

And i can just add it the open GL buffer after this, unfortunately its not working (Only black appears), im pretty sure its because of the image format because if i do the exact same proceadure with another image which i already know it works then everything goes smoothly.

I did some research and it might be something to do with the color depth in my image, since i need 4 bits per pixel to render in opengl texture (4,4,4,4).

Internal Format: RGBA, External Format: RGBA, Type: UNSIGNED BYTE, Bytes per Pixel: 4.

So my question is, how do i change this png from whatever depth it has to the scheme i need?

PD: If anyone knows a different way to load an image into opengl please let me know!

Thanks!

Pochi
  • 13,391
  • 3
  • 64
  • 104

1 Answers1

0

my method for setup texture. I think all clear, if not - ask questions

- (GLuint)setupTexture:(NSString *)fileName {
    CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
    if (!spriteImage) {
        NSLog(@"Failed to load image %@", fileName);
        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);    

    CGContextTranslateCTM (spriteContext, 0, height);
    CGContextScaleCTM (spriteContext, 1.0, -1.0);

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

    CGContextRelease(spriteContext);

    GLuint texName;
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);


    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


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

    free(spriteData);        
    return texName; 
}
Volodymyr B.
  • 3,369
  • 2
  • 30
  • 48
  • Thanks for the quick reply. I tried your method and it works only for an already defined texture. the problem is that i am trying to "texturize" an object using the camera on the iphone so i have to take the input from the camera. I have an image and i am displaying it on my screen in jpeg and png formats so i know it works, i also tried resizing it. but when i tried displaying it using your method i get the exact same result im getting (black where the texture should be) I have come across something similar when i wanted to create my texture and the problem was the color depth. Any ideas? =( – Pochi Jan 12 '12 at 08:52
  • It turns out the problem was in the image size i was creating. This line UIImage *newImage = [self imageWithImage:imgName scaledToSize:CGSizeMake(100, 144)]; Creates a NON power of 2 image, and for iphone textures you NEED a power of 2 image (256x256, 256x512, etc). Thx for ur help. – Pochi Jan 16 '12 at 09:29
  • yea! :) i think you know about this. – Volodymyr B. Jan 16 '12 at 11:00