【问题标题】:How Can I Compress a Texture in OpenGL ES on iPhone/iPad?如何在 iPhone/iPad 上的 OpenGL ES 中压缩纹理?
【发布时间】: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。但我不知道如何实现这一点。有什么帮助吗?

标签: iphone ipad opengl-es


【解决方案1】:

更高版本的 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

【讨论】:

  • 我不相信旧的 OpenGL 参考适用于 OpenGL ES,所以这些在这种情况下可能没有用。 iPhone 专门使用 PowerVR 纹理压缩,所以我不确定 Ericsson Texture Compression 的最后一个参考是否适用于此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
相关资源
最近更新 更多