GLKTextureLoader does not work when calling update

Loading textures from viewDidLoad works fine. But if I try to download them from the GLKViewController update, I get an error. I do this because I want to exchange a new background texture without changing the view.

This worked until the last update. Maybe I got lucky with timing. I suspect that he is failing because some thread is busy or something else?

Here is the complete mistake.

Domain = GLKTextureLoaderErrorDomain Code = 8 "Operation could not be completed (error GLKTextureLoaderErrorDomain 8.)" UserInfo = 0x10b5b510 {GLKTextureLoaderGLErrorKey = 1282, GLKTextureLoaderErrorKey = OpenGL error}

So the question is, can I safely load the texture from the GLKViewController update GLKViewController ? Or do I need to rethink my approach and reload the whole view or something else?

Here is my function:

 -(void) LoadTexture:(NSString *)texture textureInfo:(GLKTextureInfo**)textureInfo { NSString *path = [[NSBundle mainBundle] pathForResource:texture ofType:@"png"]; NSError *error = nil; (*textureInfo) = [GLKTextureLoader textureWithContentsOfFile:path options:nil error:&error]; NSLog(@"path %@", path); if(!(*textureInfo)) { NSLog(@"Failed to load texture %@ %@", texture, error); } else { NSLog(@"LOADED Texture %@ !!! YAY!!! ", texture); } } 

Thanks,

David

+6
source share
4 answers

I had this kind of problem and working arround loaded a texture without glktextureloader.

Here is some code to load the texture without 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 variable lTextId has an opengl texture identifier.

Note. If the texture size is not equal to two, the texture will be displayed in black if GL_TEXTURE_WRAP_S and _T are not set to GL_GLAMP_TO_EDGE

+1
source

I had a similar problem. What I did to fix the problem was to have a class that had all the textures that I wanted to use for the whole game. In viewDidLoad: I initialized the class and loaded all the textures. When I needed to use any texture, they were already loaded and the problem did not occur.

eg. In viewDidLoad

 GameTextures *textures = [GameTextures alloc] init]; [textures LoadAll]; 

LoadAll will load all textures for future reference.

Then when you need to use texture

 [myBackground setTexture: textures.backgroundTexture2]; 

Hope this helps :)

0
source

I saw the same behavior that was caused by an unrelated error. Correct the error and the texture should load correctly. See This topic: GLKTextureLoader crashes when loading a specific texture for the first time, but the second time is successful

0
source

I had almost the same error:

Domain error = GLKTextureLoaderErrorDomain Code = 8 "(null)" UserInfo = {GLKTextureLoaderGLErrorKey = 1282, GLKTextureLoaderErrorKey = OpenGLES error.}

This is caused by switching between programs. If the Open GL ES search point is detected, if I try to call glUniform1i with a program that is not currently in use.

Corrected using the correct program to avoid triggering any breakpoint.

0
source

Source: https://habr.com/ru/post/916140/


All Articles