Android:使用SurfaceTexture渲染到FrameBuffer

时间:2016-02-21 05:30:17

标签: android opengl-es textures glsurfaceview surface

我正在研究需要双通道渲染的效果(即连续应用两个单独的着色器)。我正在使用带有自定义渲染器的GLSurfaceView。

为了达到这个效果,我首先尝试渲染到FrameBuffer,然后渲染到GL_TEXTURE_2D以便在GLSurfaceView中显示。

OnSurfaceCreated方法中的相关代码如下:

        GLES20.glGenTextures(1, this.textureID, 0);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES,this.textureID[0]);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);

        GLES20.glGenTextures(1, textures2D, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures2D[0]);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

        GLES20.glGenFramebuffers(1, fboId, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[0]);
        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textures2D[0], 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

OnDrawFrame中的相关代码是:

       synchronized (this) {
            if (this.updateSurface) {
                this.surfaceTexture.updateTexImage();
                this.surfaceTexture.getTransformMatrix(this.stMatrix);
                this.updateSurface = false;
            }
        }

        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId[0]);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, this.textureID[0]);

// apply the first shader

        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures2D[0]);

// apply the second shader

        GLES20.glFinish();

然而,由于glError 1286,程序崩溃,这似乎表明无效的帧缓冲操作。我不确定是什么导致了这个问题,但我怀疑它可能与SurfaceTexture与GL_TEXTURE_EXTERNAL_OES纹理相关联的事实有关,而FrameBuffer正在输出GL_TEXTURE_2D纹理。

任何帮助或建议将不胜感激。谢谢。

0 个答案:

没有答案