1
votes

I'm making an iPhone application with OpenGL ES 2.0 using the GLKit. I'm using GLKTextureLoader to load textures. When my texture sits inside a mainBundle - EVERYTHING IS FINE. I get its name with command [[NSBundle mainBundle] pathForResource:@"brushRose.png" ofType:nil] and the texture has a following path:

/var/mobile/Containers/Bundle/Application/D79492CB-D03C-464D-B06E-00D0DE4389DF/Texture Test.app/brushRose.png

When I try to download an absolutely same texture from Internet and store it inside Application's Documents folder. Path is like follow:

/var/mobile/Containers/Data/Application/CD3FBAAB-B8AE-47F5-9C6E-51C854FC1620/Documents/brushes/ps_roses.png

I get a TERRIBLE END RESULT which can be observed on the picture below: First row - texture from mainBundle, second row - from Documents

Any ideas how to fix the second case are welcome.

Test project can be found here

1
I tried the repository and neither modes worked. It only drew coloured squares. - Rhythmic Fistman
@Rhythmic Fistman: I have re-downloaded & rebuild project from scratch + I have tried it on three different devices. It works like expected. Can you, please, provide more details why you can see just squares? Have you tried to debug it? - vaa
heh, I was running the project in the simulator. why doesn't it work there? nobody knows. that is a question for another day. - Rhythmic Fistman
yes, somehow GLKit OpenGL code does't work in simulator for me as well. I am developing my project on the devices from the beginning. It would be nice to find a solution also for this problem. - vaa

1 Answers

2
votes

When you build an app containing PNGs, Xcode runs pngcrush on them which, among other things, pre-multiplies the alpha.

The "non main-Bundle" files you are using do not have pre-multiplied alpha. This explains the difference in appearance.

Your options are to run pngcrush on your url textures, or stop pngcrush running in your project, or conditionally apply the GLKTextureLoaderApplyPremultiplication when you load non-crushed PNGs:

NSMutableDictionary *options = [@{ GLKTextureLoaderOriginBottomLeft : @NO} mutableCopy];

if ( /** png is uncrushed **/ ) {
    options[GLKTextureLoaderApplyPremultiplication] = @YES;
}

NSError* error;
GLKTextureInfo* texture = [GLKTextureLoader textureWithContentsOfFile:brushPath options:options error:&error];