【问题标题】:Render contents of UIView as an OpenGL ES texture but only black turns out将 UIView 的内容渲染为 OpenGL ES 纹理,但结果只有黑色
【发布时间】:2013-04-09 21:47:04
【问题描述】:

我正在通过 raywenderlich.com http://www.raywenderlich.com/4404/opengl-es-2-0-for-iphone-tutorial-part-2-textures上的教程学习 OpenGL ES

当我开始调整示例项目以获取 UIView 的内容作为要渲染的纹理时,结果只是黑屏:

黑色视图是 OpenGL ES 视图。

我使用了 Tommy 在这篇文章中发布的代码:Render contents of UIView as an OpenGL texture,这是我的版本:

- (GLuint)createTexture:(UIView *)view
{
    size_t width = CGRectGetWidth(view.layer.bounds) * [UIScreen mainScreen].scale;
    size_t height = CGRectGetHeight(view.layer.bounds) * [UIScreen mainScreen].scale;

    GLubyte * texturePixelBuffer = (GLubyte *) calloc(width * height * 4,
                                   sizeof(GLubyte));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(texturePixelBuffer,
                                                 width, height, 8, width*4, colorSpace, 
                                                 kCGImageAlphaPremultipliedLast | 
                                                 kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    [view.layer renderInContext:context];

    CGContextRelease(context);

    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, 
                 GL_UNSIGNED_BYTE, texturePixelBuffer);

    free(texturePixelBuffer);
    return texName;
}

【问题讨论】:

    标签: objective-c opengl-es


    【解决方案1】:

    根据 Open GL ES 2.0 规范,纹理可以是 2 的幂,也可以不是。但你必须使用

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    

    启用非二次幂纹理支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-06
      • 2018-09-17
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多