glBlendFunc and glBlendFuncSeparate (white = alpha 0, black = alpha 1)

I need some help with OpenGL textures masking. I have it working but need to find some other blending function parameters to work in other way. Now I have

    glBlendFunc(GL_ONE, GL_ZERO);

    glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ZERO);


Now it sets foreground’s opacity to 0 (fills with background texture) where mask is transparent. I need it to react to mask’s colors. I mean something like setting foregrounds opacity depending on mask’s color. For example if mask is black (0.0,0.0,0.0) then the opacity of that place in foreground is 0 (is filled with background), and if mask is white (1.0,1.0,1.0) then the opacity of foreground is 1 (not filled with background). It can be in reverse consequence (white = opacity 0, black = opacity 1). I just need it to work depending on color.


[li]1st column is my current result. Circle in mask is transparent.[/li][li]2nd column is example of result I am trying to get. Circle in mask is white.[/li][li]3rd column is example of why I want to get it working just like as I said (white mask color = foreground alpha 0, black mask color = foreground alpha 1 or reverse)[/li][/ol]|
[li]1st row is background[/li][li]2nd row is mask[/li][li]3rd row is foreground[/li][li]4th row is result[/li][/ol]||

I got some info that I should use shaders. And it was said to me that this code should help me out:

uniform sampler2D backTex,maskTex,foreText;

void main()
   float mask;
   vec4 result;

   mask = texture2D(maskTex,gl_TexCoord[0].st).r;
   result = texture2D(backTex,gl_TexCoord[0].st) * (1 - mask);
   result = result + texture2D(foreTex,gl_TexCoord[0].st) * mask;

   result.a = 1;

   gl_FragColor = result;

But I have no idea how to implement it to my code (objective-c)