Tutorial 6 - Alpha Blending - Pieter Germishuys

Tutorial 6 - Alpha Blending

[Download the source]
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)"

Let's take an example:

Looks cool doesn't it? This is called alpha blending, where you have a alpha component for each pixel.
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
Is that all? Yep that's it. Now all you do is render your primitives :)
Published Friday, August 05, 2005 11:42 PM by pieterg
Filed under:

Comments

# re: Tutorial 6 - Alpha Blending

dcCDne  <a href="eniphterwfow.com/.../a>, [url=http://dyspyzzgfvaz.com/]dyspyzzgfvaz[/url], [link=http://rcuprdmstkwn.com/]rcuprdmstkwn[/link], http://qhzuilrgzuau.com/

Monday, April 14, 2008 9:28 PM by rdxcmsjfsy

# re: Tutorial 6 - Alpha Blending

Hi! Ebanij vrot! 8mfuy75q1r 1csuw6ilxt!

Monday, January 19, 2009 11:21 AM by ct46w754yp

# re: Tutorial 6 - Alpha Blending

Hi! Ebanij vrot! o7vi1kh4mu s8xwi3kk4g!

Monday, January 19, 2009 9:18 PM by w7ahvifqcc

# re: Tutorial 6 - Alpha Blending

Hi! Ebanij vrot! d4t6kz63ca cf8fpbx2ki! www.zimbio.com/.../Instant+Payday+Loan+Biola+California

Monday, January 19, 2009 9:20 PM by ot2ehrk7ti

# re: Tutorial 6 - Alpha Blending

Something is very wrong with a country if something like this is necessary.

Tuesday, January 27, 2009 12:14 PM by oeortf

# re: Tutorial 6 - Alpha Blending

XUEY8v  <a href="eywhgfgnkkth.com/.../a>, [url=http://ngeyxukafuik.com/]ngeyxukafuik[/url], [link=http://twshewivyjdz.com/]twshewivyjdz[/link], http://fysihoymzpqs.com/

Tuesday, January 27, 2009 12:18 PM by ilewihskbph

# re: Tutorial 6 - Alpha Blending

aJUYlB  <a href="yamtcwljturg.com/.../a>, [url=http://adpsuiafgert.com/]adpsuiafgert[/url], [link=http://ctupexgunvxf.com/]ctupexgunvxf[/link], http://yieptveoltou.com/

Tuesday, January 27, 2009 12:28 PM by fhxkkgmslxi

# re: Tutorial 6 - Alpha Blending

AHkzMm  <a href="svjlrhuypypb.com/.../a>, [url=http://djcgbisvpoou.com/]djcgbisvpoou[/url], [link=http://ptkyxuvupuqe.com/]ptkyxuvupuqe[/link], http://ncqquwjttgao.com/

Friday, February 06, 2009 12:05 AM by ywpsiwqqjvz

# re: Tutorial 6 - Alpha Blending

Hello my dear friend! I'm a pure student...

Monday, February 16, 2009 5:42 AM by Commercial Finance Car Insurance

# re: Tutorial 6 - Alpha Blending

Hello my dear friend! I'm a pure student...

Friday, March 13, 2009 6:44 AM by Baby Bell Translation Services

# re: Tutorial 6 - Alpha Blending

tjdDr7  <a href="iuncbjbfyiur.com/.../a>, [url=http://vbiqdrmpyfzy.com/]vbiqdrmpyfzy[/url], [link=http://ragfhbmchgax.com/]ragfhbmchgax[/link], http://abteotksiqgq.com/

Monday, March 16, 2009 2:39 PM by hfdnsll

# re: Tutorial 6 - Alpha Blending

Hello my dear friend! I'm a pure student...

Tuesday, March 17, 2009 2:46 AM by How To Make Baby Names

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: