Search code examples
androidopengl-es-2.0render-to-texture

Avoid display of offscreen texture


I have an android mediaplayer application. I have used an offscreen texture attached to FBO that uses shader program 1 and has an onscreen texture which uses shader program 2.

For simplicity, assume my shader1 outputs red color. (R=1). My second shader reads r value from first shader and makes green = 1. So in effect I should get r+g = yellow.

My problem is that for the first split second, I get red screen (from offscreen texture) and suddenly it becomes yellow. That red for a split second at start is an issue in my real scenario. Is it possible to solve it.?

function OnSurfaceChanged :

// 1st texture:

int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
mTexture1 = textures[0];
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexture1);

// 2nd texture

int[] textures2d = new int[1];
GLES20.glGenTextures(1, textures2d, 0);
mTexture2 = textures2d[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture2);

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,
                width, height, 0, GLES20.GL_RGBA,
                GLES20.GL_UNSIGNED_BYTE, null);

// FBO
int handle2[] = { 0 };
GLES20.glGenFramebuffers(1, handle2, 0);
mFrameBufferHandle2 = handle2[0];
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBufferHandle2);

GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
                GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
                mTexture2, 0);

surface1 = new SurfaceTexture(mTexture1);
... pass it to mediaplayer

function onDrawFrame :

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

// Shader 1
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBufferHandle2);
GLES20.glViewport(0, 0, mViewportWidth, mViewportHeight);
glUseProgram(shader1);
.....
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexture1);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

// Shader 2
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glViewport(0, 0, mViewportWidth, mViewportHeight);
glUseProgram(shader2);
...
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture2);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

Solution

  • Could solve it by adding

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

    after GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);