简体   繁体   中英

GLKTextureLoader fails when calling from update

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

This was working before the last upgrade. Maybe I was being lucky with timings. I suspect that it is failing because some thread is busy or something?

Here is the error in full.

Domain=GLKTextureLoaderErrorDomain Code=8 "The operation couldn't be completed. (GLKTextureLoaderErrorDomain error 8.)" UserInfo=0x10b5b510 {GLKTextureLoaderGLErrorKey=1282, GLKTextureLoaderErrorKey=OpenGL error}

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

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

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

I had almost the same error:

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

It is caused by switching between programs. An Open GL ES error breakpoint will be encountered if I tried to call glUniform1i with a program which is not currently in use.

Fixed by using the correct program, avoid trigger any error breakpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM