• 绘制三角形


    代码:

    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;

  • 相关阅读:
    代码301的弊端和处理方法
    seo如何发外链
    seo工程师是什么,需要什么技能?
    优化一个关键词到百度首页需要多少钱
    搜索引擎优化顾问机构好不好
    谷歌分析(GA)新版的有哪些改变
    什么是相关性链接/网站相关性
    JS的部分部分疑问和小结
    Groovy学习:第一章 用Groovy简化Java代码
    SpringMVC学习(8):国际化
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/1950635.html
Copyright © 2020-2023  润新知