转换纹理中的UIImage [英] Convert an UIImage in a texture

查看:510
本文介绍了转换纹理中的UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的opengl项目中,我需要在纹理中转换UIImage;这样做的方法是什么?
你能帮助我吗?

In my opengl project I need to convert an UIImage in texture; what's the way to do it? Can you help me?

推荐答案

我没有测试以下但我将分3步拆分转换:

I haven't test the following but i will decompose the conversion in 3 steps:


  1. 提取图片信息:

  1. Extract info for your image:

UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"];
CGImageRef imageRef = [image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);


  • 分配 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);
    


  • 设置纹理:

  • Set-up your texture:

    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);
    


  • 编辑:

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

    Read this tut; everything is explained from the conversion of one image to a texture and applying a texture in an iOS environment.

    这篇关于转换纹理中的UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