# Why isn't my alpha blending working?

This topic is 4724 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.

##### Share on other sites
Think about the long run. How many full-size meshes do you really intend on rendering with alpha anyway? You should limit the number from the get-go, because too much alpha will make a player's eyes bug out. At maybe 10 full-size meshes on the screen with alpha, ordering them is no big deal. If you use collision detection to keep them from overlapping, it's even better, because you can use the center point of a bounding sphere, box, cone, mesh, whatever you want, to do the ordering. If we're talking about thousands of particles, ordering isn't really important, because they are usually so small you can't tell what's in front of what. As far as the object A part of object B thing, that's a tough one. It can get complicated. I recommend using the collision detector to keep those objects apart.

If you must take that into account though, there are some ways to keep it from being too complicated. Stick to convex geometry instead of concave. Keep low poly counts. You can then safely sort by face. There are some shortcuts such as using culling and determining the front/back faces of a mesh, and rendering them in a certain order but I don't remember the details right now.

Good luck,
Chris