ios - GLKBaseEffect 未加载纹理(纹理在对象上显示为黑色)

标签 ios opengl-es textures glkit glktextureloader

我在 OpenGL 项目中使用 GLKit。一切都基于 GLKView 和 GLKBaseEffect(无自定义着色器)。在我的项目中,我有几个 View 具有用于显示 3D 对象的 GLKViews,有时其中几个 View 可以同时“打开”(即在模态视图堆栈中)。

虽然到目前为止一切都运行良好,但在我创建的新 View 中,我需要一个带有纹理的矩形来模拟我应用程序 3D 世界的卷尺。出于某种未知原因,仅在该 View 中,纹理未直接加载到 opengl 上下文中:纹理由 GLKTextureLoader 加载,但绘制矩形时为黑色,并查看调试中的 OpenGL 帧,我可以看到加载了一个空纹理(有一个对纹理的引用,但它全部归零或为空)。

我正在绘制的形状定义为:(它最初是一个三角形带,但我切换为三角形以确保这不是问题)

static const GLfloat initTape[] = {
    -TAPE_WIDTH / 2.0f, 0, 0,
    TAPE_WIDTH / 2.0f, 0, 0,
    -TAPE_WIDTH / 2.0f, TAPE_INIT_LENGTH, 0,

    TAPE_WIDTH / 2.0f, 0, 0,
    TAPE_WIDTH / 2.0f, TAPE_INIT_LENGTH, 0,
    -TAPE_WIDTH / 2.0f, TAPE_INIT_LENGTH, 0,
};
static const GLfloat initTapeTex[] = {
    0, 0,
    1, 0,
    0, 1.0,

    1, 0,
    1, 1,
    0, 1,
};

我将效果变量设置为:

    effect.transform.modelviewMatrix = modelview;
    effect.light0.enabled = GL_FALSE;

    // Projection setup
    GLfloat ratio = self.view.bounds.size.width/self.view.bounds.size.height;
    GLKMatrix4 projection = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(self.fov), ratio, 0.1f, 1000.0f);
    effect.transform.projectionMatrix = projection;


    // Set the color of the wireframe.
    if (tapeTex == nil) {
        NSError* error;
        tapeTex = [GLKTextureLoader textureWithContentsOfFile:[[[NSBundle mainBundle] URLForResource:@"ruler_texture" withExtension:@"png"] path] options:nil error:&error];
    }
    effect.texture2d0.enabled = GL_TRUE;
    effect.texture2d0.target = GLKTextureTarget2D;
    effect.texture2d0.envMode = GLKTextureEnvModeReplace;
    effect.texture2d0.name = tapeTex.name;

渲染循环是:

       [effect prepareToDraw];
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_CULL_FACE);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        glEnableVertexAttribArray(GLKVertexAttribPosition);
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribPosition, COORDS, GL_FLOAT, GL_FALSE, 0, tapeVerts);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, tapeTexCoord);

        glDrawArrays(GL_TRIANGLES, 0, TAPE_VERTS);

        glDisableVertexAttribArray(GLKVertexAttribPosition);
        glDisableVertexAttribArray(GLKVertexAttribTexCoord0);

我还在另一个 View 中用其他对象测试了纹理本身,它工作正常,所以这不是纹理文件的错误。

非常感谢任何帮助,因为我在这个问题上停留了超过 3 天。

更新:此外,在渲染循环期间没有 glErrors。

最佳答案

许多天后,我终于发现了我的错误 - 当使用多个 openGL 上下文时,使用 shareGroup 创建 GLKTextureLoader 很重要,否则纹理不一定加载到正确的上下文。

不是使用类方法 textureWithContentOf,每个上下文都需要它自己的 GLKTextureLoader,它是用 context.sharegroup 初始化的,并且只为那个 View 使用那个纹理加载器。 (实际上纹理可以在不同的上下文之间保存,但我不需要共享组的那个功能)。

关于ios - GLKBaseEffect 未加载纹理(纹理在对象上显示为黑色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236297/

相关文章:

android - 在 Android 上动态显示/隐藏多个 OpenGL View

performance - 使用显卡在运行时生成 mipmap 更快,还是使用文件中的纹理加载它们更快?

ios - 如何在 iOS Objective C 中检索数组前的借口(解析)

ios - NSFetchedResultsController 在排序时检索特定数据

windows - 基于 OpenGL ES 的桌面硬件加速 OpenVG 实现

iphone - glDrawElements vs glDrawArray 效率

c++ - 使用openGL的glBindFramebuffer好像没有效果

XNA 不显示我自己的 fbx 模型的纹理

iphone - 移动容器 UIView 时 UITableView 调整大小

ios - iOS App调试时如何查看App Group Shared Container目录的内容?