转换纹理中的UIImage

时间:2013-01-16 16:18:23

标签: ios objective-c opengl-es textures

在我的opengl项目中,我需要在纹理中转换UIImage;这是怎么做的? 你能救我吗?

3 个答案:

答案 0 :(得分:11)

我没有测试以下内容,但我会分三步分解转换:

  1. 提取图片信息:

    UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"];
    CGImageRef imageRef = [image CGImage];
    int width = CGImageGetWidth(imageRef);
    int height = CGImageGetHeight(imageRef);
    
  2. 使用以上属性分配textureData

    GLubyte* textureData = (GLubyte *)malloc(width * height * 4); // if 4 components per pixel (RGBA)
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;  
    CGContextRef context = CGBitmapContextCreate(textureData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGColorSpaceRelease(colorSpace);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    
  3. 设置纹理:

    GLuint textureID;    
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &textureID);
    
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
    
  4. 修改

    阅读this tut;一切都是从一个图像转换为纹理并在iOS环境中应用纹理来解释的。

答案 1 :(得分:1)

这是从UIImage中获取纹理的快速版本

func setupTexture(sourceImage: UIImage) -> GLuint {

guard let textureImage = sourceImage.cgImage else {
    print("Failed to load image")
    return 0
}

let width = textureImage.width
let height = textureImage.height

/*
 it will write one byte each for red, green, blue, and alpha – so 4 bytes in total.
 */

let textureData = calloc(width * height * 4, MemoryLayout<GLubyte>.size) //4 components per pixel (RGBA)
let spriteContext = CGContext(data: textureData,
                              width: width,
                              height: height,
                              bitsPerComponent: 8,
                              bytesPerRow: width * 4,
                              space: textureImage.colorSpace!,
                              bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

spriteContext?.draw(textureImage, in: CGRect(x: 0, y: 0, width: width, height: height))

var textName = GLuint()
glGenTextures(1, &textName)
glBindTexture(GLenum(GL_TEXTURE_2D), textName)
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width),
             GLsizei(height), 0, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), textureData)

return textName

}

注意:需要注意的是,当我们加载图片时,Core Graphics会翻转图片。

答案 2 :(得分:0)

使用GLKit框架执行此操作的另一种方法:

$PATH

//Path to image NSString *path = [[NSBundle mainBundle] pathForResource:@"textureImage" ofType:@"png"]; //Set eaglContext [EAGLContext setCurrentContext:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]]; //Create texture NSError *theError; GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:filePath options:nil error:&theError]; glBindTexture(texture.target, texture.name); 是纹理的OpenGL上下文名称。