blendMode() use.

I’ve been playing around with blendMode() and its simply awesome! However, I’m unsure how to use the 3rd and 4th params (srcAlpha,dstAlpha). How are they used and do they use the same const values as scrFactor and dstFactor?

Also, is there a way to limit whats blended? I seem to always grab the background as well when I just want to blend two images.

Just a side note: I’ve never really gotten anything about blend modes past the basic presets. Is there a place I can refer to that can explain it a bit better? I’ve attempted to look at documentation for the OpenGL function that is virtually the same thing without luck.
Thanks!

@zoyt I just made a demo project and managed to get some cool torch effects. But I can’t figure out the alpha modes.

@Briarfox - Is it on CC? (I’m not near my iPad.)

@zoyt I havent posted it, Just made a table holding all the factor vars and playing around with them and images. I’ll set a demo up.

@Briarfox maybe this link will help you http://www.opengl.org/archives/resources/faq/technical/transparency.htm

Heres one of the better write-ups I’ve found.

http://www.glprogramming.com/red/chapter06.html

If you want to blend two images then you need to do it within a setContext. The blendmode controls how what is new blends with what is already there, whence the background counts as what is already there. In particular, if you draw two images then the logic is:

  1. Blend the first image with the background,
  2. Blend the second image with the result of the first operation.

So to blend two images you need to ensure that the first image is the background for the second. Thus setContext.

The srcAlpha and dstAlpha control the alpha channel of the final image. So putting factors in there says how much of the source alpha and destination alpha should make up the final alpha. The same constants can go here as in the colour options.

The best site that I’ve found is http://www.andersriggelsen.dk/glblendfunc.php where one can experiment with the various parameters. I don’t find explanations of blending all that helpful, and demos that simply run through all the options are similarly unilluminating (partly because there are so many options).

@andrew_stacey so src alpha takes a fraction between 0 and one to set blending alpha? That makes sense, I had been trying the factor Constance. Thank you.

@Briarfox I completely stole someones post on another forum but blendMode comes from glBlendFunc:

"Yes, OpenGL blending takes source and destination colors, multiplies them by something and then adds them. glBlendFunc specifies what those colors should be multiplied by.

Possible modes:

GL_ZERO - color is ignored
GL_ONE - full color is taken into addition
GL_###_COLOR - color is multiplied by ### color
GL_ONE_MINUS_###_COLOR - color is multiplied by inverted ### color
GL_###_ALPHA - color is multiplied by ### alpha value
GL_ONE_MINUS_###_ALPHA - color is multiplied by inverted ### alpha value
GL_SRC_ALPHA_SATURATE - color is multiplied by MIN(src_alpha,1-dst_alpha) (may not work in some implementations)
where ### can be:
SRC - source
DST - destination

Note that OpenGL some combinations may not work, like multiplying color by itself.

Examples:
glBlendFunc(GL_ONE,GL_ZERO); - everything works as usual, despite blending turned on
glBlendFunc(GL_ZERO,GL_ONE); - nothing is being drawn (Z-buffer may be still affected)
glBlendFunc(GL_ONE,GL_ONE); - colors are added (values >1 are clipped to 1)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); - transparency: alpha=0 - invisible, alpha=1 - no transparency"

The idea is if I have half red (128,0,0) and half red + blue (128,0,128) and add them
with ONE,ONE then both colours are added (I don’t believe glBlendEquation is changeable so subtraction isn’t an option)

I visualise it as a fraction of 1 then add these together using the idea of what each possible mode does (ONE_MINUS_SRC_ALPHA for instance) with ONE as the second parameter means the alpha is multiplied by the inverse of the colour (this may the the other way round) so you can understand what each effect will produce. I use the first two parts of blendMode() mostly and sometimes the last 2 which are for alpha.

It took me this quoted post and the link which andrew stacey provided above to understand throughly how they can be manipulated.

Subtraction is possible: use those ONE_MINUS multipliers.

@Luatee @Andrew_Stacey I think I got the factor part down but do the 3rd and 4th alpha values take a fraction from 0-1 or do you use the Alpha multipliers there?

@Andrew_Stacey I see, are all the same operations there with the ONE_MINUS?

Alpha multipliers, so for example SRC_ALPHA, DST_ALPHA etc are used for the 3rd and 4th.

@luatee but they can also be used in the 1 & 2nd. I was rarely able to see a difference when using *_ALPHA so I wasn’t sure I was doing it right. Luatee, did you see the link I posted it has some really good examples. I’ve found this very useful for creating snazzy splash screens :slight_smile:

@Briarfox all of these values (ONE,ZERO,ONE_MINUS_“”"_COLOR/ALPHA) correspond to values of 0-1 so it won’t matter where you put SRC_ALPHA for example as it has a value of 0-1 but you won’t really be able to make a formula out of it for a specific type of action e.g multiplying colours (SRC_COLOR,DST_COLOR) does this including alpha values as well.

So your first question was right, they do take values from 0 to 1 but they are taken in the form of words SRC_etc which correspond to this variable value, I myself have never tried putting numbers in as I don’t think SRC_* is a variable value but a constant value that returns a specific value.