Question

I am using C #. I need to draw points using DirectX and they should be semitransparent. I declared vertices to be transformedColored and set the color value to be:

 vertices[i].Color = Color.FromArgb(20,0,0,255).ToArgb();

This should be quite transparent blue but what I got was opaque blue. Whatever value I use for the alpha value, it always gets fully opaque. Any ideas why? I hope the transformedcolored color field supports alpha value.

Thanks in advance.

The drawing code is:

device.Clear(ClearFlags.Target, System.Drawing.Color.White, 1.0f, 0);

device.RenderState.AlphaBlendEnable = true;

device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;

device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;

device.RenderState.BlendOperation = BlendOperation.Add;

CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[N];

for (int i = 0; i < N; i++)
{
   vertices[i].Position = new Vector4(2.5f + (g_embed[i, 0] - minx) * (width - 5.0f) / maxx, 2.5f + (g_embed[i, 1] - miny) * (height - 5.0f) / maxy, 0f, 1f);//g_embed, minx, width,maxx, miny,height, maxy are all predifined

   vertices[i].Color = Color.FromArgb(20, 0, 0, 255).ToArgb();
}

 device.BeginScene();

 device.VertexFormat = CustomVertex.TransformedColored.Format;

 device.DrawUserPrimitives(PrimitiveType.PointList, N, vertices);

 device.EndScene();

 device.Present();

 this.Invalidate();
Was it helpful?

Solution

Your renderstates are used for premultiplied alpha. Using a real alpha-channel needs the following settings (Blendenumerationdoc):

graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
graphics.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;

It creates following the formular, where a is your alpha of the color: a * SourceColor + (1-a) * DestColor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top