Loading PNG files with texture 2d? PNG Alpha Channel support?

Discussion in 'iOS Programming' started by MasterObiWan, Jan 31, 2009.

  1. MasterObiWan macrumors newbie

    Jan 12, 2009
    Hello again,

    I am trying to load a png with texture 2D. Can someone please point me out where I can find more information about pixelFormat? and what kind of formats I can choose from? I'd appreciate you're help.

    The code I am working with:
    //makes a link to the file
    NSString *mytest = [[NSBundle mainBundle] pathForResource:mad:"brick" ofType:mad:"png"];
    NSData *mydata = [NSData dataWithContentsOfFile:mytest];

    //loads the file
    testTexture = [[Texture2D alloc] initWithData:[mydata bytes] pixelFormat:kTexture2DPixelFormat_RGBA_PVRTC4 pixelsWide:64 pixelsHigh:64 contentSize:CGSizeMake(64,64)];

    I need to change this part of the code pixelFormat:kTexture2DPixelFormat_RGBA_PVRTC4 to something the PNG format desires but I can't seem to find the documentation.

    What i'm trying to do is load an image of a character sprite for a game, which also has an alpha channel (parts where you can see through the image). Loading it as a pvr file makes the see through parts black. So how can I make this support png with alpha channel? (and if possible allowing images with dimensions other than powers of 2's.)

    I also have another question which doesn't need to be answered. Is their a way I can load 3ds files using a similar method as above? It'd be great help if you knew the answer.
  2. xsmasher macrumors regular


    Jul 18, 2008
    a 24-bit PNG file with an eight-bit alpha channel would be closer to kTexture2DPixelFormat_RGBA8888; but I don't think changing that one constant will help you. PNG files contain compressed data, and loading the file directly into a data object doesn't decompress it.

    I don't use texture2d, but it looks like initWithImage will do what you want instead:

    texture = [[Texture2D alloc] initWithImage:[UIImage imageWithContentsOfFile :[[NSBundle mainBundle] pathForResource:@"myTexture" ofType:@"png"]]];
    Nothing built in. I've seen posts from a few people building .obj file loaders in objective-C; your best bet would be getting a copy of their code. Search this forum or iphonedevsdk.com , or check out this example code:


Share This Page