The workings of glBlendFunc
Rob Fletcher

Hello,

I'm a little confused, and I'm having trouble googling my troubles away. I'm getting into some of OpenGLs blendy abilities, so glBlendFunc has become of interest to me.

There is no shortage of the manual out there, but I don't quite get something.

I think what I'm not understanding is what color scaling is all about. At least, why there would be two of them. In my brain, I see blending as having a function that takes the new and the old colors, does stuff to them, returns the output color. Functions are like 'add', 'screen', etc... Yet, glBlendFunc asks for two parameters, and I can't seem to understand it.

Could somebody please explain to me (or maybe link to something with a good explanation) glBlendFunc's two parameters and how they affect the blending?

Thanks!

Rob

James Howard

sfactor is the source colour
dfactor is the destination colour

manual said:

sfactor:
Specifies how the red, green, blue and alpha source blending factors are computed

dfactor:
Specifies how the red, green, blue and alpha destination blending factors are computed

e.g. if you call glBlendFunc(GL_ONE, GL_ONE), and
source colour = r:0.2, g:0.4, b:0.1
dest colour = r:0.1, g:0.1, b:0.6
output colour = r:0.3, g:0.5, b:0.7

gillius

I believe it does "s * s_fac + d * d_fac", where you give it s_fac and d_fac. In the previous post, GL_ONE was used, so it became s+d, or an additive blend.

Krzysztof Kluczek

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

Rob Fletcher

Ah!

It all clicks now. Thanks guys. Sometimes when my brain isn't in math-mode I forget the implications of the term "factor".

It makes good sense now, thanks!

Rob

Bob
Quote:

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

Unless NV_blend_square is supported, of course ;D

Thread #381346. Printed from Allegro.cc