FBO进行多纹理拼接

有个需求,要将多张纹理拼接到一张,可以设定拼接区域;

由于OGLES2.0,不支持glReadBudder(), glblitframebuffer().

就考虑采用Render to Texture的方式

(1)创建一张空Texture用于保存最终结果 defualtTex。

       glGenTextures(1, &defualtTex);
glBindTexture(GL_TEXTURE_2D, defualtTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, a_iWidth, a_iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

(2)初始化FBO

glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,defualtTex, 0);
// now got back to the default render context
glBindFramebuffer(GL_FRAMEBUFFER, 0);

(3)每次往FBO指定区域绘制一次纹理

//以下根据需求多次调用

glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_TesTex.GetTextureID(), 0);
Draw(tex);// 此处为绘制的纹理,

       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

(4)这样多张纹理就绘制到defualtTex中了,可以保存出来,也可以直接作为纹理渲染显示

unsigned char* output_image = new unsigned char[M_WIDTH * M_HEIGHT * 3];

        glBindTexture(GL_TEXTURE_2D,defualtTex);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, output_image);


此时output_image保存了defualtTex中的数据,可以保存出看


代码稍后上传~

你可能感兴趣的:(FBO进行多纹理拼接)