GLAPI/glBlendFunc: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
m Description: table formatting.
Redirect to the separate page.
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{infobox feature
#REDIRECT [[GLAPI/glBlendFuncSeparate]]
| name = glBlendFunc
| core = 1.0
}}


'''glBlendFunc:''' specify pixel arithmetic
[[Category:Core API Ref Post Fragment Shader Operations|BlendFunc]]
 
[[Category:Core API Reference|BlendFunc]]
== Function Definition ==
 
  void '''glBlendFunc'''(GLenum ''sfactor'', GLenum ''dfactor'');
  void '''glBlendFunci'''(GLuint ''buf'', GLenum ''sfactor'', GLenum ''dfactor'');
 
; buf
: For '''glBlendFunci''', specifies the index of the draw buffer for which to set the blend function.
; sfactor
: Specifies how the red, green, blue, and alpha source blending factors are computed. The initial value is {{code|GL_ONE}}.
; dfactor
: Specifies how the red, green, blue, and alpha destination blending factors are computed. The following symbolic constants are accepted: {{code|GL_ZERO}}, {{code|GL_ONE}}, {{code|GL_SRC_COLOR}}, {{code|GL_ONE_MINUS_SRC_COLOR}}, {{code|GL_DST_COLOR}}, {{code|GL_ONE_MINUS_DST_COLOR}}, {{code|GL_SRC_ALPHA}}, {{code|GL_ONE_MINUS_SRC_ALPHA}}, {{code|GL_DST_ALPHA}}, {{code|GL_ONE_MINUS_DST_ALPHA}}. {{code|GL_CONSTANT_COLOR}}, {{code|GL_ONE_MINUS_CONSTANT_COLOR}}, {{code|GL_CONSTANT_ALPHA}}, and {{code|GL_ONE_MINUS_CONSTANT_ALPHA}}. The initial value is {{code|GL_ZERO}}.
 
== Description ==
 
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 [[GLAPI/glEnable|glEnable]] and [[GLAPI/glDisable|glDisable]] with argument {{code|GL_BLEND}} to enable and disable blending.
 
'''glBlendFunc''' defines the operation of blending for all draw buffers when it is enabled. '''glBlendFunci''' defines the operation of blending for a single draw buffer specified by ''buf'' when enabled for that draw buffer. ''sfactor'' specifies which method is used to scale the source color components. ''dfactor'' specifies which method is used to scale the destination color components. Both parameters must be one of the following symbolic constants: {{code|GL_ZERO}}, {{code|GL_ONE}}, {{code|GL_SRC_COLOR}}, {{code|GL_ONE_MINUS_SRC_COLOR}}, {{code|GL_DST_COLOR}}, {{code|GL_ONE_MINUS_DST_COLOR}}, {{code|GL_SRC_ALPHA}}, {{code|GL_ONE_MINUS_SRC_ALPHA}}, {{code|GL_DST_ALPHA}}, {{code|GL_ONE_MINUS_DST_ALPHA}}, {{code|GL_CONSTANT_COLOR}}, {{code|GL_ONE_MINUS_CONSTANT_COLOR}}, {{code|GL_CONSTANT_ALPHA}}, {{code|GL_ONE_MINUS_CONSTANT_ALPHA}}, {{code|GL_SRC_ALPHA_SATURATE}}, {{code|GL_SRC1_COLOR}}, {{code|GL_ONE_MINUS_SRC1_COLOR}}, {{code|GL_SRC1_ALPHA}}, and {{code|GL_ONE_MINUS_SRC1_ALPHA}}. The possible methods are described in the following table. Each method defines four scale factors, one each for red, green, blue, and alpha. In the table and in subsequent equations, first source, second source and destination color components are referred to as <math>(R_{s0}, G_{s0}, B_{s0}, A_{s0})</math>, <math>(R_{s1}, G_{s1}, B_{s1}, A_{s1})</math> and <math>(R_d, G_d, B_d, A_d)</math>, respectively. The color specified by [[GLAPI/glBlendColor|glBlendColor]] is referred to as <math>(R_c, G_c, B_c, A_c)</math>. They are understood to have integer values between 0 and <math>(k_R, k_G, k_B, k_A)</math>, where
 
<math>k_c = 2^{m_c} - 1</math>
 
and <math>(m_R, m_G, m_B, m_A)</math> are the number of red, green, blue, and alpha bitplanes.
 
