如何在iPhone/iPad上的OpenGL ES中压缩纹理?

问题描述:

我正在制作一款iPad应用程序,需要使用OpenGL来做翻转动画。我有一个正面图像纹理和背面图像纹理。这两个纹理都是截图。如何在iPhone/iPad上的OpenGL ES中压缩纹理?

// 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应用程序来说是不可接受的。任何人都可以告诉我如何压缩纹理来减少内存使用量?

+0

我会建议查看设备上的PowerVR纹理压缩,但这个问题的答案似乎表明,这是不可能的:[将.png转换为PVRTC * on * iPhone。](http://*.com/questions/1020944/convert-png-to-pvrtc-on-the-iphone)。但是,实现这一点可能有不同的方法。 – 2010-12-22 16:09:06

+0

@BradLarson我想我错了。我认为更好的方法是缩小屏幕截图(512 * 512)并将其缩放以适应纹理(1024 * 1024)。这样,现在每个纹理都是1MB。但我不知道如何实现这一点。任何帮助? – nonamelive 2010-12-22 16:12:17

更高版本OpenGL支持压缩纹理。您可以在上传纹理数据时使用OpenGL对其进行压缩,或者自己完成并将预压缩数据提供给OpenGL。

在OpenGL 压缩纹理支持的ARB规范http://www.opengl.org/registry/specs/ARB/texture_compression.txt

这里还有一个特别的压缩格式的描述 http://www.opengl.org/registry/specs/ARB/texture_compression_rgtc.txt

而具体到OpenGL ES的这种压缩格式: http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt