3

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

4

2 回答 2

5

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

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().

于 2012-05-02T14:47:23.230 回答
4

如果您想在 GPU 上执行压缩而不传输到 CPU - 这里有两个示例您可以重新用于 OpenGL(它们是基于 DX 的)

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

希望这可以帮助!

于 2012-05-02T13:39:34.643 回答