Tutorial 6 - Alpha BlendingBy Pieter Germishuys, January 4 2006 |
Today we are going to discuss a very interesting topic, Alpha blending.
What is alpha blending?
"The real world is composed of transparent, translucent, and opaque objects. Alpha blending is a technique for adding transparency information for translucent objects. It is implemented by rendering polygons through a stipple mask whose on-off density is proportional to the transparency of the object. The resultant color of a pixel is a combination of the foreground and background color. Typically, alpha has a normalized value of 0 to 1 for each color pixel. new pixel = (alpha)(pixel A color) + (1 - alpha)(pixel B color)"
How does it work?
Pretty boring stuff
Let's take 2 pixels. One is already in the back buffer (Color buffer) and one is being sent to the back buffer (Color buffer).
Source's Pixel Color (The pixel on the triangle) = (127, 255, 0, ???0); //This being an alpha value of 127 (50%) and Red (255), Green(0), Blue(0)
Destination's Pixel Color (The pixel in the back buffer) = (127, ???0, 0, 255); //This being an alpha value of 127 (50%) and Red (0), Green(0), Blue(255)
New Pixel Color = Source*Alpha/max_Alpha + Destination*(max_Alpha-Alpha)/max_Alpha
New Pixel Color = 255 * 127/255 + 255 * (255 - 127) / 127
New Pixel Color = 50% Red + 100% Blue (I might be off with the calculation though)
This means the 2 pixels get blended and placed in the back buffer (Color buffer)
How do I make it work?
The interesting stuff
We use the simple set of render states that DirectX so happily offers us.
deviceRenderState.SourceBlend = Blend.SourceAlpha; //set the source to be the source's alpha device.RenderState.DestinationBlend = Blend.InvSourceAlpha; //set the destination to be the inverse of the source's alpha device.RenderState.AlphaBlendEnable = true; //enable |
Files for this tutorial
Filename | Size |
? tut6.rar | 185.3 KB |