简体   繁体   English

从更新调用时,GLKTextureLoader失败

[英]GLKTextureLoader fails when calling from update

Loading textures from viewDidLoad works fine. 从viewDidLoad加载纹理工作正常。 But if I try to load them from the GLKViewController update I get an error. 但是,如果我尝试从GLKViewController更新加载它我得到一个错误。 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} Domain = GLKTextureLoaderErrorDomain Code = 8“无法完成操作。(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? 所以问题是,我可以安全地从GLKViewController更新函数加载纹理吗? 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. 我有这样的问题,工作arround正在加载没有glktextureloader的纹理。

Here some code for loading the texture without the GLKtextureLoader: 这里有一些代码用于在没有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. lTextId变量具有opengl纹理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 注意:如果纹理尺寸不是2的幂,如果GL_TEXTURE_WRAP_S和_T未设置为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. 我解决这个问题的方法是让一个拥有我想要用于整个游戏的所有纹理的Class。 In viewDidLoad: I initialised the class and loaded all of the textures. viewDidLoad:我初始化了类并加载了所有纹理。 When I needed to use any of the textures, they were already loaded and the problem didn't occur. 当我需要使用任何纹理时,它们已经加载并且没有出现问题。

eg. 例如。 In viewDidLoad viewDidLoad

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

LoadAll would load all the textures for later use LoadAll将加载所有纹理以供以后使用

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 看到这个帖子: GLKTextureLoader在第一次加载某个纹理时失败,但第二次成功

I had almost the same error: 我有几乎相同的错误:

Error Domain=GLKTextureLoaderErrorDomain Code=8 "(null)" UserInfo={GLKTextureLoaderGLErrorKey=1282, GLKTextureLoaderErrorKey=OpenGLES 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. 如果我尝试使用当前未使用的程序调用glUniform1i,则会遇到Open GL ES错误断点。

Fixed by using the correct program, avoid trigger any error breakpoint. 通过使用正确的程序修复,避免触发任何错误断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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