Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL glBlendFuncSeparate

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:

//Background 
...code...
    glBlendFunc(GL_ONE, GL_ZERO);
...code

//Mask
...code...
    glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ZERO);
...code...

//Foreground
...code
    glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
...code

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.

My current result's visualization bellow.


Background:
enter image description here

Mask (circle is transparent):
enter image description here

Foreground:
enter image description here

Result:
enter image description here


And I want it to work like this:

Background:
enter image description here

Mask (circle is white, background is black):
enter image description here

Foreground:
enter image description here

Result:
enter image description here


So that later it could be used like this:

Background:
enter image description here

Mask (circle is white, background is black):
enter image description here

Foreground:
enter image description here

Result:
enter image description here


Attempt with @Gigi solution:
enter image description here

like image 398
hockeyman Avatar asked Jul 18 '12 14:07

hockeyman


1 Answers

Perhaps this is what you want:


1) Clear the destination image:

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

2) Draw the background, masking out the alpha channel:

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);

3) Draw the "masking overlay", masking out the color channels:

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);

4) Draw the foreground, enabling blending:

glEnable(GL_BLEND);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE, GL_ZERO);

Note: The overlay image must have the alpha channel specified.

like image 71
Gigi Avatar answered Sep 20 '22 12:09

Gigi