How I can add transparency to simple vertex objects in Direct3D?
I used information from here with no result: https://learn.microsoft.com/ru-ru/windows/win32/direct3d9/vertex-alpha
I initialize device like this:
PresentParameters presentParams = new PresentParameters();
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.DeviceWindowHandle = this.Handle;
presentParams.BackBufferFormat = Format.A8R8G8B8;
presentParams.PresentFlags = PresentFlags.LockableBackBuffer;
presentParams.BackBufferWidth = this.ClientSize.Width;
presentParams.BackBufferHeight = this.ClientSize.Height;
presentParams.Windowed = true;
var device = new Device(_direct3d, 0, DeviceType.Hardware, this.Handle, _createFlags, presentParams);device.SetRenderState(RenderState.Lighting, false);
// ..tried different variations of flags here with no result..
device.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Color1);
var surface = device.GetBackBuffer(0, 0);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
Vertex class declaration:
[StructLayout(LayoutKind.Sequential)]
public struct ColoredVertex : IEquatable<ColoredVertex>
{
public Vector3 Position { get; set; }
public int Color { get; set; }
public static VertexElement[] Format
{
get
{
return new VertexElement[] {
new VertexElement(0,0,DeclarationType.Float3,DeclarationMethod.Default,DeclarationUsage.PositionTransformed,0),
new VertexElement(0,12,DeclarationType.Color,DeclarationMethod.Default,DeclarationUsage.Color,0),
VertexElement.VertexDeclarationEnd
};
}
}
public ColoredVertex(Vector3 position, int color)
: this()
{
Position = position;
Color = color;
}
Then I draw primitives. Color contains alpha which is 50 for a second triangle so I hope the triangle will be transparent.. but no
_device.BeginScene();
var colorN1 = Color.FromArgb(255, 100, 100, 100);
var triangleN1 = new ColoredVertex[] {
new ColoredVertex(new Vector3(10f, 10f, 0.0f), colorN1.ToArgb()),
new ColoredVertex(new Vector3(1000.0f, 10.0f, 0.0f), colorN1.ToArgb()),
new ColoredVertex(new Vector3(1000f, 800f, 0.0f), colorN1.ToArgb()),
};
using (var decl = new VertexDeclaration(_device, ColoredVertex.Format))
{
_device.VertexFormat = VertexFormat.Diffuse;
_device.VertexDeclaration = decl;
_device.DrawUserPrimitives<ColoredVertex>(PrimitiveType.TriangleList, 1, triangleN1);
}
var colorN2 = Color.FromArgb(50, 100, 0, 0);
var triangleN2 = new ColoredVertex[] {
new ColoredVertex(new Vector3(100f, 100f, 1.0f), colorN2.ToArgb()),
new ColoredVertex(new Vector3(800.0f, 100.0f, 1.0f), colorN2.ToArgb()),
new ColoredVertex(new Vector3(700f, 900f, 1.0f), colorN2.ToArgb()),
};
using (var decl = new VertexDeclaration(_device, ColoredVertex.Format))
{
_device.VertexFormat = VertexFormat.Diffuse;
_device.VertexDeclaration = decl;
_device.DrawUserPrimitives<ColoredVertex>(PrimitiveType.TriangleList, 1, triangleN2);
}
_device.EndScene();
_device.Present();
These RenderState flags did the magic: