Why isn't my alpha blending working?

Started by
9 comments, last by Supernat02 19 years, 1 month ago
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
Advertisement
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
Device3D.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha

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

:)
Bulma
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
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?
Quote:Original post by rjackets
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?



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
You also need to disable Z buffer, because if you draw closer triangle first, second one will fail depth test, and won`t be drawn at all...
Bulma
When rendering alphablended objects you need to render them back to front for proper blending.
Stay Casual,KenDrunken Hyena
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.
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
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
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.
Stay Casual,KenDrunken Hyena

This topic is closed to new replies.

Advertisement