Depth, blending & cullingGL 1.0

glBlendFunc

Sets how a new fragment's color is mixed with the color already in the framebuffer.

Signature

void glBlendFunc(GLenum sfactor, GLenum dfactor);

How it works

With GL_BLEND enabled, the final color is:

result = source × sfactor + destination × dfactor

where source is the fragment shader output and destination is what was already drawn. For normal transparency, sfactor = GL_SRC_ALPHA and dfactor = GL_ONE_MINUS_SRC_ALPHA: a 30% opaque fragment contributes 30% of its color and keeps 70% of what is behind.

sourceshader output× 0.3SRC_ALPHA+destinationalready drawn× 0.7ONE_MINUS_SRC_ALPHA=result(101, 81, 158)
Classic transparency, glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA): a fragment with alpha 0.3 keeps 30% of its own colour and 70% of what was already drawn. Channel by channel: 220 × 0.3 + 50 × 0.7 = 101 for red.

Parameters

sfactorGLenum

Multiplier for the NEW (source) color.

Common values

GL_SRC_ALPHA
The new fragment's alpha.
GL_ONE_MINUS_SRC_ALPHA
1 − new alpha. Paired with GL_SRC_ALPHA it gives classic transparency.
GL_ONE
Factor 1 — keep full value. GL_ONE, GL_ONE = additive blending (fire, glow).
GL_ZERO
Factor 0 — discard this term.
GL_DST_COLOR
The color already in the framebuffer (multiply effects).
dfactorGLenum

Multiplier for the EXISTING (destination) color.

Common values

GL_SRC_ALPHA
The new fragment's alpha.
GL_ONE_MINUS_SRC_ALPHA
1 − new alpha. Paired with GL_SRC_ALPHA it gives classic transparency.
GL_ONE
Factor 1 — keep full value. GL_ONE, GL_ONE = additive blending (fire, glow).
GL_ZERO
Factor 0 — discard this term.
GL_DST_COLOR
The color already in the framebuffer (multiply effects).

Tips & pitfalls

  • Order matters: draw opaque objects first, then transparent ones from farthest to nearest.

  • Blending does nothing until glEnable(GL_BLEND).

Example

example.cpp
1glEnable(GL_BLEND);
2glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Official Khronos documentation