Search code examples
openglglsl

Does blending work with the GLSL mix function


These are my blending functions.

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);

I mix two textures in the fragment shader using mix function.

FragColor = 
    mix(texture(texture1, TexCoord), texture(texturecubeMap, ransformedR), MixValueVariables.CubeMapValue ) * 
    vec4(result, material.opacity / 100.0); // multiplying texture with material value

Despite changing the blend Equation or BlendFunc types i don't see any difference in the output.

Does mix function works with the blend types.


Solution

  • Does mix function works with the blend types.

    No. Blending and the glsl function mix are completely different things.

    The function mix(x, y, a) calculates always x * (1−a) + y * a.

    Blending takes the fragment color outputs from the Fragment Shader and combines them with the colors in the color buffer of the frame buffer.

    If you want to use Blending, then you've to

    • draw the geometry with the first texture
    • activate blending
    • draw the geometry with the second texture

    If you just want to blend the textures in the fragment shader, then you've to use the glsl arithmetic. See GLSL Programming/Vector and Matrix Operations

    e.g.

    vec4 MyBlend(vec4 source, vec4 dest, float alpha)
    {
        return source * alpha + dest * (1.0-alpha);
    }
    
    void main()
    {
        vec4 c1 = texture(texture1, TexCoord);
        vec4 c2 = texture(texturecubeMap, ransformedR);
        float f = MixValueVariables.CubeMapValue
    
        FragColor = MyBlend(c1, c2, f) * vec4(result, material.opacity / 100.0); 
    }
    

    Adapt the function MyBlend to your needs.