DrawTriangle.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:4k
源码类别:

DirextX编程

开发平台:

C#

  1. ////////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using Microsoft.DirectX;
  15. using Microsoft.DirectX.Direct3D;
  16. namespace 绘制一个三角形
  17. {
  18.     public partial class DrawTriangle : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.         public DrawTriangle()
  22.         {
  23.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  24.             this.Text = "绘制一个三角形";//指定窗体标题
  25.         }
  26.         public bool InitializeDirect3D()
  27.         {
  28.             try
  29.             {
  30.                 PresentParameters presentParams = new PresentParameters();
  31.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  32.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  33.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  34.                 return true;
  35.             }
  36.             catch (DirectXException e)
  37.             {
  38.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  39.                 return false;
  40.             }
  41.         }
  42.         public void Render()
  43.         {
  44.             if (device == null)   //如果device为空则不渲染
  45.             {
  46.                 return;
  47.             }
  48.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  49.             device.BeginScene();
  50.             //在此添加渲染图形代码
  51.             CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[3];//定义顶点
  52.             vertices[0].Position = new Vector4(150f, 400f, 0f, 1f);
  53.             vertices[0].Color = Color.Red.ToArgb();
  54.             vertices[1].Position = new Vector4(this.Width / 2 , 100f, 0f, 1f);
  55.             vertices[1].Color = Color.Green.ToArgb();
  56.             vertices[2].Position = new Vector4(this.Width-150f, 400f, 0f, 1f);
  57.             vertices[2].Color = Color.Yellow.ToArgb();
  58.             device.VertexFormat = CustomVertex.TransformedColored.Format;
  59.             device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
  60.             device.EndScene();
  61.             device.Present();
  62.         }
  63.         static void Main()
  64.         {
  65.             DrawTriangle DrawTriangle = new DrawTriangle(); //创建窗体对象
  66.             if (DrawTriangle.InitializeDirect3D() == false) //检查Direct3D是否启动
  67.             {
  68.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  69.                 return;
  70.             }
  71.             DrawTriangle.Show(); //如果一切都初始化成功,则显示窗体
  72.             while (DrawTriangle.Created) //设置一个循环用于实时更新渲染状态
  73.             {
  74.                 DrawTriangle.Render(); //保持device渲染,直到程序结束
  75.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  76.             }
  77.         }
  78.     }
  79. }