在纹理中转换 UIImage [英] Convert an UIImage in a texture
本文介绍了在纹理中转换 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:
为您的图片提取信息:
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屋!
查看全文