4
votes

In a paint app I am developing, I want my user to be able to draw with a transparent brush, for example black paint over white background should result in grey colour. When more paint is applied, the resulting colour will be closer to black.

enter image description here

However no matter how many times I draw over the place, the resulting colour never turnts black; in fact it stops changing after a few lines. Photoshop says that the alpha of the blob drawn on the left in OpenGL is max 0.8, where I expect it to be 1.

My app works by drawing series of stamps as in Apple's GLPaint sample to form a line. The stamps are blended with the following function:

glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);

My fragment shader:

uniform lowp sampler2D u_texture;
varying highp vec4 f_color;

void main(){
    gl_FragColor = texture2D(u_texture, gl_PointCoord).aaaa*f_color*vec4(f_color.aaa, 1);
}

How should I configure the blending in order to get full colour when drawing repeatedly?

Update 07/11/2013

Perhaps I should also note that I first draw to a texture, and then draw the texture onscreen. The texture is generated using the following code:

    glGenFramebuffers(1, &textureFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, textureFramebuffer);

    glGenTextures(1, &drawingTexture);
    glBindTexture(GL_TEXTURE_2D, drawingTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  pixelWidth, pixelHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

Update 02/12/2013 I tried modifying Apple's GLPaint program to and it turned out that this behaviour is observable only on iOS7. As it can be seen on the screenshots bellow, the colours on iOS 7 are a bit pale and don't blend nicely. The

GL_ONE, GL_ONE_MINUS_SRC_ALPHA

blend function does well on iOS6. Can this behaviour be caused by iOS7's implementation of CALAyer or something else and how do I solve it?

iOS6iOS7

Update 10/07/2014

Apple recently updated their GLPaint sample for iOS7 and the issue is observable there, too. I made a separate thread based on their code: Unmodified iOS7 Apple GLPaint example blending issue

1
Have you tried using a higher precision sampler? If I am not mistaken mediump is the default declaration for a sampler uniform.Andon M. Coleman
@AndonM.Coleman I just tried both medium and highp, no effect. I guess lowp is enough for this application.Hristo
This is exactly the problem I am seeing (or very similar). Have you found any solution or made any progress? SktetchBookX for iPhone does exactly what I want.The Way
Unfortunately, I have not found any solutions yet. I will look into the issue again soon. I believe it is something really simple that changed with ios7. Meanwhile, if you have any solutions or suggestions, please let me know.Hristo

1 Answers

1
votes

Just because your brush does "darken" the image doesn't mean, that this was subtractive blending. This is in face regular additive blending, where the black brush merely overdraws the picture. You want a (GL_ONE, GL_ONE_MINUS_SRC_ALPHA) blending function (nonseparated). The brush is contained only within the alpha channel of the texture, there's no color channels in the texture, the brush color is determined by glColor or an equivalent uniform.

Using the destination alpha value is not required in most cases.