Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenGL, is there a way to blend based on a separate channel's value in the shader?

In OpenGL (not ES), is there a universal way to blend based a texture while drawing based on another texture or variable's value? On OpenGLES, I know that I can do custom blending on some platforms via extensions like GL_EXT_shader_framebuffer_fetch. The reason I ask, is that I have a special texture where the forth channel is not alpha, and I need to be able to blend it on a separate alpha which is available on a different map.

like image 826
David Avatar asked Dec 14 '25 15:12

David


1 Answers

You want dual-source blending, which is available in core as of OpenGL 3.3. This allows you to provide a fragment shader with two outputs and use both of them in the blend function.

You would declare outputs in the fragment shader like this:

layout(location = 0, index = 0) out vec4 outColor;
layout(location = 0, index = 1) out vec4 outAlpha;

You could then set the blending function like this, for premultiplied alpha:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC1_COLOR);

Or non-premultiplied alpha:

glBlendFunc(GL_SRC1_COLOR, GL_ONE_MINUS_SRC1_COLOR);

Note that SRC1 here is the second output of the fragment shader. If I remember correctly, this blending will only work for one location.

like image 78
Dietrich Epp Avatar answered Dec 16 '25 06:12

Dietrich Epp