Thursday, May 26, 2011

glBlendFunc example c c++ objc java

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. The following symbolic constants are accepted: GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, and GL_SRC_ALPHA_SATURATE. The initial value is GL_ONE.
dfactor
Specifies how the red, green, blue, and alpha destination blending factors are computed. Eight symbolic constants are accepted: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, and GL_ONE_MINUS_DST_ALPHA. The initial value is GL_ZERO.

Description

Pixels can be drawn using a function that blends the incoming (source) values with the values that are already in the color buffer (the destination values). Use glEnable and glDisable with argument GL_BLEND to enable and disable blending. Blending is initially disabled.
glBlendFunc defines the operation of blending when it is enabled. sfactor specifies which of nine methods is used to scale the source color components. dfactor specifies which of eight methods is used to scale the destination color components. The eleven 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, source and destination color components are referred to as ( Rs, Gs, Bs, As ) and ( Rd, Gd, Bd, Ad ). They are understood to have integer values between 0 and ( kR, kG, kB, kA ), where
kc = 2mc - 1
and ( mR, mG, mB, mA ) is the number of red, green, blue, and alpha bitplanes.
Source and destination scale factors are referred to as ( sR, sG, sB, sA ) and ( dR, dG, dB, dA ). The scale factors described in the table, denoted ( fR, fG, fB, fA ), represent either source or destination factors. All scale factors have range [0, 1].
Parameter ( fR, fG, fB, fA )
GL_ZERO ( 0, 0, 0, 0 )
GL_ONE ( 1, 1, 1, 1 )
GL_SRC_COLOR ( Rs / kR, Gs / kG, Bs / kB, As / kA )
GL_ONE_MINUS_SRC_COLOR ( 1, 1, 1, 1 ) - ( Rs / kR, Gs / kG, Bs / kB, As / kA )
GL_DST_COLOR ( Rd / kR, Gd / kG, Bd / kB, Ad / kA )
GL_ONE_MINUS_DST_COLOR ( 1, 1, 1, 1 ) - ( Rd / kR, Gd / kG, Bd / kB, Ad / kA )
GL_SRC_ALPHA ( As / kA, As / kA, As / kA, As / kA )
GL_ONE_MINUS_SRC_ALPHA ( 1, 1, 1, 1 ) - ( As / kA, As / kA, As / kA, As / kA )
GL_DST_ALPHA ( Ad / kA, Ad / kA, Ad / kA, Ad / kA )
GL_ONE_MINUS_DST_ALPHA ( 1, 1, 1, 1 ) - ( Ad / kA, Ad / kA, Ad / kA, Ad / kA )
GL_SRC_ALPHA_SATURATE ( i, i, i, 1 )
In the table, (glBlendFunc)
i = min( As, kA - Ad ) / kA
To determine the blended values of a pixel, the system uses the following equations:
Rd = min( kR, Rs sR + Rd dR )
Gd = min( kG, Gs sG + Gd dG )
Bd = min( kB, Bs sB + Bd dB )
Ad = min( kA, As sA + Ad dA )
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 GL_SRC_ALPHA, dfactor is GL_ONE_MINUS_SRC_ALPHA, and As is equal to kA, the equations reduce to simple replacement:
Rd = Rs
Gd = Gs
Bd = Bs
Ad = As
glBlendFunc operates on all pixel write operations, including the scan conversion of points, lines, and polygons. glBlendFunc does not affect glClear.

Examples

Transparency is best implemented using glBlendFunc(GL_SRC_ALPHA, 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 color buffer.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) is also useful for rendering antialiased points and lines.

Notes

Incoming (source) alpha is correctly thought of as a material opacity, ranging from 1.0 (kA), representing complete opacity, to 0.0 (0), representing complete transparency.

Errors

GL_INVALID_ENUM is generated if either sfactor or dfactor is not an accepted value.

Copyright

Copyright © 2003 Silicon Graphics, Inc.
This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

See Also

glAlphaFunc, glClear, glDepthFunc, glEnable, glLogicOp, glStencilFunc

Example

 glEnable(GL_BLEND);  // Enable blending.
  glBlendFunc(GL_ONE, GL_ZERO);  //  Just the source image.
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

 // Alpha blending.
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd();