将纹理转换为GL_COMPRESSED_RGBA

Convert texture to GL_COMPRESSED_RGBA

本文关键字:COMPRESSED RGBA GL 纹理 转换      更新时间:2023-10-16

我正在寻找如何将GL_RGBA帧缓冲纹理转换为GL_COMPRESSED_RGBA纹理,最好是在 GPU 上。帧缓冲显然不能具有GL_COMPRESSED_RGBA的内部格式,因此我需要一种转换方法。

请参阅此描述 OpenGL 纹理压缩的文档。步骤的顺序就像(这很笨拙 - 整个纹理的缓冲区对象会有所改善)

GLUint mytex, myrbo, myfbo;
glGenTextures(1, &mytex);
glBindTexture(GL_TEXTURE_2D, mytex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA, width, height, 0,
    GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glGenRenderbuffers(1, &myrbo);
glBindRenderbuffer(GL_RENDERBUFFER, myrbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height)
glGenFramebuffers(1, &myfbo);
glBindFramebuffer(GL_FRAMEBUFFER, myfbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
    GL_RENDERBUFFER, myrbo);
// If you need a Z Buffer:
// create a 2nd renderbuffer for the framebuffer GL_DEPTH_ATTACHMENT 
// render (i.e. create the data for the texture)
// Now get the data out of the framebuffer by requesting a compressed read
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA,
    0, 0, width, height, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glDeleteRenderbuffers(1, &myrbo);
glDeleteFramebuffers(1, &myfbo);
// Validate it's compressed / read back compressed data
GLInt format = 0, compressed_size = 0;
glGetTexLevelParameteri(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
glGetTexLevelParameteri(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, 
char *data = malloc(compressed_size);
glGetCompressedTexImage(GL_TEXTURE_2D, 0, data);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTexture(1, &mytex);
// data now contains the compressed thing

如果您使用PBO对象作为纹理,则可以在没有malloc()的情况下逃脱。

如果您想在不

传输到 CPU 的情况下在 GPU 上执行压缩 - 这里有两个示例,您可以重新用于 OpenGL(它们基于 DX)

  1. GPU 加速纹理压缩
  2. GPU 加速纹理压缩 2

希望这有帮助!