Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 274
David John Avatar asked May 19 '12 02:05

David John


1 Answers

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

like image 104
ZoserLock Avatar answered Oct 18 '22 01:10

ZoserLock