1

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...

isaiah.li
  • 35
  • 5
  • my question is similar to this one:https://stackoverflow.com/questions/15739256/using-gl-src1-color-in-glblendfunc , but it doesn't seem to be solved – isaiah.li Mar 23 '20 at 09:29

1 Answers1

0

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).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • "*Blending doesn't combines the colors of multiple fragment shader outputs.*" Technically, dual-source blending can ;) – Nicol Bolas Mar 23 '20 at 14:45
  • thanks,I know that i can use mix, I just wanna try dual source blending, i set the output like this `layout(location = 0, index = 0) out vec4 outputColor0;` `layout(location = 0, index = 1) out vec4 outputColor1;`,but the display is still as I described above,I'm a little confused.. – isaiah.li Mar 24 '20 at 03:46