Sprite alpha blending

Started by
3 comments, last by DrunkenHyena 17 years, 1 month ago
I have a sprite and a texture with white letters and a blue background (background RGB is 0,0,255) When drawing the sprite I want to ignore the blue pixels so only the white letter will appear on the screen. What is simplest way to achieve this ? thx. [Edited by - DesignerX on March 15, 2007 3:00:52 AM]
There is nothing that can't be solved. Just people that can't solve it. :)
Advertisement
The term you're looking for is "colour key".
NextWar: The Quest for Earth available now for Windows Phone 7.
Thx, but you could have been more specific.
When I create the texture I specify an ARGB value. What this value means ?
If I use D3DCOLOR_ARGB(0, 0, 0, 255) does it mean that the blue color will have an alpha value of 0 ??? (Although I tried it didn't work)

My texture format is jpg. should it be something else ? are the pixel in the jpg get a default alpha value of 255 ?

thx.
There is nothing that can't be solved. Just people that can't solve it. :)
I'll assume you're loading your textures with D3DXCreateTextureFromFileEx(). From the D3D documentation:

Quote:
ColorKey
[in] D3DCOLOR value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys. Thus, for opaque black, the value would be equal to 0xFF000000.


That means, that the colour your specify in this parameter is the colour used for colour-keying. So if you specify blue, (255, 0, 0, 255), then all pure blue pixels will be rendered transparent. The colour key doesn't actually change the alpha value of the texture.

The texture format of your texture will be converted to whatever you specified it to in D3DXCreateTextureFromFileEx(). If you didn't specify one, it'll be taken from the image. If, for example, you load an image without an alpha channel and specify A8R8G8B8 format, I'm pretty sure the alpha values are filled with white.

To use colourkeying you probably have to set some states. I'm not sure exactly what it involves, perhaps the D3D documentation can provide more info.
NextWar: The Quest for Earth available now for Windows Phone 7.
Quote:Original post by Sc4Freak
I'll assume you're loading your textures with D3DXCreateTextureFromFileEx(). From the D3D documentation:

Quote:
ColorKey
[in] D3DCOLOR value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys. Thus, for opaque black, the value would be equal to 0xFF000000.


That means, that the colour your specify in this parameter is the colour used for colour-keying. So if you specify blue, (255, 0, 0, 255), then all pure blue pixels will be rendered transparent. The colour key doesn't actually change the alpha value of the texture.

Passing blue as the colour key to D3DXCreateTextureFromFileEx will load the texture and change all of the blue pixels to transparent black. it very much does change the texture. This is important to know because you need to specify a texture format that is capable of storing alpha.

To actually render it as transparent you'll need to enable AlphaTest/AlphaBlending either directly or via D3DXSprite, depending on what you're doing.
Stay Casual,KenDrunken Hyena

This topic is closed to new replies.

Advertisement