Monday, May 30, 2011

glBlendEquationSeparate example c c++ java objc

Name

glBlendEquationSeparate — set the RGB blend equation and the alpha blend equation separately

C Specification

void glBlendEquationSeparate(GLenum  modeRGB,
GLenum  modeAlpha);

Parameters

modeRGB
specifies the RGB blend equation, how the red, green, and blue components of the source and destination colors are combined. It must be GL_FUNC_ADDGL_FUNC_SUBTRACT,GL_FUNC_REVERSE_SUBTRACTGL_MINGL_MAX.
modeAlpha
specifies the alpha blend equation, how the alpha component of the source and destination colors are combined. It must be GL_FUNC_ADDGL_FUNC_SUBTRACT,GL_FUNC_REVERSE_SUBTRACTGL_MINGL_MAX.

Description of glBlendEquationSeparate

The blend equations determines how a new pixel (the ''source'' color) is combined with a pixel already in the framebuffer (the ''destination'' color). This function specifies one blend equation for the RGB-color components and one blend equation for the alpha component.
The blend equations use the source and destination blend factors specified by either glBlendFunc or glBlendFuncSeparate. See glBlendFunc or glBlendFuncSeparate for a description of the various blend factors.
In the equations that follow, source and destination color components are referred to as R s G s B s A s and R d G d B d A d , respectively. The result color is referred to asR r G r B r A r . The source and destination blend factors are denoted s R s G s B s A and d R d G d B d A , respectively. For these equations all color components are understood to have values in the range 0 1 . (glBlendEquationSeparate)
ModeRGB ComponentsAlpha Component
GL_FUNC_ADD
Rr = R s  s R + R d  d R
Gr = G s  s G + G d  d G
Br = B s  s B + B d  d B
Ar = A s  s A + A d  d A
GL_FUNC_SUBTRACT
Rr = R s  s R - R d  d R
Gr = G s  s G - G d  d G
Br = B s  s B - B d  d B
Ar = A s  s A - A d  d A
GL_FUNC_REVERSE_SUBTRACT
Rr = R d  d R - R s  s R
Gr = G d  d G - G s  s G
Br = B d  d B - B s  s B
Ar = A d  d A - A s  s A
GL_MIN
Rr = min  R s R d
Gr = min  G s G d
Br = min  B s B d
Ar = min  A s A d
GL_MAX
Rr = max  R s R d
Gr = max  G s G d
Br = max  B s B d
Ar = max  A s A d

The results of these equations are clamped to the range 0 1 .
The GL_MIN and GL_MAX equations are useful for applications that analyze image data (image thresholding against a constant color, for example). The GL_FUNC_ADD equation is useful for antialiasing and transparency, among other things.
Initially, both the RGB blend equation and the alpha blend equation are set to GL_FUNC_ADD.

Notes

glBlendEquationSeparate is available only if the GL version is 2.0 or greater.
The GL_MIN, and GL_MAX equations do not use the source or destination factors, only the source and destination colors.

Errors

GL_INVALID_ENUM is generated if either modeRGB or modeAlpha is not one of GL_FUNC_ADDGL_FUNC_SUBTRACTGL_FUNC_REVERSE_SUBTRACTGL_MAX, or GL_MIN.
GL_INVALID_OPERATION is generated if glBlendEquationSeparate is executed between the execution of glBegin and the corresponding execution of glEnd.

Associated Gets

glGet with an argument of GL_BLEND_EQUATION_RGB
glGet with an argument of GL_BLEND_EQUATION_ALPHA

Copyright

Copyright © 2006 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999.http://opencontent.org/openpub/.

Example of glBlendEquationSeparate

_checkGLErrors();
m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_tempTexture.glTarget, m_tempTexture.glTexID );
assert( m_fbo.IsValid() );
m_fbo.Bind();
_checkGLErrors();
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT );
// C = srcC * srcA + dstC = C * A + C’ * A’ + …
// A = srcA + dstA = A + A’ + A” + …
glBlendFuncSeparate( GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE );
glBlendEquationSeparate( GL_FUNC_ADD, GL_FUNC_ADD );
for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {
(*i)->bake();
}
m_fbo.Unattach( GL_COLOR_ATTACHMENT0_EXT );
m_fbo.Disable();
_checkGLErrors();
// divide color by alpha
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
—> glDisable(GL_BLEND); <---
glBlendFunc( GL_ONE, GL_ZERO );
procInf.imageOp( m_tempTexture, m_transferFunction, m_program );
// render again this time to figure out the max alpha value
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_transferFunction.glTarget, m_transferFunction.glTexID );
assert( m_fbo.IsValid() );
m_fbo.Bind();
_checkGLErrors();
---> glEnable( GL_BLEND ); <---
glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE );
glBlendEquation( GL_MAX );
for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {
(*i)->bake();
}
m_fbo.Disable();