【问题标题】:Loading textures in another Thread using detachNewThreadSelector使用 detachNewThreadSelector 在另一个线程中加载纹理
【发布时间】:2011-12-04 13:57:54
【问题描述】:

我正在我的 iPhone 应用程序中加载 3D 模型。由于模型可能很大,我想将它加载到另一个线程中。我调用了 detachNewThreadSelector 来生成新线程并简单地将模型加载调用移到那里。模型加载但没有纹理。当我签入调试器时,我看到 glGenTextures 调用不会创建新的名称/ID(它保留为 0)。

如果我将函数调用移到主线程,一切正常。

【问题讨论】:

    标签: opengl-es textures nsthread


    【解决方案1】:

    OpenGL 仅在您创建 OpenGL 上下文时工作,并且该上下文仅对一个线程是当前的,因此要在另一个线程中使用 OpenGL,您必须为该线程创建另一个上下文,并使其在该线程中成为当前的。

    通常在上下文之间共享资源(通过类似于 wglShareLists 的方式)。

    【讨论】:

    • 抱歉忘了提 - 这是在 iPhone 上的 OpenGL ES 上。有没有例子?
    【解决方案2】:

    我是使用共享组完成的。这是为我完成的代码:

    主线程:

    [NSThread detachNewThreadSelector:@selector(load:) toTarget:[AsynchModelLoader 类] withObject:self];

    衍生线程:

    +(void) 加载:(id)param {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    ModelViewer* mview = (ModelViewer*)param;

    EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 共享组:mview.context.sharegroup];
    [EAGLContext setCurrentContext:aContext];

    mview.lib = [Importer fromResource:@"model3d"];

    [池排水]; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多