【发布时间】:2010-12-23 00:03:42
【问题描述】:
我正在制作一个需要 OpenGL 来执行翻转动画的 iPad 应用程序。我有一个正面图像纹理和一个背面图像纹理。这两个纹理都是屏幕截图。
// Capture an image of the screen
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Allocate some memory for the texture
GLubyte *textureData = (GLubyte*)calloc(maxTextureSize*4, maxTextureSize);
// Create a drawing context to draw image into texture memory
CGContextRef textureContext = CGBitmapContextCreate(textureData, maxTextureSize, maxTextureSize, 8, maxTextureSize*4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(textureContext, CGRectMake(0, maxTextureSize-size.height, size.width, size.height), image.CGImage);
CGContextRelease(textureContext);
// ...done creating the texture data
[EAGLContext setCurrentContext:context];
glGenTextures(1, &textureToView);
glBindTexture(GL_TEXTURE_2D, textureToView);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maxTextureSize, maxTextureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
// free texture data which is by now copied into the GL context
free(textureData);
每个纹理占用大约 8 MB 内存,这对于 iPhone/iPad 应用程序来说是不可接受的。谁能告诉我如何压缩纹理以减少内存使用?
【问题讨论】:
-
我建议研究设备上的 PowerVR 纹理压缩,但这个问题的答案似乎表明这是不可能的:Convert .png to PVRTC on the iPhone.。但是,可能有不同的方法来实现这一点。
-
@BradLarson 我想我错了。我认为更好的方法是缩小屏幕截图(512 * 512)并缩放以适应纹理(1024 * 1024)。这样,现在每个纹理都是 1MB。但我不知道如何实现这一点。有什么帮助吗?