# Why isn't my alpha blending working?

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm a directx starter, so apologies if this is a bit basic: I have a few objects drawn in 3d space. When I draw them, I do this: material.Diffuse = Color.FromArgb(128, R, G, B) Device3D.Material = material In my device setup I have this (I've tried many other things as well): Device3D.RenderState.AlphaBlendEnable = True Device3D.RenderState.AlphaSourceBlend = Blend.SourceAlpha Device3D.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha Device3D.RenderState.BlendOperation = BlendOperation.Add I expect that as I rotate my viewing position, and shapes overlap, I should see 1/2 of the front shape and 1/2 of what is behind it, but everything is still solid. What have I missed? Thanks

##### Share on other sites
Quote:
 Original post by ibelchamber Device3D.RenderState.AlphaBlendEnable = True Device3D.RenderState.AlphaSourceBlend = Blend.SourceAlpha Device3D.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha Device3D.RenderState.BlendOperation = BlendOperation.Add

replace:
Device3D.RenderState.AlphaSourceBlend = Blend.SourceAlpha

with:
Device3D.RenderState.SourceBlend = Blend.SourceAlpha
Device3D.RenderState.DestinationBlend = Blend.InvSourceAlpha

:)

##### Share on other sites
Thanks, there's definately some transparency going on now, but maybe I need something else- I can see shape A through shape B, but if I rotate my viewing position, I can't see shape B through shape A.

I have enabled the depth stencil with:
PresentParams.AutoDepthStencilFormat = DepthFormat.D24S8

and the objects appear in the right order from any angle, do I have to do something eles to get the transparency working correctly from all viewing positions?

Many thanks

##### Share on other sites
That could be a culling problem? Is it possible that maybe you can't see it because on looking from the opposite direction it isn't visible at all?

##### Share on other sites
Quote:
 Original post by rjacketsThat could be a culling problem? Is it possible that maybe you can't see it because on looking from the opposite direction it isn't visible at all?

No, I can see shape B, but not through shape A, it's like A is not transparent to B, but B is transparent to A

Thanks

##### Share on other sites
You also need to disable Z buffer, because if you draw closer triangle first, second one will fail depth test, and wont be drawn at all...

##### Share on other sites
When rendering alphablended objects you need to render them back to front for proper blending.

##### Share on other sites
Many thanks for your help, but I'm a bit surprised- is it really the case that I have to work out the distances of all my meshes from the viewing point, and draw them in decreasing distance (just what I thought I had to do to get depth correct before I discovered depth stencils), to make transparency work correctly? What if I have meshes overlapping, so that part of mesh A is behind mesh B and part is in front? Is it really not possible to get transparency working correctly in this case?

Thanks.

##### Share on other sites
It's not perfect, but disabling z-writing before u render stuff with alpha values ussually looks pretty decent and saves u from sorting everything.

	m_pD3DDevice->GetRenderState(D3DRS_ZWRITEENABLE,  FALSE);`

Matt Hughson

##### Share on other sites
Typically, games cheat. There are a lot of different methods and it depends what you're doing. The suggestion of disabling ZWrites can work well in some cases.

The only way to get transparent meshes to blend perfectly in arbitrary situations is to break them down and handle them 1 triangle at a time. Obviously this isn't feasible.

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632176
• Total Posts
3004590

×