2

I want to do additive blending on camera preview's surface texture binded to my opengl context.

I am getting weirdly rendered texture when i enable blending(20 noisy squares getting rendered), if I call GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); preview is proper but i lose my additive blending as i have cleared the buffer!

With glClear call enter image description here

Without glClear call enter image description here

I have no clue whats the problem is, i am newbie to opengl-es, Any suggestions? If any piece of code is needed i can provide to better understand the issue.

putting relevant code only. Ask for any other part of code if necessary.

public void onSurfaceCreated(GL10 unused, EGLConfig config) {

//compile shader, link program.... etc omitted for brevity 

//enable additive blending
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE);
GLES20.glBlendEquation(GLES30.GL_MAX);
}

public void onDrawFrame(GL10 unused) {
// Do not want to do this, but if i do preview is normal but i lose my blending
//GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);    
surfaceTexture.updateTexImage();
    GLES20.glUseProgram(_onscreenShader); 

    int th = GLES20.glGetUniformLocation(_onscreenShader, "sTexture");
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, _rawVideoTexture); 
    GLES20.glUniform1i(th, 0);
    GLES20.glVertexAttribPointer(_onscreenPositionAttribute, 2, GLES20.GL_FLOAT, false, 4 * 2, pVertex);
    GLES20.glVertexAttribPointer(_onscreenUVAttribute, 2, GLES20.GL_FLOAT, false, 4 * 2, pTexCoord);
    GLES20.glEnableVertexAttribArray(_onscreenPositionAttribute);
    GLES20.glEnableVertexAttribArray(_onscreenUVAttribute);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 
}
2
  • I wonder not even a single android developer giving it a try! Nov 10, 2014 at 17:55
  • Maybe if you show us some more code...
    – Jaa-c
    Nov 10, 2014 at 19:25

1 Answer 1

0

Most Android devices have a PowerVR, Mali, Adreno or Vivante GPU core which are all deferred, tiled-based renderers. This architecture requires the glClear operation at the start of each frame to tell the OpenGL ES driver when to clear internal triangle binning queues as well as the frame buffer. If you don't do the glClear, this caching design does not work properly and you will get weird results that will differ from one GPU type to another. So, you really must do the glClear.

2
  • I do agree results may vary. But even if i dont do glClear and disable blending then also it is woking, now thats disturbing why enabling blending not working properly Nov 12, 2014 at 2:44
  • Because if you disable blending, you're telling the GPU to ignore the current contents of the framebuffer and overwrite them with new results. In that case it doesn't really matter if you clear the previous buffer or not, because its contents won't have an effect on the final result. Jun 22, 2015 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.