1.應用directx圖形庫進行開發;緩存
2.代碼:dom
public class TClass : System.Windows.Forms.Form { /// <summary> /// 設備對象,場景中全部圖形對象的父對象 /// </summary> private Device device = null; /// <summary> /// 座標系四棱錐頂點緩衝 /// </summary> VertexBuffer vertexBuffer = null; /// <summary> /// 此參數設置爲必須,它定義了要建立的Direct3D設備的表示參數,如後臺緩衝區的高度、寬度和像素格式、如何從後臺緩衝區複製到前臺緩存、以及屏幕顯示的方式等等 /// </summary> PresentParameters presentParameters; /// <summary> /// 暫停標誌 /// </summary> bool pause = false; /// <summary> /// 隨機數,用來生成隨機顏色用的 /// </summary> Random rn = new Random(); /// <summary> /// 構造函數,設置窗體大小 /// </summary> public TClass() { this.ClientSize = new System.Drawing.Size(300, 300); } /// <summary> /// 初始化繪圖環境 /// </summary> /// <returns></returns> public bool InitializeGraphics() { try { presentParameters = new PresentParameters(); //設置屏幕顯示模式爲窗口模式 presentParameters.Windowed = true; //設置如何從後臺緩衝區複製到前臺緩衝區(SwapEffect.Discard表示緩衝區在顯示後當即被捨棄,這樣能夠節省開銷) presentParameters.SwapEffect = SwapEffect.Discard; //建立一個設備 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters); //爲設備釋放訂閱事件處理 device.DeviceReset += new System.EventHandler(this.OnResetDevice); this.OnCreateDevice(device, null); this.OnResetDevice(device, null); pause = false; return true; } catch (DirectXException) { return false; } } /// <summary> /// 設備建立時創建頂點緩衝 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnCreateDevice(object sender, EventArgs e) { Device dev = (Device)sender; //建立頂點緩衝,有個頂點 vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 18, dev, 0, CustomVertex.PositionColored.Format, Pool.Default); //爲建立頂點緩存訂閱事件處理 vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer); this.OnCreateVertexBuffer(vertexBuffer, null); } /// <summary> /// 設備撤銷的事件處理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnResetDevice(object sender, EventArgs e) { Device dev = (Device)sender; //關閉剔除模式,使咱們能看見此四棱錐的前面和後面 dev.RenderState.CullMode = Cull.None; // 關閉場景裏的燈光,顯示頂點本身的顏色 dev.RenderState.Lighting = false; } /// <summary> /// 建立頂點緩存的事件處理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnCreateVertexBuffer(object sender, EventArgs e) { VertexBuffer vb = (VertexBuffer)sender; CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0, 0); //四棱錐原始的個點 Vector3 vertex1 = new Vector3(25, 0, 0); Vector3 vertex2 = new Vector3(0, 0, -25); Vector3 vertex3 = new Vector3(-25, 0, 0); Vector3 vertex4 = new Vector3(0, 0, 25); Vector3 vertex5 = new Vector3(0, 25, 0); //四棱錐中包含個三角形,因此要構造個點來繪製 verts[0].Position = vertex1; verts[1].Position = vertex2; verts[2].Position = vertex5; verts[3].Position = vertex2; verts[4].Position = vertex3; verts[5].Position = vertex5; verts[6].Position = vertex3; verts[7].Position = vertex4; verts[8].Position = vertex5; verts[9].Position = vertex4; verts[10].Position = vertex1; verts[11].Position = vertex5; verts[12].Position = vertex2; verts[13].Position = vertex1; verts[14].Position = vertex3; verts[15].Position = vertex3; verts[16].Position = vertex1; verts[17].Position = vertex4; //給每一個點賦予隨機顏色 for (int i = 0; i < 18; i++) { verts[i].Color = Color.FromArgb(SetColor(), SetColor(), SetColor()).ToArgb(); } vb.Unlock(); } /// <summary> /// 返回到之間的一個隨機數,用來生成隨機顏色 /// </summary> /// <returns></returns> public int SetColor() { int number = rn.Next(256); return number; } /// <summary> /// 設置攝像機的位置 /// </summary> private void SetupCamera() { //設置世界矩陣,根據系統運行時間而變化 device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f); //設置攝像機的位置,它在z軸上-50處,看着原點,y軸爲正方向 device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -50f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); //設置攝像機的視界,角度爲度,看的最近爲,看的最遠處爲.再也不這個視界中的影像都不會被顯示 device.Transform.Projection = Matrix.PerspectiveFovLH(((float)(float)Math.PI / 2), 1.0f, 10.0f, 200.0f); } /// <summary> /// 繪製圖形 /// </summary> public void Render() { if (device == null) return; if (pause) return; //背景設爲綠色 device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0); //開始場景 device.BeginScene(); // 設置世界,視野和投影矩陣 SetupCamera(); // 給設備指定頂點緩存 device.SetStreamSource(0, vertexBuffer, 0); //設置設備的頂點格式 device.VertexFormat = CustomVertex.PositionColored.Format; //繪製圖形,使用的方法爲三角形列表,個數爲個 device.DrawPrimitives(PrimitiveType.TriangleList, 0, 6); //結束場景 device.EndScene(); //更新場景 device.Present(); } //重載OnPaint函數 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { //繪製圖形 this.Render(); } }
調用代碼:ide
using (TClass frm = new TClass()) { if (!frm.InitializeGraphics()) // 初始化 Direct3D { MessageBox.Show("不能初始化 Direct3D.程序將退出."); return; } frm.Show(); // While the form is still valid, render and process messages while (frm.Created) { frm.Render(); Application.DoEvents(); //處理當前在消息隊列中的全部 Windows 消息 } }
3.須要引用directx的程序集,下載鏈接(含項目):函數
連接:https://pan.baidu.com/s/1D4wrHC7c2Pg1wpWXlrLrSA
提取碼:7f6w this
4.注意調用Microsoft.DirectX.dll時候,須要在程序配置文件中設置useLegacyV2RuntimeActivationPolicy爲true。orm