how can i use glblendfuc to blend two image?

481 views Asked by At

I want to blend two texture in my shader,so my code in shader like this:

out vec4 fragColor1;
out vec4 fragColor2;
void main(){
fragColor1=vec4(1.0,0.0,0.0,1.0);
fragColor2=vec4(0.0,1.0,0.0,1.0);
}

when i use

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

it only show red on the screen,but when i use

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

it can't show green,it shows black,I don't know what's wrong and how to fix it...

1

There are 1 answers

2
Rabbid76 On BEST ANSWER

You've a basic misunderstanding, if you want to blend the outputs of a fragment shader, then you would have to use Dual Source Blending.
Blending, would combines the consecutive fragment shader outputs for a fragment, when multiple geometries are rendered consecutively.
Furthermore, the default framebuffer just has one color buffer, so it makes no sense to wirte to multiple targets in the framebuffer.
If you can access the colors of both textures in the fragment shader, then you don't need any Blending at all, because you can mix the colors in the fragment shader:

out vec4 fragColor;

void main(){
    vec4 color1 = ...;
    vec4 color2 = ...;
    float a = 0.5;

    fragColor = mix(color1, color2, a);
}

Note, alternatively a can be set by the alpha channel of an image (e.g. a = color2.a).