您可以将第一个通道渲染到 FBO,然后在将第二个通道渲染到默认帧缓冲区时使用生成的纹理。
如果您要模糊的图像位于具有名称的纹理中inputTexId
,以下是代码外观的概述。您没有指定是使用 C++ 还是 Java。下面使用 C++ 绑定,但在 Java 中看起来非常相似。
一旦在设置过程中,您创建了一个 FBO 和一个将用于包含第一个渲染通道结果的纹理:
GLuint pass1TexId = 0;
glGenTextures(1, &pass1TexId);
glBindTexture(GL_TEXTURE_2D, pass1TexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, pass1TexId, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
每次您想要应用模糊滤镜时,您都可以使用此 FBO 作为第一个渲染通道的渲染目标,并将原始图像作为输入:
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glBindTexture(GL_TEXTURE_2D, inputTexId);
// Set up shaders and state for first blur pass, and render.
然后,对于第二遍,渲染到默认帧缓冲区,并使用第一遍生成的纹理作为输入:
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, pass1TexId);
// Set up shaders and state for second blur pass, and render.