opengl-es-2.0glblendfunc

OpenGL ES 2.0 blending


I set glBlendFunc to

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

and for testing purposes I set the color in my fragment shader like this

gl_FragColor = vec4(1.0,0.0,0.0,0.0);

Shouldn't the object be fully transparent? What could the reason be if it's not?


Solution

  • The first argument of glBlendFunc() is the source factor, the second is the destination factor. In your case:

    sfactor = 1.0;
    dfactor = 1.0 - src.alpha;
    

    being src.alpha = 0.0, from your gl_FragColor:

    sfactor = 1.0;
    dfactor = 1.0;
    

    So the color put to the buffer will be:

    buffer = sfactor * src + dfactor * dst;
    

    Substituting...

    buffer = (1.0,0.0,0.0,0.0) + dst;
    

    So, putting it simple, you are adding 1 to the red channel of the existing buffer.

    If you want to make the output fully transparent, the usual function is:

    glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    

    The one you wrote is usually used for pre-multiplied alpha in the source. But (1, 0, 0, 0) is obviously not a premultiplied alpha value!