How Can I Compress a Texture in OpenGL ES on iPhone/iPad?
I'm making an iPad app which needs OpenGL to do a flip animation. I have a front image texture and a back image texture. Both the two textures are screenshots.
// Capture an image of the screen
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContex开发者_JS百科t()];
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);
Each of the texture takes up about 8 MB of memory, which is unacceptable for an iPhone/iPad app. Could anyone tell me how I can compress the texture to reduce memory usage?
Later versions OpenGL support compresses textures. You can have them being compressed by OpenGL upon uploading the texture data, or do it yourself and provide the pre-compressed data to OpenGL.
The ARB specification for compressed texture support in OpenGL http://www.opengl.org/registry/specs/ARB/texture_compression.txt
And here's the description of one particular compression format http://www.opengl.org/registry/specs/ARB/texture_compression_rgtc.txt
And specific to OpenGL ES this compression format: http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt
精彩评论