glBlendFunc(3G) OpenGL Reference glBlendFunc(3G)
NAME
glBlendFunc - specify pixel arithmetic
C SPECIFICATION
void glBlendFunc( GLenum sfactor,
GLenum dfactor )
PARAMETERS
sfactor Specifies how the red, green, blue, and alpha source blending
factors are computed. Thirteen symbolic constants are accepted:
GLZERO, GLONE, GLDSTCOLOR, GLONEMINUSDSTCOLOR,
GLSRCALPHA, GLONEMINUSSRCALPHA, GLDSTALPHA,
GLONEMINUSDSTALPHA, GLSRCALPHASATURATE,
GLCONSTANTCOLOREXT, GLONEMINUSCONSTANTCOLOREXT,
GLCONSTANTALPHAEXT, and GLONEMINUSCONSTANTALPHAEXT.
dfactor Specifies how the red, green, blue, and alpha destination
blending factors are computed. Twelve symbolic constants are
accepted: GLZERO, GLONE, GLSRCCOLOR,
GLONEMINUSSRCCOLOR, GLSRCALPHA, GLONEMINUSSRCALPHA,
GLDSTALPHA, GLONEMINUSDSTALPHA, GLCONSTANTCOLOREXT,
GLONEMINUSCONSTANTCOLOREXT, GLCONSTANTALPHAEXT, and
GLONEMINUSCONSTANTALPHAEXT.
DESCRIPTION
In RGB mode, pixels can be drawn using a function that blends the
incoming (source) RGBA values with the RGBA values that are already in
the frame buffer (the destination values). Blending is initially
disabled. Use glEnable and glDisable with argument GLBLEND to enable and
disable blending.
When blending is enabled, glBlendFunc, glBlendColorEXT, and
glBlendEquationEXT determine the blending operation. sfactor and dfactor
specify the scaling rules used for scaling the source and destination
color components, respectively. Each rule defines four scale factors,
one each for red, green, blue, and alpha. The rules are described in the
table below.
In the following, source, destination, and GLBLENDCOLOREXT color
components are denoted by (R ,G ,B ,A ), (R ,G ,B ,A ) and (R ,G ,B ,A ),
s s s s d d d d c c c c
respectively. Color components are understood to have integer values
m
c
between 0 and (k ,k ,k ,k ), where k = 2 -1 with m being the number of
R G B A c c
bitplanes for the corresponding color component (c is one of R, G, B, or
A). All scale factors are in the range [0,1]. Source and destination
scale factors are denoted by (s ,s ,s ,s ) and (d ,d ,d ,d ). In the
R G B A R G B A
following table, (f ,f ,f ,f ) denotes either source or destination
R G B A
factors, and i = min(A , k -A ) / k .
s A d A
Page 1
glBlendFunc(3G) OpenGL Reference glBlendFunc(3G)
parameter (f , f , f , f )
R G B A
_____________________________________________________________________________
GLZERO (0, 0, 0, 0)
GLONE (1, 1, 1, 1)
GLSRCCOLOR (R /k , G /k , B /k , A /k )
s R s G s B s A
GLONEMINUSSRCCOLOR (1, 1, 1, 1) - (R /k , G /k , B /k , A /k )
s R s G s B s A
GLDSTCOLOR (R /k , G /k , B /k , A /k )
d R d G d B d A
GLONEMINUSDSTCOLOR (1, 1, 1, 1) - (R /k , G /k , B /k , A /k )
d R d G d B d A
GLSRCALPHA (A /k , A /k , A /k , A /k )
s A s A s A s A
GLONEMINUSSRCALPHA (1, 1, 1, 1) - (A /k , A /k , A /k , A /k )
s A s A s A s A
GLDSTALPHA (A /k , A /k , A /k , A /k )
d A d A d A d A
GLONEMINUSDSTALPHA (1, 1, 1, 1) - (A /k , A /k , A /k , A /k )
d A d A d A d A
GLCONSTANTCOLOREXT (R G B A
c, c, c, c)
GLONEMINUSCONSTANTCOLOREXT (1, 1, 1, 1) - (R G B A
c, c, c, c)
GLCONSTANTALPHAEXT (A A A A
c, c, c, c)
GLONEMINUSCONSTANTALPHAEXT (1, 1, 1, 1) - (A A A A
c, c, c, c)
GLSRCALPHASATURATE (i, i, i, 1)
Blending combines corresponding source and destination color components
according to the blending operation specified by GLBLENDEQUATIONEXT.
The blending operations are:
GLBLENDEQUATIONEXT Binary Operation
__________________________________________________
GLFUNCADDEXT min(C *s +C *d ,k )
s C d C C
GLFUNCSUBTRACTEXT max(C *s -C *d ,0)
s C d C
GLFUNCREVERSESUBTRACTEXT max(C *d -C *s ,0)
d C s C
GLLOGICOP C Lop C
s d
GLMINEXT min(C ,C )
s d
GLMAXEXT max(C ,C )
s d
where C is the relevant color component (R, G, B, or A), C and C are
s d
the source and destination color components, respectively, s and d are
C C
the source and destination scale factors, respectively, and Lop is one of
16 bitwise operators specified by glLogicOp.
Despite the apparent precision of the above equations, blending
arithmetic is not exactly specified, because blending operates with
imprecise integer color values. However, a blend factor that should be
equal to one is guaranteed not to modify its multiplicand, and a blend
factor equal to zero reduces its multiplicand to zero. Thus, for
example, when sfactor is GLSRCALPHA, dfactor is GLONEMINUSSRCALPHA,
GLBLENDEQUATIONEXT is GLFUNCADDEXT, and A is equal to k , the
s A
equations reduce to simple replacement:
R = R , G = G , B = B , A = A .
d s d s d s d s
EXAMPLES
Transparency is best implemented using blend function (GLSRCALPHA,
GLONEMINUSSRCALPHA) with primitives sorted from farthest to nearest.
Note that this transparency calculation does not require the presence of
alpha bitplanes in the frame buffer.
Page 2
glBlendFunc(3G) OpenGL Reference glBlendFunc(3G)
Blend function (GLSRCALPHA, GLONEMINUSSRCALPHA) is also useful for
rendering antialiased points and lines in arbitrary order.
Polygon antialiasing is optimized using blend function
(GLSRCALPHASATURATE, GLONE) with polygons sorted from nearest to
farthest. (See the glEnable, glDisable reference page and the
GLPOLYGONSMOOTH argument for information on polygon antialiasing.)
Destination alpha bitplanes, which must be present for this blend
function to operate correctly, store the accumulated coverage.
To blend two RGB (no alpha component) images as (1-p)*ImageA+p*ImageB,
one would disable blending, draw ImageA into the frame buffer, enable
blending, set GLBLENDCOLOREXT to (0,0,0,p), the blend function to
(GLCONSTANTALPHAEXT, GLONEMINUSCONSTANTALPHAEXT), (and the blend
equation to the default GLFUNCADDEXT), and draw ImageB.
NOTES
Incoming (source) alpha is correctly thought of as a material opacity,
ranging from 1.0 (K ), representing complete opacity, to 0.0 (0),
A
representing complete transparency.
When more than one color buffer is enabled for drawing, blending is done
separately for each enabled buffer, using for destination color the
contents of that buffer. (See glDrawBuffer.)
Blending affects only RGB rendering. It is ignored by color index
renderers.
ERRORS
GLINVALIDENUM is generated if either sfactor or dfactor is not an
accepted value.
GLINVALIDOPERATION is generated if glBlendFunc is executed between the
execution of glBegin and the corresponding execution of glEnd.
ASSOCIATED GETS
glGet with argument GLBLENDSRC, GLBLENDDST, GLLOGICOPMODE, or
GLBLENDEQUATIONEXT, GLBLENDCOLOREXT.
glIsEnabled with argument GLBLEND
MACHINE DEPENDENCIES
RealityEngine, RealityEngine2, and VTX systems do not support the use of
source blend factor GLSRCALPHASATURATE and a destination factor based
on the constant blend color (GL..CONSTANT..EXT); such combinations
produce incorrect results.
Page 3
glBlendFunc(3G) OpenGL Reference glBlendFunc(3G)
SEE ALSO
glAlphaFunc, glClear, glDrawBuffer, glEnable, glLogicOp, glStencilFunc,
glBlendColorEXT, glBlendEquationEXT.
Page 4