Source and destination scale factors are referred to as <math>(s_R, s_G, s_B, s_A)</math> and <math>(d_R, d_G, d_B, d_A)</math>. The scale factors described in the table, denoted <math>(f_R, f_G, f_B, f_A)</math>, represent either source or destination factors. All scale factors have range [0, 1].
 
{| class="wikitable" style="background-color:transparent;"
|+
! '''Parameter'''
! '''<math>(f_R, f_G, f_B, f_A)</math>'''
|+
| {{code|GL_ZERO}}
| <math>(0, 0, 0, 0)</math>
|+
| {{code|GL_ONE}}
| <math>(1, 1, 1, 1)</math>
|+
| {{code|GL_SRC_COLOR}}
| <math>\left (\tfrac{R_{s0}}{k_R}, \tfrac{G_{s0}}{k_G}, \tfrac{B_{s0}}{k_B}, \tfrac{A_{s0}}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_SRC_COLOR}}
| <math>(1, 1, 1, 1) - \left (\tfrac{R_{s0}}{k_R}, \tfrac{G_{s0}}{k_G}, \tfrac{B_{s0}}{k_B}, \tfrac{A_{s0}}{k_A} \right )</math>
|+
| {{code|GL_DST_COLOR}}
| <math>\left (\tfrac{R_d}{k_R}, \tfrac{G_d}{k_G}, \tfrac{B_d}{k_B}, \tfrac{A_d}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_DST_COLOR}}
| <math>(1, 1, 1, 1) - \left (\tfrac{R_d}{k_R}, \tfrac{G_d}{k_G}, \tfrac{B_d}{k_B}, \tfrac{A_d}{k_A} \right )</math>
|+
| {{code|GL_SRC_ALPHA}}
| <math>\left (\tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_SRC_ALPHA}}
| <math>(1, 1, 1, 1) - \left (\tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A}, \tfrac{A_{s0}}{k_A} \right )</math>
|+
| {{code|GL_DST_ALPHA}}
| <math>\left (\tfrac{A_d}{k_A}, \tfrac{A_d}{k_A}, \tfrac{A_d}{k_A}, \tfrac{A_d}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_DST_ALPHA}}
| <math>(1, 1, 1, 1) - \left (\tfrac{A_d}{k_A}, \tfrac{A_d}{k_A}, \tfrac{A_d}{k_A}, \tfrac{A_d}{k_A} \right )</math>
|+
| {{code|GL_CONSTANT_COLOR}}
| <math>\left (\tfrac{R_c}{k_R}, \tfrac{G_c}{k_G}, \tfrac{B_c}{k_B}, \tfrac{A_c}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_CONSTANT_COLOR}}
| <math>(1, 1, 1, 1) - \left (\tfrac{R_d}{k_R}, \tfrac{G_d}{k_G}, \tfrac{B_d}{k_B}, \tfrac{A_d}{k_A} \right )</math>
|+
| {{code|GL_CONSTANT_ALPHA}}
| <math>\left (\tfrac{A_c}{k_A}, \tfrac{A_c}{k_A}, \tfrac{A_c}{k_A}, \tfrac{A_c}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_CONSTANT_ALPHA}}
| <math>(1, 1, 1, 1) - \left (\tfrac{A_c}{k_A}, \tfrac{A_c}{k_A}, \tfrac{A_c}{k_A}, \tfrac{A_c}{k_A} \right )</math>
|+
| {{code|GL_SRC_ALPHA_SATURATE}}
| <math>(i, i, i, 1)</math>
|+
| {{code|GL_SRC1_COLOR}}
| <math>\left (\tfrac{R_{s1}}{k_R}, \tfrac{G_{s1}}{k_G}, \tfrac{B_{s1}}{k_B}, \tfrac{A_{s1}}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_SRC1_COLOR}}
| <math>(1, 1, 1, 1) - \left (\tfrac{R_{s1}}{k_R}, \tfrac{G_{s1}}{k_G}, \tfrac{B_{s1}}{k_B}, \tfrac{A_{s1}}{k_A} \right )</math>
|+
| {{code|GL_SRC1_ALPHA}}
| <math>\left (\tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A} \right )</math>
|+
| {{code|GL_ONE_MINUS_SRC1_ALPHA}}
| <math>(1, 1, 1, 1) - \left (\tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A}, \tfrac{A_{s1}}{k_A} \right )</math>
|}
 
