Question

I have a problem with multiple drawing of textures in my program.

Blending mode is

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

The value of a-channel is passed into the shader from cpu-code.

precision highp float;

uniform sampler2D tShape;

uniform vec4      vColor;
uniform float     sOpacity;

varying vec4 texCoords;\n"

void main() {
    float a = texture2D(tShape, texCoords.xy).x * sOpacity;
    gl_FragColor = vec4(vColor.rgb, a);
}

It's calculated previously with

O = pow(O, 1.3);

for the best visual effect.

I draw with color (0; 0; 0) on the black transparent canvas (0;0;0;0), but with very low opacity:

0.03 -> 0.01048
0.06 -> 0.0258
0.09 -> 0.0437
0.12 -> 0.0635
...

I expect, that maximal value of point's color will be (0;0;0;1) (black, no transparent) after multiple drawings as on the simulator: enter image description here

but it isn't so on the device: enter image description here

Do you have any ideas, why is it so?

UPDATE:

Also manual blending works incorrect too (and with difference from standard).

glBlendFunc(GL_ONE, GL_ZERO);

Fragment shader code:

#extension GL_EXT_shader_framebuffer_fetch : require
precision highp float;

uniform sampler2D tShape;

uniform vec4      vColor;
uniform float     sOpacity;

varying vec4 texCoords;

void main() {
    float a = texture2D(tShape, texCoords.xy).x * sOpacity;
    gl_FragColor = vec4(vColor.rgb * a, a) + (gl_LastFragData[0] * (1.0 - a));
}

Result on the simulator: enter image description here

Result on the device: enter image description here

Was it helpful?

Solution 2

After some experiments I've understood, that this problem is in supported precision of device . So on the iPad Air this problem appears less than on iPad 4, 3.

OTHER TIPS

I'm trying to understand your approach so I wrote down some equations:

This is how a new drawing is performed (If didn't make any mistake):

color = {pencil_shape}*sourceAlpha + {old_paint}*(1-sourceAlpha)
alpha = {pencil_shape} + {old_paint}*(1-sourceAlpha)

So basically you alpha is getting closer to 1 on each frame, and you color is blended each time based on the src alpha in the *pencil_shape*.

Questions:

  • Do you intend to use the alpha in the output image for anything?
  • Is your *pencil_shape* all black (0, 0, 0, 0)? (besides the cornes where I suppose it has some antialiasing effect)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top