在android/opengles2中使用fbos显示高斯模糊的多过程着色器时出现的问题

w8f9ii69  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(252)

环境为android/opengles2.0
我正在使用水平和垂直过程将二维高斯模糊重构为2个一维模糊,如中所述:http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/. 这是从一个surfacetexture模糊视频。
我知道这可以通过帧缓冲区来实现,并且已经通过所需的步骤实现了这些,但是我无法显示模糊。
任何帮助将不胜感激,日志删除代码的清晰度。
帧缓冲区设置,调用setupframebuffers():

  1. int[] newFbo = new int[2];
  2. int[] newTex = new int[2];
  3. void setupFramebuffers() {
  4. createFramebuffer(newFbo, newTex, 0);
  5. createFramebuffer(newFbo, newTex, 1);
  6. GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  7. }
  8. void createFramebuffer(int[] fbo, int[] fbtex, int offset) {
  9. GLES20.glGenTextures(1, fbtex, offset);
  10. GLES20.glBindTexture(GL_TEXTURE_2D, fbtex[offset]);
  11. GLES20.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, null);
  12. GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  13. GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  14. GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  15. GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  16. GLES20.glGenFramebuffers(1, fbo, offset);
  17. GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[offset]);
  18. GLES20.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex[offset], 0);
  19. int status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
  20. switch (status) {
  21. case GL_FRAMEBUFFER_COMPLETE:
  22. break;
  23. case GL_FRAMEBUFFER_UNSUPPORTED:
  24. return;
  25. default:
  26. return;
  27. }
  28. }

在draw方法中,调用generatefbos():

  1. private synchronized void drawToWindow() {
  2. for(ShaderSurfaceView shaderSurfaceView : this.shaderSurfaceViewsToRender) {
  3. if(!shaderSurfaceView.getStopRendering()) {
  4. final int program = shaderSurfaceView.getProgramId();
  5. final WindowSurface windowSurface = shaderSurfaceView.getWindowSurface();
  6. if (windowSurface == null) {
  7. continue;
  8. }
  9. windowSurface.makeCurrent();
  10. GLES20.glViewport(0, 0, windowSurface.getWidth(), windowSurface.getHeight());
  11. generateFbos(shaderSurfaceView, windowSurface);
  12. GLES20.glEnable(GLES20.GL_BLEND);
  13. windowSurface.swapBuffers();
  14. }
  15. }
  16. }

生成BOS:

  1. void generateFbos(ShaderSurfaceView shaderSurfaceView, WindowSurface windowSurface) {
  2. final int positionHandle = shaderSurfaceView.getPositionHandle();
  3. final int textureHandle = shaderSurfaceView.getTextureHandle();
  4. final int mvpMatrixHandle = shaderSurfaceView.getMvpMatrixHandle();
  5. final int stMatrixHandle = shaderSurfaceView.getStMatrixHandle();
  6. final int verticalProgramId = shaderSurfaceView.getVerticalProgramId();
  7. final int horizontalProgramId = shaderSurfaceView.getHorizontalProgramId();
  8. // create the mesh
  9. this.triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
  10. GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, this.triangleVertices);
  11. GLES20.glEnableVertexAttribArray(positionHandle);
  12. this.triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
  13. GLES20.glVertexAttribPointer(textureHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, this.triangleVertices);
  14. GLES20.glEnableVertexAttribArray(textureHandle);
  15. Matrix.setIdentityM(this.mvpMatrix, 0);
  16. GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, this.mvpMatrix, 0);
  17. GLES20.glUniformMatrix4fv(stMatrixHandle, 1, false, this.stMatrix, 0);
  18. GLES20.glBindTexture(GL_TEXTURE_2D, textureHandle);
  19. GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, newFbo[0]);
  20. GLES20.glUseProgram(verticalProgramId);
  21. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  22. GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, newFbo[1]);
  23. GLES20.glBindTexture(GL_TEXTURE_2D, newTex[0]);
  24. GLES20.glUseProgram(horizontalProgramId);
  25. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  26. GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // Re-enable the default window-system framebuffer for drawing
  27. GLES20.glBindTexture(GL_TEXTURE_2D, newTex[0]);
  28. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题