如何使用SharpDX Direct3D9 alpha顶点?
我目前正在尝试使用DrawPrimitives
NuGeT在C#中使用Direct3D9
中的SharpDX
方法显示一些矩形。我希望矩形的不透明度不是最大,但是尽管我使用RawColorBGRA
,但Alpha字段似乎并没有改变不透明度。
这是代码的一部分:
ColorBGRA:
public static ColorBGRA lightBlue = new ColorBGRA
{
R = 0x00,
G = 0x99,
B = 0xcc,
A = 0x33
};
我的DirectX类:
using System;
using SharpDX;
using SharpDX.Direct3D9;
using SharpDX.Mathematics.Interop;
namespace DirectXProject
{
public class DX
{
public Device device;
public RawRectangle fontDimension;
VertexBuffer vertices;
VertexDeclaration vertexDecl;
VertexElement[] vertexElems;
struct Vertex
{
public Vector4 Position;
public ColorBGRA Color;
}
public DX(IntPtr handle, int width, int height)
{
device = new Device(
new Direct3D(),
0,
DeviceType.Hardware,
handle,
CreateFlags.HardwareVertexProcessing,
new PresentParameters(width, height));
}
public void RunLoop()
{
lock (this)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, DXColor.wheat, 1.0f, 0);
device.BeginScene();
DrawFillRectangle(vertices, 20, 20, 40, 40, DXColor.lightBlue);
device.EndScene();
device.Present();
}
}
public void DrawFillRectangle(VertexBuffer vertices, float x1, float y1, float x2, float y2, ColorBGRA color)
{
vertices = new VertexBuffer(device, 4 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
new Vertex() { Color = color, Position = new Vector4(x1, y1, 0.5f, 1.0f) },
new Vertex() { Color = color, Position = new Vector4(x2, y1, 0.5f, 1.0f) },
new Vertex() { Color = color, Position = new Vector4(x2, y2, 0.5f, 1.0f) },
new Vertex() { Color = color, Position = new Vector4(x1, y2, 0.5f, 1.0f) }
});
vertices.Unlock();
vertexElems = new[] {
new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
VertexElement.VertexDeclarationEnd
};
vertexDecl = new VertexDeclaration(device, vertexElems);
device.SetStreamSource(0, vertices, 0, 20);
device.VertexDeclaration = vertexDecl;
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
}
}
}
尽管Alpha字段中有Ox33
,该代码仍提供了完整的不透明矩形。
还有其他方法可以改变不透明度吗? 感谢您的阅读。