绘制三角形2

public partial class MainForm : Form
{
   public MainForm()
   {
       InitializeComponent();
 
       this.InitialDX();
   }
 
   private Device device;
   private void InitialDX()
   {
       PresentParameters presentParams = new PresentParameters();
       presentParams.Windowed = true;
       presentParams.SwapEffect = SwapEffect.Discard;
 
       device = new Device(
           0,
           DeviceType.Hardware,
           this,
           CreateFlags.SoftwareVertexProcessing,
           presentParams);
 
       device.RenderState.Lighting = false;
       device.RenderState.CullMode = Cull.None;
 
       //摄像机和投影设置
       Matrix camaraMx = Matrix.LookAtLH(
           new Vector3(0, 0, -150),
           new Vector3(0, 0, 0),
           new Vector3(0, 1, 0));
 
       Matrix projectionMx = Matrix.PerspectiveFovLH(
           (float)Math.PI / 4,
           this.Width / this.Height,
           10,
           200);
 
       device.Transform.Projection = projectionMx;
       device.Transform.View = camaraMx;
   }
 
   protected override void OnPaint(PaintEventArgs e)
   {
       base.OnPaint(e);
       this.DrawMyDX();
   }
 
   private void DrawMyDX()
   {
       device.Clear(ClearFlags.Target,
           Color.AliceBlue,
           1f,
           0);
       device.BeginScene();
       this.DrawMyGraphics();
       device.EndScene();
       device.Present();
   }
 
   private void DrawMyGraphics()
   {
       CustomVertex.PositionColored[] vertics = new CustomVertex.PositionColored[3];
       vertics[0].Position = new Vector3(
           -50, 0, 0);
       vertics[0].Color = Color.Red.ToArgb();
 
       vertics[1].Position = new Vector3(
           0, 30, 0);
       vertics[1].Color = Color.Green.ToArgb();
 
       vertics[2].Position = new Vector3(
           50, 0, 0);
       vertics[2].Color = Color.Blue.ToArgb();
 
       device.VertexFormat = CustomVertex.PositionColored.Format;
       device.DrawUserPrimitives(PrimitiveType.TriangleList,
           1,
           vertics);
   }
}

效果图:

image

原文地址:https://www.cnblogs.com/sharpfeng/p/1951909.html