1

GLKit を使用して IPhone OpengGL アプリを開発しており、次のコードを使用してテクスチャを作成しています。

NSRange dotRange = [textureFileName rangeOfString:@"." options:NSCaseInsensitiveSearch];
        if (dotRange.location == NSNotFound){
            NSLog(@"OpenGLDRawMaterial:createTextureFromFileName, incorrect file name given in inputs");
            return nil;
        }   

   GLKTextureInfo *newTexture;

        NSError *error = nil;   // stores the error message if we mess up
        NSString *bundlepath = [[NSBundle mainBundle] pathForResource:[textureFileName substringToIndex:dotRange.location] 
                                                               ofType:[textureFileName substringFromIndex:(dotRange.location+1)]];

newTexture = [GLKTextureLoader textureWithContentsOfFile:bundlepath options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft] error:&error];

メインスレッドで動作する限り、コードは非常にうまく機能します。ワーカースレッドで機能させようとするたびに、次のメッセージが表示されます。

"2013-03-04 02:09:01.528 Puppeteer[7063:1503] イメージからのテクスチャの読み込み中にエラーが発生しました: エラー Domain=GLKTextureLoaderErrorDomain Code=17 "操作を完了できませんでした。(GLKTextureLoaderErrorDomain エラー 17)" UserInfo=0x1c5977e0 "

グランドセントラルディスパッチキューに使用しているコードは次のとおりです。

dispatch_queue_t backgroundQueue = dispatch_queue_create("loadPlayViewBackgroundTexture", 0);

    dispatch_async(backgroundQueue, ^{
       [self createTexturesForPlayView]; // method calling texture creation

        dispatch_async(dispatch_get_main_queue(), ^{
                  });
             });
             dispatch_release(backgroundQueue);

この問題を解決し、テクスチャをバックグラウンドでロードする方法についての洞察やアイデアがあれば、私は非常に感謝しています:) Cheers, Stéphane

4

1 に答える 1

2

+textureWithContentsOfFile:options:error:ドキュメントには、次のステートメントが含まれていることに注意してください。ここを確認してください

This class method loads the texture into the sharegroup attached to the current context for the thread this method is called on.

バックグラウンド スレッドから呼び出す場合-textureWithContentsOfFile:、そのスレッドには OpenGL コンテキスト セットがないため (現在の GL コンテキストはスレッドごとの状態です)、GLKit はテクスチャをロードする共有グループを認識しません。

しかし、あなたはこれを必要以上に難しくしています。GLKit は、非同期テクスチャの読み込み自体を既に管理できます。バリアントを見てください-textureWithContentsOfFile:options:queue:completionHandler:。独自のキューを作成する必要はまったくありません。メイン キューを渡すだけで、ロードが完了したときに通知を受け取ることができます。

于 2013-03-04T03:29:45.107 に答える