问题
In order to create a blur effect the theory says to create 2 vertex shaders, one for the horizontal pass, and the second for the vertical pass. And then one fragment shader for actual sampling.
My question is, how to actually to execute the 2 vertex shaders? Do I need to render, then get back the pixels via the glReadPixels and then to render again?
My environment is Android, OpenGL ES 2.0
Thanks
回答1:
You can render the first pass to an FBO, and then use the resulting texture when rendering the second pass to the default framebuffer.
If the image you want to blur is in a texture with name inputTexId
, the following is an outline of how the code could look. You didn't specify if you use C++ or Java. The following uses C++ bindings, but it will look very similar in Java.
Once, during setup, you create an FBO and a texture that will be used to contain the result of the first rendering pass:
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);
Every time you want to apply the blur filter, you use this FBO as the render target for the first render pass, with your original image as input:
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glBindTexture(GL_TEXTURE_2D, inputTexId);
// Set up shaders and state for first blur pass, and render.
Then for the second pass, you render to the default framebuffer, and use the texture produced by the first pass as input:
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, pass1TexId);
// Set up shaders and state for second blur pass, and render.
来源:https://stackoverflow.com/questions/28137625/how-to-execute-2-passes-for-a-blur-effect-in-opengl-es-2-0