一种是直观的使用glGetTexImage,然后获取数据到内存再贴上去,这种方法很慢
另一种是使用FBO,将纹理附加到FBO,然后用glCopyTexSubImage2D,不涉及跟内存交换数据,速度飞快。
方法一:获取纹理再拷贝
-
unsigned char * data = new unsigned char[normal_texture.width*normal_texture.Hight*4]; -
glBindTexture(GL_TEXTURE_2D,src_id); -
glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,data); -
glBindTexture(GL_TEXTURE_2D,dest_id); -
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,data); -
glBindTexture(GL_TEXTURE_2D,0);
第二种方法:FBO拷贝
-
GLuint fbo; -
glGenFramebuffers(1,&fbo); -
/// bind the FBO -
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo); -
/// attach the source texture to the fbo -
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, -
GL_TEXTURE_2D, src_id, 0); -
/// bind the destination texture -
glBindTexture(GL_TEXTURE_2D, dest_id); -
/// copy from framebuffer (here, the FBO!) to the bound texture -
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width,height); -
/// unbind the FBO -
glBindFramebuffer(GL_FRAMEBUFFER, 0); -
glBindTexture(GL_TEXTURE_2D,0);
