GLKTextureLoader fails when calling from update
I had a problem like this and the work arround was loading the texture without the glktextureloader.
Here some code for loading the texture without the GLKtextureLoader:
bool lPowerOfTwo = false;UIImage *image = [UIImage imageNamed:@"texture.png"];GLuint width = CGImageGetWidth(image.CGImage);GLuint height = CGImageGetHeight(image.CGImage);CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();void *imageData = malloc( height * width * 4 );CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );CGColorSpaceRelease( colorSpace );CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );CGRect bounds=CGRectMake( 0, 0, width, height );CGContextScaleCTM(context, 1, -1);bounds.size.height = bounds.size.height*-1;CGContextDrawImage(context, bounds, image.CGImage);GLuint lTextId;glGenTextures(1, &lTextId);glBindTexture(GL_TEXTURE_2D, lTextId);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);if(!lPowerOfTwo){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glGenerateMipmap(GL_TEXTURE_2D);}else{ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );}CGContextRelease(context);free(imageData);
The lTextId variable has the opengl Texture Id.
Note: if the texture dimension is not power of two, the texture will be shown black if the GL_TEXTURE_WRAP_S and _T are not set to GL_GLAMP_TO_EDGE
I had a similar problem to you. What I did to fix the problem was to have a Class which had all of the textures I wanted to use for the whole game. In viewDidLoad:
I initialised the class and loaded all of the textures. When I needed to use any of the textures, they were already loaded and the problem didn't occur.
eg. In viewDidLoad
GameTextures *textures = [GameTextures alloc] init];[textures LoadAll];
LoadAll would load all the textures for later use
Then when you need to use a texture
[myBackground setTexture: textures.backgroundTexture2];
Hope this helped :)
I was seeing this same behavior, which was caused by an unrelated error. Fix the error and the texture should load properly. See this thread: GLKTextureLoader fails when loading a certain texture the first time, but succeeds the second time