   multi-layer painting
  Hi,Re-posted with a title - sorryI'm programming a multi-layer painting app and I'm currently struggling with the blending operations. This is where I am today :- when I project the brush image on a layer, I use the classical blending equation : COL = COLsrc * ALPHAsrc + COLdest (1 - ALPHAsrc). This gives ugly results, i.e. a halo at intermediate alphas (at the edge of the brush image). - looking at the blending equations, this is logical, as shown in the example : color = red(src) * 0.5 + blue(dest) * (1 - 0.5) = purple ; this is OK if the destination is opaque purple, but if the destination is transparent at that pixel, the purple color is unwanted.- glBlendFuncSeparate improves somewhat, but there are still similar problem.- after playing a little with the blending equations, I came to the conclusion that no standard openGL blending function would do the job. Anybody familiar with painting on layers with openGL ? Is there a site that covers this specific quaestion ? If not, how can I blend two layers without using the built-in blending operation ? Thanks   
  You can switch over to shader programming. There you have maximum flexibility.   ;  That's what I do. Almost everything runs with a glsl shader. However, at the end, you need to issue a gl_FragColor command to write to some buffer. For example, when painting, the brush image is projected to some texture ; the brush has an alpha channel so that the right amount of paint is projected. To have this effect, you need to turn on blending ; and no blending function available with openGL will do the work properly.   ;  I don't know about the speed implications of this, but if you want to define a custom blending operation you could do the following:1)Write first layer to texture2)Write second layer to new texture while sampling from the first texture and blend in the frag shader.3)Write third layer to new texture while sampling the second texture..This way every time you draw a new layer you can sample the existing color and do custom blend op.   ;  Like karwosts has answered in the meanwhile, the intention was to do the blending itself in the fragment shader, so that the output is the already blended pixel. Of course, you still need a better suited algorithm for the blending as is given by the build-in functionality.   ;Quote:Original post by Farfadet- after playing a little with the blending equations, I came to the conclusion that no standard openGL blending function would do the job. The blending function you want is:glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);I know, it looks wrong, because it doesn't appear to be masking the foreground layer at all - but here's the thing: your brush texture's RGB values have already been multiplied by is alpha. A black background was baked into the map itself as it was created.The classical blending equation assumes the foreground element has a consistently bright RGB and a varying alpha, so when used with ready-made brushes (which already fade to black where transparent), it redundantly multiplies RGB by A and produces those ugly dark fringes.By skipping that alpha multiplication at compositing time with (GL_ONE,GL_ONE_MINUS_SRC_ALPHA), you get what's called a 'premultiplied alpha blend', and that's generally the right way to composite any foreground elements that have their own black backgrounds.   ;  The final blend you output to the backbuffer should be a simple  ONE:ZERO copy.The trick is to keep your composited "real screen" as a texture also and feed that into your blend shaders. Takes a bit of extra maintenance, but all of a sudden you can literally do anything you want.   ;  Helicobster : using your solution, my example becomescolor = red(src) * 1.0 + blue(dest) * (1 - 0.5) And this is still not good.karwosts, haegarr, Rubicon, I believe you're right, only a blend shader will do. I did a slightly more mathematical approach and came to this :Looking at one pixel, we can define the visible color at layer i as :Vi = Ci. αi + Vi-1.(1 - αi)Notice Vi-1 instead of the classical Ci-1 . This means that each visible color depends upon the visible color of the previous layer, not its own color. To compute Vi, we need to compute V0, V1, V2... same as karwosts explanation, put another way.This also gives the equations to merge two layers (here layers 3 and 4 are merged)α’3 = 1- (1-α3).(1-α4)C’3 = (C4. α4 + C3. α3. (1-α4)) / α’3Thanks everybody, most helpfull.   ;  hello,    hello do any know about creating layers in openGL..I heard that it s only supported by certain drivers only..??some extra h/w settings has to b made or what??   