[OpenGL] An understandable explanation of glBlendFunc

I had to use glBlendFunc recently and found the official documentation really hard to understand immediately.
A coworker explained it in a much more understandable way.

So, the signature of glBlendFunc is:

void glBlendFunc(GLenum sfactor, GLenum dfactor);

Let S be the source color (color of one pixel in the source buffer).
Let D be the destination color (color of the corresponding pixel already in the destination buffer).
And let R be the resulting color (color to write in the destination buffer).

Then, we have the simple following equation:

R = sfactor * S + dfactor * D

This equation may become a bit more complex regarding the glBlendFunc parameters, but here is the principle.

Example: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);


Xy being the y component of the X color,
R = S + (1 – Sa) * D

Rr = Sr + (1 – Sa) * Dr
Rg = Sg + (1 – Sa) * Dg
Rb = Sb + (1 – Sa) * Db
Ra = Sa + (1 – Sa) * Da

Let’s see the case I was working on.
I had to convert an RGBA8 source to an RGBA8 PREMULTIPLIED destination.

Sa * S gives the premultiplied source.
(1 – Sa) * D gives allows alpha blending to the previous value in the destination buffer.

Therefore, R = Sa * S + (1 – Sa) * D
finally giving glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

See also:
glBlendFuncSeparate in order to set a different factor for the RGB and the A components.
glBlendEquation in order to use another equation to compute the resulting color value (R = sfactor * S – dfactor * D for instance).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s