DrawHexClass.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.Collections.Generic;
  11. using System.Text;
  12. using System.Drawing;
  13. using Microsoft.DirectX;
  14. using Microsoft.DirectX.Direct3D;
  15. namespace Mesh网格
  16. {
  17.     public class DrawHexClass
  18.     {
  19.         public Mesh MeshObject;
  20.         public Device device;
  21.         public Vector3[] pointVectors;
  22.         public DrawHexClass(Device device, Vector3[] pointVectors)
  23.         {
  24.             this.device = device;
  25.             this.pointVectors = pointVectors;
  26.             MeshObject = DrawHexObject(device, pointVectors);//定义网格            
  27.         }        
  28.         public static Mesh DrawHexObject(Device device, Vector3[] pointVectors)//定义网格
  29.         {
  30.             Mesh meshObject;
  31.             //定义顶点
  32.             CustomVertex.PositionColored[] arrayVertices= new CustomVertex.PositionColored[8];
  33.             //定义索引
  34.             Int16[] arrayIndices = new Int16[36];
  35.             //定义属性
  36.             AttributeRange attributeRange = new AttributeRange();
  37.             //实例化网格对象
  38.             meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionColored.Format, device);
  39.             //设置顶点坐标值
  40.             arrayVertices[0].Position = pointVectors[0];
  41.             arrayVertices[0].Color = Color.Yellow.ToArgb();
  42.             arrayVertices[1].Position = pointVectors[1];
  43.             arrayVertices[1].Color = Color.Red.ToArgb();
  44.             arrayVertices[2].Position = pointVectors[2];
  45.             arrayVertices[2].Color = Color.Green.ToArgb();
  46.             arrayVertices[3].Position = pointVectors[3];
  47.             arrayVertices[3].Color = Color.Gold.ToArgb();
  48.             arrayVertices[4].Position = pointVectors[4];
  49.             arrayVertices[4].Color = Color.GhostWhite.ToArgb();
  50.             arrayVertices[5].Position = pointVectors[5];
  51.             arrayVertices[5].Color = Color.LightPink.ToArgb();
  52.             arrayVertices[6].Position = pointVectors[6];
  53.             arrayVertices[6].Color = Color.Maroon.ToArgb();
  54.             arrayVertices[7].Position = pointVectors[7];
  55.             //设置索引,顶部三角形
  56.             arrayIndices[0] = 0; arrayIndices[1] = 1; arrayIndices[2] = 3;
  57.             arrayIndices[3] = 3; arrayIndices[4] = 1; arrayIndices[5] = 2;
  58.             //周围三角形
  59.             arrayIndices[6] = 4; arrayIndices[7] = 0; arrayIndices[8] = 7;
  60.             arrayIndices[9] = 7; arrayIndices[10] = 0; arrayIndices[11] = 3;
  61.             arrayIndices[12] = 7; arrayIndices[13] = 3; arrayIndices[14] = 6;
  62.             arrayIndices[15] = 6; arrayIndices[16] = 3; arrayIndices[17] = 2;
  63.             arrayIndices[18] = 6; arrayIndices[19] = 2; arrayIndices[20] = 5;
  64.             arrayIndices[21] = 5; arrayIndices[22] = 2; arrayIndices[23] = 1;
  65.             arrayIndices[24] = 5; arrayIndices[25] = 1; arrayIndices[26] = 4;
  66.             arrayIndices[27] = 4; arrayIndices[28] = 1; arrayIndices[29] = 0;
  67.             //底部三角形
  68.             arrayIndices[30] = 5; arrayIndices[31] = 4; arrayIndices[32] = 7;
  69.             arrayIndices[33] = 7; arrayIndices[34] = 6; arrayIndices[35] = 5;
  70.             //设置属性
  71.             attributeRange.AttributeId = 0;
  72.             attributeRange.FaceStart = 0;
  73.             attributeRange.FaceCount = arrayIndices.Length / 3;
  74.             attributeRange.VertexStart = 0;
  75.             attributeRange.VertexCount = arrayVertices.Length;
  76.             //设置网格对象的索引缓冲和顶点缓冲数据
  77.             meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
  78.             meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
  79.             meshObject.SetAttributeTable(new AttributeRange[] { attributeRange });
  80.             return meshObject;
  81.         }
  82.     }
  83. }