texturesglkitglktextureloader

GLKTextureLoader loads transparent textures unexpectedly


I generate text as image using UIKit (simplified):

UIGraphicsBeginImageContextWithOptions(textureSize, NO, 0);
    [variant.text drawInRect:CGRectIntegral(necessaryRect) withFont:textFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    UIImage *img = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
UIGraphicsEndImageContext();

Then I save it to caches directory and load from file with GLKTextureLoader (-textureWithContentsOfFile:options:error:; options = nil).

After loading I see "white artefacts" - white pixels around text, especially well visible, when light is not straight (applying some rotation to textured surface).

I've checked generated images - opened them in image editor & added black background - I can't see anything there, except black color. Then I've checked textures - added one more texture behind text's one - simply filled with black color. And on my 3D-object I see "white shadows" around text letters, like some border.

effect.textrue2d0.envMode = GLKTextureEnvModeDecal;    // just text
...
effect.texture2d0.envMode = GLKTextureEnvModeModulate; // black bg gexture + text
effect.textrue2d1.envMode = GLKTextureEnvModeDecal;
...
glEnable(GL_BLEND);  // always
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

If I load texture with GLKTextureLoaderApplyPremultiplication enabled, problem is fixed, but text looks not-so-clear (like not 100% black color used).

Changing envMode of bottom texture (2d0) to GLKTextureEnvModeReplace will also fix white pixels, but remove light.

What am I doing wrong?

Update:

I've wrote my own shaders recently. And there are no problems with textures, loaded without alpha premultiplication. So, I expect problem with GLKit shader itself.


Solution

  • The problem was in different EAGLContexts.

    GLKTextureLoader loads fonts to current context ([EAGLContext currentContext]) and correctly displays it from there. And in my app (it uses 2 different contexts) it was buggy: sometimes I loaded in one context and displayed in another (simply forgot to change them during loading).