OpenGL ES 帧缓冲区位块传送
帧缓冲区位块传送(Blit)也是 OpenGL ES 3.0 的新特性,主要用于帧缓冲区之间的像素拷贝,性能高且使用方便,可以指定缓冲区任意矩形区域的像素拷贝。
本文的绘制流程是,新建一个 FBO 绑定纹理作为颜色附着,然后绑定该帧缓冲区进行一次离屏渲染,最后绑定渲染到屏幕的缓冲区作为渲染缓冲区,从新的帧缓冲区中拷贝像素。
渲染代码:
//首先获取当前默认帧缓冲区的 id
GLint defaultFrameBuffer = GL_NONE;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFrameBuffer);
//绑定我们新创建的帧缓冲区进行渲染
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glViewport ( 0, 0, m_RenderImage.width, m_RenderImage.height);
glClear(GL_COLOR_BUFFER_BIT);
glDrawBuffers(ATTACHMENT_NUM, attachments);
glUseProgram (m_ProgramObj);
glBindVertexArray(m_VaoId);
UpdateMVPMatrix(m_MVPMatrix, 0, m_AngleY, (float)screenW / screenH);
glUniformMatrix4fv(m_MVPMatLoc, 1, GL_FALSE, &m_MVPMatrix[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_TextureId);
glUniform1i(m_SamplerLoc, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (const void *)0);
//绑定默认的帧缓冲区对象,将像素从新建的帧缓冲区拷贝到当前默认的帧缓冲区
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, defaultFrameBuffer);
glViewport ( 0, 0, m_SurfaceWidth, m_SurfaceHeight);
glClear(GL_COLOR_BUFFER_BIT);
//位块传送
BlitTextures();
进行帧缓冲区间位块传之前,需要指定好源帧缓冲区 GL_READ_FRAMEBUFFER 和目标帧缓冲区 GL_DRAW_FRAMEBUFFER,下面代码实现是将四个颜色附着对应的缓冲区像素,分别拷贝到当前渲染缓冲区中的 1/4 矩形区域内:
void FBOBlitSample::BlitTextures() {
//很重要,指定源帧缓冲区
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_FBO);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
0, 0, m_SurfaceWidth/2, m_SurfaceHeight/2,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
m_SurfaceWidth/2, 0, m_SurfaceWidth, m_SurfaceHeight/2,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT2);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
0, m_SurfaceHeight/2, m_SurfaceWidth/2, m_SurfaceHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT3);
glBlitFramebuffer(0, 0, m_RenderImage.width, m_RenderImage.height,
m_SurfaceWidth/2, m_SurfaceHeight/2, m_SurfaceWidth, m_SurfaceHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
最后呈现的效果是将四张图像渲染到一个屏幕上。
进技术交流群,添加我的微信:Byte-Flow
本文来自作者投稿,版权归原作者所有。如需转载,请注明出处:https://www.nxrte.com/jishu/18004.html