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

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

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

int[] newFbo = new int[2];
int[] newTex = new int[2];

void setupFramebuffers() {
            createFramebuffer(newFbo, newTex, 0);
            createFramebuffer(newFbo, newTex, 1);
            GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
        }

    void createFramebuffer(int[] fbo, int[] fbtex, int offset) {
            GLES20.glGenTextures(1, fbtex, offset);
            GLES20.glBindTexture(GL_TEXTURE_2D, fbtex[offset]);
            GLES20.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, null);
            GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            GLES20.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            GLES20.glGenFramebuffers(1, fbo, offset);
            GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[offset]);
            GLES20.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex[offset], 0);
            int status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
            switch (status) {
                case GL_FRAMEBUFFER_COMPLETE:
                    break;
                case GL_FRAMEBUFFER_UNSUPPORTED:
                    return;
                default:
                    return;
            }
        }

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

private synchronized void drawToWindow() {
        for(ShaderSurfaceView shaderSurfaceView : this.shaderSurfaceViewsToRender) {
            if(!shaderSurfaceView.getStopRendering()) {
                final int program = shaderSurfaceView.getProgramId();

                final WindowSurface windowSurface = shaderSurfaceView.getWindowSurface();

                if (windowSurface == null) {
                    continue;
                }

                windowSurface.makeCurrent();

                GLES20.glViewport(0, 0, windowSurface.getWidth(), windowSurface.getHeight());
                generateFbos(shaderSurfaceView, windowSurface);
                GLES20.glEnable(GLES20.GL_BLEND);

                windowSurface.swapBuffers();
            }
        }
    }

生成BOS:

void generateFbos(ShaderSurfaceView shaderSurfaceView, WindowSurface windowSurface) {
        final int positionHandle = shaderSurfaceView.getPositionHandle();
        final int textureHandle = shaderSurfaceView.getTextureHandle();
        final int mvpMatrixHandle = shaderSurfaceView.getMvpMatrixHandle();
        final int stMatrixHandle = shaderSurfaceView.getStMatrixHandle();
        final int verticalProgramId = shaderSurfaceView.getVerticalProgramId();
        final int horizontalProgramId = shaderSurfaceView.getHorizontalProgramId();

        // create the mesh
        this.triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
        GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, this.triangleVertices);
        GLES20.glEnableVertexAttribArray(positionHandle);
        this.triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
        GLES20.glVertexAttribPointer(textureHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, this.triangleVertices);
        GLES20.glEnableVertexAttribArray(textureHandle);

        Matrix.setIdentityM(this.mvpMatrix, 0);
        GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, this.mvpMatrix, 0);
        GLES20.glUniformMatrix4fv(stMatrixHandle, 1, false, this.stMatrix, 0);

        GLES20.glBindTexture(GL_TEXTURE_2D, textureHandle);
        GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, newFbo[0]);

        GLES20.glUseProgram(verticalProgramId);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, newFbo[1]);
        GLES20.glBindTexture(GL_TEXTURE_2D, newTex[0]);

        GLES20.glUseProgram(horizontalProgramId);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // Re-enable the default window-system framebuffer for drawing
        GLES20.glBindTexture(GL_TEXTURE_2D, newTex[0]);

    }

暂无答案!

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

相关问题