Background Background Background Background Background Background


Understanding Blend Modes

How Blend Modes Work in idTech 3

In the Quake 3 engine, blendmodes determine how the pixels of one texture stage are mathematically combined with what’s already been drawn in the framebuffer. As mentioned in a previous article, this is why the rgbGen stage should come before blendFunc in your scripts. This is done with OpenGL’s glBlendFunc under the hood. It’s used for transparency, additive effects, light glows, decals, and other special effects.

Common blendFunc Combinations

These are the most common pairs that a level designer would use. There are more pairs that work together, however don't serve much practical purpose.

Shader Command OpenGL Equivalent Typical Use
blendFunc GL_ONE GL_ZERO Replace Opaque surfaces. Just draws the texture, no blending.
blendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA Alpha blend Fades based on alpha channel, used for soft transparency (glass, smoke, decals with feathered edges).
blendFunc GL_ONE GL_ONE Additive Sprites, Light glows, energy beams, muzzle flashes; Colors add together, which can add a bright glow - can be used to your advanteage with energy VFX that brighten underlying pixels over 255,255,255 ranges. Avoid exceeding colour ranges unintentionally, it looks ugly.
blendFunc GL_DST_COLOR GL_ZERO Multiply Lightmaps and shadowing. Darkens based on destination color.
blendFunc GL_ZERO GL_ONE_MINUS_SRC_COLOR Inverse multiply Similar to multiply but reverses source/dest roles.


How the maths works

Getting a handle on this concept is absolutely key to understanding all shader manipulation of graphics. As Blend Functions are the equation at the core of processing shader graphics.

The blending formula in OpenGL is:

FinalPixel = (SourceColor × SRC_FACTOR) + (DestinationColor × DEST_FACTOR)

Source is usually the RGB color data in a texture TGA file (remember it's all numbers) modified by any rgbGen and alphaGen. In the shader, the source is generally identified by command map, followed by the name of the image.

Destination is the color data currently existing in the frame buffer.

Rather than think of the entire texture as a whole, it maybe easier to think of the number values that correspond to a single pixel, because that is essentially what the computer is processing... one pixel of the bitmap at a time.



Here is also a useful visual reference, with the destination factor being the wood surface rendering behind our sprite and the source factor being the white sprite texture we are blending.

Source Blend (srcBlend)

The following values are valid for the source blend part of the equation.

GL_ONE
This is the value 1. When multiplied by the source, the value stays the same. The value of the color information does not change.
GL_ZERO
This is the value 0. When multiplied by the source, all RGB data in the source becomes zero (essentially black).
GL_DST_COLOR
This is the value of color data currently in the destination (frame buffer). The value of that information depends on the information supplied by previous stages.
GL_ONE_MINUS_DST_COLOR
This is nearly the same as GL_DST_COLOR except that the value for each component color is inverted by subtracting it from one. (i.e. R = 1.0 - DST.R, G = 1.0 - DST.G, B = 1.0 - DST.B, etc.)
GL_SRC_ALPHA
The TGA file being used for the source data must have an alpha channel in addition to its RGB channels (for a total of four channels). The alpha channel is an 8-bit black and white only channel. An entirely white alpha channel will not darken the source.
GL_ONE_MINUS_SRC_ALPHA
This is the same as GL_SRC_ALPHA except that the value in the alpha channel is inverted by subtracting it from one. (i.e. A=1.0 - SRC.A)

Destination Blend (dstBlend)

The following values are valid for the destination blend part of the equation.

GL_ONE
This is the value 1. When multiplied by the destination, the value stays the same the value of the color information does not change.
GL_ZERO
This is the value 0. When multiplied by the destination, all RGB data in the destination becomes zero (essentially black).
GL_SRC_COLOR
This is the value of color data currently in the source (which is the texture being manipulated here).
GL_ONE_MINUS_SRC_COLOR
This is the value of color data currently in source, but subtracted from one (i.e. inverted).
GL_SRC_ALPHA
The TGA file being used for the source data must have an alpha channel in addition to its RGB channels (four a total of four channels). The alpha channel is an 8-bit black and white only channel. An entirely white alpha channel will not darken the source.v GL_ONE_MINUS_SRC_ALPHA This is the same as GL_SRC_ALPHA except that the value in the alpha channel is inverted by subtracting it from one. (i.e. A=1.0 - SRC.A).

Important to Remember:

Combine effects where possible, each extra stage costs fill and draw time. In idTech3, each shader stage is an extra draw call.. This can add up quickly for frequently used shaders like windows.