iOS的OpenGL使用glTexImage2D的参数,使UIImage?

我正在通过一些现有的代码为我分配的项目工作。

我有一个成功的调用glTexImage2D像这样:
glTexImage2D(GL_TEXTURE_2D, 0, texture->format, texture->widthTexture, texture->heightTexture, 0, texture->format, texture->type, texture->data);

我想创build一个图像(最好是CGImageUIImage )使用传递给glTexImage2D的variables,但不知道是否有可能。

我需要从OpenGL视图创build许多顺序图像(每秒钟很多),并保存以备后用。

我应该能够使用glTexImage2D使用的variables创build一个CGImageUIImage

如果我能够,我该怎么做呢?

如果没有,为什么我不能和我build议保存/捕获我的opengl视图的内容每秒多次?

编辑:我已经成功地使用glReadPixels等提供的苹果提供的一些技术捕捉图像我希望更快的东西,所以我可以每秒获得更多的图像。

编辑:审查和添加汤姆森的代码后,这里是由此产生的图像: 从汤姆逊的代码产生的图像

图像非常类似于图像应该看起来像,除了水平重复~5倍,下面有一些随机的黑色空间。

注意:video(每一帧)数据通过一个ad-hocnetworking连接到iPhone。 我相信相机是用YCbCr色彩空间拍摄每一帧

编辑:进一步审查汤姆森的代码我已经将您的新代码复制到我的项目,并得到了不同的图像作为结果:

图片2

宽度:320高度:240

我不知道如何find纹理 – >数据中的字节数。 这是一个无效的指针。

编辑:格式和types

texture.type = GL_UNSIGNED_SHORT_5_6_5

texture.format = GL_RGB

嘿binnyb,这里是使用存储在texture->data的数据创buildUIImage的解决scheme。 v01d肯定是正确的,你不会得到UIImage因为它出现在你的GL framebuffer中,但是它会在数据通过framebuffer之前得到一个图像。

发现你的纹理数据是16位格式的,5位是红色的,6位是绿色的,5位是蓝色的。 我添加了在创buildUIImage之前将16位RGB值转换为32位RGBA值的代码。 我期待着听到这是怎么回事。

 float width = 512; float height = 512; int channels = 4; // create a buffer for our image after converting it from 565 rgb to 8888rgba u_int8_t* rawData = (u_int8_t*)malloc(width*height*channels); // unpack the 5,6,5 pixel data into 24 bit RGB for (int i=0; i<width*height; ++i) { // append two adjacent bytes in texture->data into a 16 bit int u_int16_t pixel16 = (texture->data[i*2] << 8) + texture->data[i*2+1]; // mask and shift each pixel into a single 8 bit unsigned, then normalize by 5/6 bit // max to 8 bit integer max. Alpha set to 0. rawData[channels*i] = ((pixel16 & 63488) >> 11) / 31.0 * 255; rawData[channels*i+1] = ((pixel16 & 2016) << 5 >> 10) / 63.0 * 255; rawData[channels*i+2] = ((pixel16 & 31) << 11 >> 11) / 31.0 * 255; rawData[channels*4+3] = 0; } // same as before int bitsPerComponent = 8; int bitsPerPixel = channels*bitsPerComponent; int bytesPerRow = channels*width; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rawData, channels*width*height, NULL); free( rawData ); CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider,NULL,NO,renderingIntent); UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 

用于创build新图像的代码来自于使用Rohit 从原始RGBA数据创buildUIImage 。 我已经用我们的原始320×240图像尺寸testing了这个,已经将24位RGB图像转换为5,6,5格式,然后是32位。 我没有在512×512的图像上进行testing,但我不希望有任何问题。

您可以从您发送给GL的数据中创build图像,但是我怀疑这是您想要实现的。

我的猜测是你想要的帧缓冲区的输出。 要做到这一点,你需要glReadPixels()。 考虑到一个大的缓冲区(比如说1024×768),从GL读取像素需要几秒钟的时间,你不会每秒超过1个像素。

你应该能够使用UIImage的初始化imageWithData这个。 所有你需要的是确保texture-> data中的数据是UIImage构造函数可识别的结构化格式。

 NSData* imageData = [NSData dataWithBytes:texture->data length:(3*texture->widthTexture*texture->heightTexture)]; UIImage* theImage = [UIImage imageWithData:imageData]; 

imageWithData支持的types没有很好的文档,但是你可以从.png,.jpg,.gif创buildNSData,并且我毫不费力地设定.ppm文件。 如果纹理 – >数据是在这些二进制格式之一,我怀疑你可以得到这个运行一点实验。