*****************************************************************
* APRON TUTORIALS PRESENTED BY MORROWLAND *
*****************************************************************
* Project Name : Blending *
* Project Description : Blending in OpenGL *
* Project Type : OpenGL *
* Author : Ronny Andr� Reierstad *
* Web Page : www.morrowland.com *
* E-Mail : apron@morrowland.com *
* Version : English (UK) *
* Date : 07.02.2004 *
*****************************************************************
This tutorial demonstrates blending in OpenGL.
Blending in OpenGL is very easy.. You will be able to blend
textures, colors, etc.. Into nice looking special effects..
Enable blending with glEnable(GL_BLEND);
Setup different blending modes with glBlendFunc(source, destination);
The most natural way for you to think of blending operations is to view the
RGB components of a fragment as representing its color, and the alpha component
as representing opacity. Thus, transparent or translucent surfaces have lower
opacity than opaque ones. For example, if you�re viewing an object through green
glass, the color you see is partly green from the glass and partly the color of the
object. The percentage varies depending on the transmission properties of the glass:
If the glass transmits 80 percent of the light that strikes it (that is, has an
opacity of 20 percent), the color you see is a combination of 20 percent glass color
and 80 percent of the color of the object behind it. You can easily imagine
situations with multiple translucent surfaces. If you look at an automobile,
for instance, its interior has one piece of glass between it and your viewpoint;
some objects behind the automobile are visible through two pieces of glass.
Blending occurs after your scene has been rasterized and converted to fragments,
but just before the final pixels are drawn in the framebuffer. Alpha values can
also be used in the alpha test to accept or reject a fragment based on its alpha
value.
Without blending, each new fragment overwrites any existing color values in the
framebuffer, as though the fragment is opaque. With blending, you can control
how much of the existing color value should be combined with the new fragment�s
value. Thus, you can use alpha blending to create a translucent fragment, one that
lets some of the previously stored color value "show through." Color blending lies
at the heart of techniques such as transparency, digital compositing, and painting.
Use these constants for blending in OpenGL:
-----------------------------------------------------------------------------
Constant: Relevant Factor: Computed Blend Factor:
-----------------------------------------------------------------------------
GL_ZERO source or destination (0, 0, 0, 0)
GL_ONE source or destination (1, 1, 1, 1)
GL_DST_COLOR source (Rd, Gd, Bd, Ad)
GL_SRC_COLOR destination (Rs, Gs, Bs, As)
GL_ONE_MINUS_DST_COLOR source (1, 1, 1, 1)-(Rd, Gd, Bd, Ad)
GL_ONE_MINUS_SRC_COLOR destination (1, 1, 1, 1)-(Rs, Gs, Bs, As)
GL_SRC_ALPHA source ordestination (As, As, As, As)
GL_ONE_MINUS_SRC_ALPHA source or destination (1, 1, 1, 1)-(As, As, As, As)
GL_DST_ALPHA source or destination (Ad, Ad, Ad, Ad)
GL_ONE_MINUS_DST_ALPHA source or destination (1, 1, 1, 1)-(Ad, Ad, Ad, Ad)
GL_SRC_ALPHA_SATURATE source (f, f, f, 1); f=min(As, 1-Ad)
-----------------------------------------------------------------------------
(Table from the book OpenGL Programming Guide.)
-----------------------------------------------------------------
Regards
Ronny Andr� Reierstad
www.morrowland.com
apron@morrowland.com