【问题标题】:glError: 0x0501 when loading a large texture with OpenGL ES on the iPhone?在 iPhone 上使用 OpenGL ES 加载大纹理时出现 glError: 0x0501?
【发布时间】:2009-05-15 07:21:51
【问题描述】:

这是我用来加载纹理的代码。图像是一个 CGImageRef。使用此代码加载图像后,我最终使用 glDrawArrays() 绘制图像。

    size_t imageW = CGImageGetWidth(image);
    size_t imageH = CGImageGetHeight(image);
    size_t picSize = pow2roundup((imageW > imageH) ? imageW : imageH);

    GLubyte *textureData = (GLubyte *) malloc(picSize * picSize << 2);
    CGContextRef imageContext = CGBitmapContextCreate( textureData, picSize, picSize, 8, picSize << 2, CGImageGetColorSpace(image), kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big );  

    if (imageContext != NULL) {
        CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, (CGFloat)imageW, (CGFloat)imageH), image);    
        glGenTextures(1, &textureId);   
        glBindTexture(GL_TEXTURE_2D, textureId);

        // when texture area is small, bilinear filter the closest mipmap
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        // when texture area is large, bilinear filter the original
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        // the texture wraps over at the edges (repeat)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, picSize, picSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

        GLenum err = glGetError();
        if (err != GL_NO_ERROR)
            NSLog(@"Error uploading texture. glError: 0x%04X", err);

        CGContextRelease(imageContext);
    }

    free(textureData);

当图像为 320x480 时,这似乎可以正常工作,但当图像较大时(例如,picSize = 2048)就失败了。

这是我在调试器控制台中得到的:

Error uploading texture. glError: 0x0501

这个错误是什么意思?最好的解决方法是什么?

【问题讨论】:

    标签: iphone opengl-es


    【解决方案1】:

    你不是简单地达到了最大纹理大小限制吗?错误码为GL_INVALID_VALUE,见glTexImage2D docs

    如果width,则生成GL_INVALID_VALUEheight 小于0 或更大 比2 + GL_MAX_TEXTURE_SIZE,或者如果 要么不能表示为 2k+2(border) 的某个整数值 k.

    iPhone 不支持大于 1024 像素的纹理。解决方法是将图像拆分为多个纹理。

    【讨论】:

      【解决方案2】:

      也许您只是内存不足 - 您是否确认从 malloc() 返回的值不为 NULL?
      (这可以解释得到 0x0501,即 GL_INVALID_VALUE)。

      【讨论】:

        【解决方案3】:

        GLKitBaseEffect 也遇到了同样的问题,这似乎是内存问题(正如 Hexagon 建议的那样)。为了解决这个问题,我不得不添加手动纹理释放调用:

        GLuint name = self.texture.name;
        glDeleteTextures(1, &name);
        

        有关更多信息,请参阅此主题:Release textures (GLKTextureInfo objects) allocated by GLKTextureLoader

        【讨论】:

          猜你喜欢
          • 2014-10-03
          • 2012-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多