• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Alpha blending stuff

## Recommended Posts

I have recently started programming (managed) directx, and I am quite happy with it. Most things I can findout for myself, form books, google and msdn. But now I rean into a problem which none of these can help me with. Anyway to the point... Take a look at my 3d experiment so far I am trying to implement a simple UI, the arrows are to move the camera, and the other Icon is to rotate the camera. I already did that part, so everything is right about good. Except for the fact that the icons should have transparent parts. The icons are simple Transformed Textured quads (is it even possible to alpha blend those?), which get renderd last so they are on top. Steps I already took are the following: -First I made sure I used a texture format that has A8 (i used PNG first and dds later but should make no difference) -Then I double checked my render states. I use these now: RenderState.AlphaBlendEnable = True RenderState.AlphaSourceBlend = Blend.SourceAlpha RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha dev.TextureState(0).AlphaArgument1 = TextureArgument.TextureColor dev.TextureState(0).AlphaArgument2 = TextureArgument.Diffuse dev.TextureState(0).AlphaOperation = TextureOperation.Modulate These where taken from the billboarding sample which renders correctly on my machine. Now there are a lot of different sources on the internet which deal with alpha blending and nearly all of them use different settings. Some (like the billboarding sample) also use alpha testing. What is it? Should i use that? (also I saw that all the other samples I looked at used lighting, but I haven''t learned that yet so I don''t use it, is that a problem?) I''ve spent a day or so trying to figure this thing out. so any help would be apreciated.

##### Share on other sites
Alpha testing is often used with alphablending to skip pixels that are completely transparent. For example, most apps set alphatest up such that any pixels with an alpha < 8 are skipped. They''re not written to the backbuffer, or the depth buffer. Alphablending doesn''t need to be enabled for this to work, and you don''t need to use this to make alphablending work.

Lighting should only affect where the diffuse color and alpha come from. Since you''re trying to use the alpha from the texture, lighting isn''t a problem.

Everything else looks set up fine, so lets worry about your texture...

You''ve made a texture that has A8, BUT, did you put anything in the alpha channel? Does you paint package support alpha (ie: Photoshop defaults to 3 channels(RGB), but you can add another, for alpha.)

If your paint package doesn''t support it, look in your DX SDK for the texture tool. It will allow you to load one image in the color bits and another image into the alpha bits, then save that new image out.

Another simplistic option is in D3DXCreateTextureFromFileEx, to pass an appropriate value for the color key. This takes any matching input pixel and turns it transparent black (beware of filtering problems this can introduce). To make you icons be transparent, try using 0xFF000000 (solid black) as your color key.

##### Share on other sites
Also, alpha blended stuff should be rendered after all opaque items, so make sure you''re drawing those last otherwise what you could be seeing is it blended with the black background.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
Well I did chech I have an alpha channel. That''s why I also tried DirectX''s texture format.

And as for the sort order, these quads are designed to be a sort of image overlay, so it was natural to assume they would have to be renderd last. Therefore, they already are.

Can anything else be wrong?

##### Share on other sites
Try grabbing an image that you know has a valid alpha channel and replace your image with that. Something like Tree35S.dds from the SDK Media directory. If that displays alpha then your image is bad, otherwise the problem is elsewhere.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
I am stuck exactly where you are (see my post). I too then looked at the billboard sample and I too ended up with code very similar to your and I too couldn''t get it to work. All I can say is that the Sprite object included in Microsoft.DirectX.Direct3DX (had to add an extra reference to the project) solved my problem. I figured out how to make the sprite draw properly with its alpha. But I would still really like to know what''s wrong with the trianglestrip approach.

"All you need to do to learn circular logic is learn circular logic"

##### Share on other sites
After taking another look at the documentation I just figured out our problem, finally . It looks like we should be using SourceBlend and DestinationBlend instead of AlphaSourceBlend and AlphaDestinationBlend. I removed a bunch of junk code dealing with textures and other blend operations (which I see you also have) and found that these are the only 3 lines that seem to be needed:

dev.RenderState.AlphaBlendEnable = true;dev.RenderState.SourceBlend = Blend.SourceAlpha;dev.RenderState.DestinationBlend = Blend.InvSourceAlpha;

##### Share on other sites
Perfect, this fixed my problem too. I am using CustomVertex.TransformedColored to make a console-like box that comes down. My console is now transparent yay. Thanks.

-- Trivex

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978846

• 10
• 10
• 21
• 14
• 12