meno's picture

photoshop like blending

hi
i started recently to use opentk and learn a bit opengl. im not very good at this yet but with the help of some pages of this site i managed to draw pictures on the screen (yeah).

now i want to implement photoshop like blending modes (multiply, screen, lighten etc).

currently im using this code:

                GL.Enable(EnableCap.Blend);
                GL.Color4(color.R, color.G, color.B, color.A);
 
 
                BlendingFactorSrc src = BlendingFactorSrc.SrcAlpha;
                BlendingFactorDest dest = BlendingFactorDest.OneMinusSrcAlpha;
                BlendEquationMode mode = BlendEquationMode.FuncAdd;
 
                switch(blendMode) {
                    default:
                        // Blendmode alpha (default)
                        break;
                }
 
                GL.BlendEquation(mode);
                GL.BlendFunc(src,dest);

this works fine for alpha blending. i can use the alpha values stored in the picture and modify color and alpha in the program.
so now i want implement for example the multiply mode which is sourceColor * destinationColor.
i would write:

                BlendingFactorSrc src = BlendingFactorSrc.DestColor;
                BlendingFactorDest dest = BlendingFactorDest.Zero;

this works also fine as long as i dont change the color/alpha value in the program. i guess it does not work because it would need the alpha information already stored in the texture instead of assigning it via opengl.

im probably doing something wrong.
i want to achieve a photoshop like behavior. basically i need to have a alpha blendmode applied and then using the result to multiply. or am i totally wrong?

sorry im very confused. maybe you could clear things up.

thanks in advance


Comments

the Fiddler's picture

Multiply blend is usually defined like you said, DestColor & Zero. When you say it does not work, what doesn't work?Screenshots could also help (current vs desired result).

meno's picture

thanks for the answer.

i probably wasnt clear enough.

this is how it is currently:

as you see, the transparent areas of the image are only transparent when using alpha blending. also the new alpha value assigned via GL.Color4 is only used in alpha blending. but i want to have that taken in account with multiply, too.

like this (photoshopped)

the Fiddler's picture

This might help with the black-regions (which should be transparent instead):

    BlendingFactorSrc src = BlendingFactorSrc.DestColor;
    BlendingFactorDest dest = BlendingFactorDest.OneMinusSrcAlpha;

A quick search indicates that Photoshop tends to pre-multiply the alpha-channel. What happens if you use this as your vertex color?

GL.Color4(0.1, 0.1, 0.1, 0.1);
// or GL.Color4(r * a, g * a, b * a, a) in the general case
meno's picture

yay thanks. that works. i now do it this way

                BlendingFactorSrc src = BlendingFactorSrc.SrcAlpha;
                BlendingFactorDest dest = BlendingFactorDest.OneMinusSrcAlpha;
                BlendEquationMode mode = BlendEquationMode.FuncAdd;
 
                switch (blendMode) {
                    case BlendMode.Multiply:
                        src = BlendingFactorSrc.DstColor;
                        dest = BlendingFactorDest.OneMinusSrcAlpha;
                        GL.Color4(color.R * color.A, color.G * color.A, color.B * color.A, color.A);
                        break;
                    default:
                        // Blendmode alpha (default)
                        GL.Color4(color.R, color.G, color.B, color.A);
                        break;
                }
 
                GL.BlendEquation(mode);
                GL.BlendFunc(src, dest);

i think it works fine. i will try other blendmodes in a similar way. thanks.