绘制三角形

代码:

public partial class MainForm : Form
{
   public MainForm()
   {
       InitializeComponent();
       this.InitialDX();
   }
 
   private Device device = null;
 
   private void InitialDX()
   {
       PresentParameters presentParams = new PresentParameters();
       presentParams.Windowed = true;
       presentParams.SwapEffect = SwapEffect.Discard;
 
       device = new Device(
           0,
           DeviceType.Hardware,
           this,
           CreateFlags.SoftwareVertexProcessing,
           presentParams);
   }
 
   protected override void OnPaint(PaintEventArgs e)
   {
       base.OnPaint(e);
       this.DrawDX();
   }
 
   private void DrawDX()
   {
       if (this.device == null)
           return;
 
       this.device.Clear(ClearFlags.Target,
           Color.AliceBlue,
           1f,
           0);
 
       this.device.BeginScene();
       //绘制DX图形
       this.DrawMyGraphics();
       this.device.EndScene();
 
       this.device.Present();//显示图形
   }
 
   private void DrawMyGraphics()
   {
       CustomVertex.TransformedColored[] vertex =
           new CustomVertex.TransformedColored[3];
 
       vertex[0].Position = new Vector4(
           0,
           this.Height,
           0,
           0);
       vertex[0].Color = Color.Red.ToArgb();
 
       vertex[1].Position = new Vector4(
           this.Width / 2,
           0,
           0,
           0);
       vertex[1].Color = Color.Green.ToArgb();
 
       vertex[2].Position = new Vector4(
           this.Width,
           this.Height,
           0,
           0);
       vertex[2].Color = Color.Blue.ToArgb();
 
       this.device.VertexFormat = CustomVertex.TransformedColored.Format;
       this.device.DrawUserPrimitives(PrimitiveType.TriangleList,
           1,
           vertex);
   }
}

 

结果:

image

绘制三角形的代码主要在DrawMyGraphics中。需要注意的是顶点的索引是顺时针的,如果是逆时针,结果就没有三角形了,需要进行特殊设置。

device.RenderState.CullMode = Cull.None;

转载于:https://www.cnblogs.com/sharpfeng/archive/2011/02/10/1950635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值