Farnk

I am trying to draw a textured transparent square. Here is the drawing code:

CustomVertex.PositionColoredTextured[] verts = new CustomVertex.PositionColoredTextured[4];

verts[0] = new CustomVertex.PositionColoredTextured(-10.0f, 10.0f, 0, Color.FromArgb(128, 255, 255, 255).ToArgb(), 0, 0);
verts[1] = new CustomVertex.PositionColoredTextured(-10.0f, -10.0f, 0, Color.FromArgb(128, 255, 255, 255).ToArgb(), 0, 1);
verts[2] = new CustomVertex.PositionColoredTextured(10.0f, 10.0f, 0, Color.FromArgb(128, 255, 255, 255).ToArgb(), 1, 0);
verts[3] = new CustomVertex.PositionColoredTextured(10.0f, -10.0f, 0, Color.FromArgb(128, 255, 255, 255).ToArgb(), 1, 1);

device.SetTexture(0, grassTexture);

device.VertexFormat = CustomVertex.PositionColoredTextured.Format;

device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, verts);


And I have the following to enable alpha blending:

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


It seems like this should work from the tutorials I've seen and since I'm setting the alpha of the color for each vertex to 128, but unfortunately this draws the square opaque. I know that alpha blending is working because If I load a texture with an alpha channel it is transparent where it's supposed to be, and if I use CustomVertex.PositionColored it also works fine.

I read on the MSDN that Renderstate.DiffuseMaterialSource might help, but I've tried all three settings and this did not help... so I must be doing something wrong... again...


Re: Game Technologies: Graphics Another Alpha Blend Question in Managed DirectX 9

Farnk

Ok so I figured this out, and figured I'd post the answer just in case any other newbies have the same problem. Here is the code:

// setup the correct texture arguments
device.TextureState[0].ColorOperation = TextureOperation.Modulate;
device.TextureState[0].ColorArgument0 = TextureArgument.Diffuse;
device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;

device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
device.TextureState[0].AlphaArgument0 = TextureArgument.Diffuse;
device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;

I found this in a tutorial for regular DirectX in c++ and it took me a little while to figure out how to implement it in managed DirectX

This code basically tells directx how you want it to blend the texture with the diffuse, both the color and alpha. The code above will make it render with the default behavior of OpenGL, it will modulate the color and alpha from the texture with the color and alpha of the diffuse component of the vertex.