将纹理转换为GL_COMPRESSED_RGBA

时间:2012-05-02 11:08:13

标签: c++ opengl

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

2 个答案:

答案 0 :(得分:5)

请参阅描述OpenGL纹理压缩的this document。步骤的顺序就像(这是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()的情况下离开。

答案 1 :(得分:4)

如果你想在没有传输到CPU的情况下在GPU上执行压缩 - 这里有两个样本你可以为OpenGL重新调整用途(它们是基于DX的)

  1. GPU accelerated texture compression
  2. GPU accelerated texture compression 2
  3. 希望这有帮助!