In the table,
 
<math>i = \frac{min(A_s, (k_A - A_d))}{k_A}</math>
 
To determine the blended RGBA values of a pixel, the system uses the following equations:
 
<math>\begin{align}
R_d = min(k_R, R_s*s_R + R_d*d_R)\\
G_d = min(k_G, G_s*s_G + G_d*d_G)\\
B_d = min(k_B, B_s*s_B + B_d*d_B)\\
A_d = min(k_A, A_s*s_A + A_d*d_A)\\
\end{align}</math>
 
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 1 is guaranteed not to modify its multiplicand, and a blend factor equal to 0 reduces its multiplicand to 0. For example, when ''sfactor'' is {{code|GL_SRC_ALPHA}}, ''dfactor'' is {{code|GL_ONE_MINUS_SRC_ALPHA}}, and
<math>A_s</math> is equal to <math>k_A</math>, the equations reduce to simple replacement:
 
<math>\begin{align}
R_d = R_s\\
G_d = G_s\\
B_d = B_s\\
A_d = A_s\\
\end{align}</math>
 
== Examples ==
 
Transparency is best implemented using blend function ({{code|GL_SRC_ALPHA}}, {{code|GL_ONE_MINUS_SRC_ALPHA}}) with primitives sorted from farthest to nearest. Note that this transparency calculation does not require the presence of alpha bitplanes in the frame buffer.
 
Blend function ({{code|GL_SRC_ALPHA}}, {{code|GL_ONE_MINUS_SRC_ALPHA}}) is also useful for rendering antialiased points and lines in arbitrary order.
 
Polygon antialiasing is optimized using blend function ({{code|GL_SRC_ALPHA_SATURATE}}, {{code|GL_ONE}}) with polygons sorted from nearest to farthest. (See the [[GLAPI/glEnable|glEnable]], [[GLAPI/glDisable|glDisable]] reference page and the {{code|GL_POLYGON_SMOOTH}} argument for information on polygon antialiasing.) Destination alpha bitplanes, which must be present for this blend function to operate correctly, store the accumulated coverage.
 
== Notes ==
 
Incoming (source) alpha is correctly thought of as a material opacity, ranging from 1.0 (<math>k_A</math>), representing complete opacity, to 0.0 (0), representing complete transparency.
 
When more than one color buffer is enabled for drawing, the GL performs blending separately for each enabled buffer, using the contents of that buffer for destination color. (See [[GLAPI/glDrawBuffer|glDrawBuffer]].)
 
When dual source blending is enabled (i.e., one of the blend factors requiring the second color input is used), the maximum number of enabled draw buffers is given by {{code|GL_MAX_DUAL_SOURCE_DRAW_BUFFERS}}, which may be lower than {{code|GL_MAX_DRAW_BUFFERS}}.
 
== Errors ==
 
{{code|GL_INVALID_ENUM}} is generated if either ''sfactor'' or ''dfactor'' is not an accepted value.
 
{{code|GL_INVALID_VALUE}} is generated by '''glBlendFunci''' if ''buf'' is greater than or equal to the value of {{code|GL_MAX_DRAW_BUFFERS}}.
 
== Associated Gets ==
 
[[GLAPI/glGet|glGet]] with argument {{code|GL_BLEND_SRC}}
 
[[GLAPI/glGet|glGet]] with argument {{code|GL_BLEND_DST}}
 
[[GLAPI/glIsEnabled|glIsEnabled]] with argument {{code|GL_BLEND}}
 
 
 
== See Also ==
 
[[GLAPI/glBlendColor|glBlendColor]], [[GLAPI/glBlendEquation|glBlendEquation]], [[GLAPI/glBlendFuncSeparate|glBlendFuncSeparate]], [[GLAPI/glClear|glClear]], [[GLAPI/glDrawBuffer|glDrawBuffer]], [[GLAPI/glEnable|glEnable]], [[GLAPI/glLogicOp|glLogicOp]], [[GLAPI/glStencilFunc|glStencilFunc]]
 
== Copyright ==
 
Copyright © 1991-2006 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, see [http://oss.sgi.com/projects/FreeB/ http://oss.sgi.com/projects/FreeB/].
 
[[Category:API Reference 4|BlendFunc]]

Latest revision as of 12:33, 27 August 2012