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

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 Microsoft.DirectX;
  13. using Microsoft.DirectX.Direct3D;
  14. namespace 贴图
  15. {
  16.     class DrawTextureHexClass
  17.     {
  18.         public static Mesh DrawTextureHexObject(Device device, Vector3[] pointVectors, string texturePath, out Texture meshtexture)//定义网格
  19.         {
  20.             //定义模型贴图并导入贴图
  21.             Texture meshTexture = TextureLoader.FromFile(device, texturePath);
  22.             Mesh meshObject;
  23.             //定义顶点
  24.             CustomVertex.PositionTextured[] arrayVertices = new CustomVertex.PositionTextured[36];
  25.             //定义索引
  26.             Int16[] arrayIndices = new Int16[36];
  27.             //定义属性
  28.             AttributeRange attributeRange = new AttributeRange();
  29.             //实例化网格对象
  30.             meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionTextured.Format, device);
  31.             //设置顶点坐标值及贴图坐标,顶部三角形
  32.             arrayVertices[0].Position = pointVectors[0];
  33.             arrayVertices[0].Tu = 0; arrayVertices[0].Tv = 1;
  34.             arrayVertices[1].Position = pointVectors[1];
  35.             arrayVertices[1].Tu = 0; arrayVertices[1].Tv = 0;
  36.             arrayVertices[2].Position = pointVectors[3];
  37.             arrayVertices[2].Tu = 1; arrayVertices[2].Tv = 1;
  38.             arrayVertices[3] = arrayVertices[1];
  39.             arrayVertices[4].Position = pointVectors[2];
  40.             arrayVertices[4].Tu = 1; arrayVertices[4].Tv = 0;
  41.             arrayVertices[5] = arrayVertices[2];
  42.             //周围三角形
  43.             arrayVertices[6].Position = pointVectors[0];
  44.             arrayVertices[6].Tu = 0; arrayVertices[6].Tv = 0;
  45.             arrayVertices[7].Position = pointVectors[7];
  46.             arrayVertices[7].Tu = 1; arrayVertices[7].Tv = 1;
  47.             arrayVertices[8].Position = pointVectors[4];
  48.             arrayVertices[8].Tu = 0; arrayVertices[8].Tv = 1;
  49.             arrayVertices[9] = arrayVertices[6];
  50.             arrayVertices[10].Position = pointVectors[3];
  51.             arrayVertices[10].Tu = 1; arrayVertices[10].Tv = 0;
  52.             arrayVertices[11] = arrayVertices[7];
  53.             arrayVertices[12].Position = pointVectors[3];
  54.             arrayVertices[12].Tu = 0; arrayVertices[12].Tv = 0;
  55.             arrayVertices[13].Position = pointVectors[6];
  56.             arrayVertices[13].Tu = 1; arrayVertices[13].Tv = 1;
  57.             arrayVertices[14].Position = pointVectors[7];
  58.             arrayVertices[14].Tu = 0; arrayVertices[14].Tv = 1;
  59.             arrayVertices[15] = arrayVertices[12];
  60.             arrayVertices[16].Position = pointVectors[2];
  61.             arrayVertices[16].Tu = 1; arrayVertices[16].Tv = 0;
  62.             arrayVertices[17] = arrayVertices[13];
  63.             arrayVertices[18].Position = pointVectors[2];
  64.             arrayVertices[18].Tu = 0; arrayVertices[18].Tv = 0;
  65.             arrayVertices[19].Position = pointVectors[5];
  66.             arrayVertices[19].Tu = 1; arrayVertices[19].Tv = 1;
  67.             arrayVertices[20].Position = pointVectors[6];
  68.             arrayVertices[20].Tu = 0; arrayVertices[20].Tv = 1;
  69.             arrayVertices[21] = arrayVertices[18];
  70.             arrayVertices[22].Position = pointVectors[1];
  71.             arrayVertices[22].Tu = 1; arrayVertices[22].Tv = 0;
  72.             arrayVertices[23] = arrayVertices[19];
  73.             arrayVertices[24].Position = pointVectors[1];
  74.             arrayVertices[24].Tu = 0; arrayVertices[24].Tv = 0;
  75.             arrayVertices[25].Position = pointVectors[4];
  76.             arrayVertices[25].Tu = 1; arrayVertices[25].Tv = 1;
  77.             arrayVertices[26].Position = pointVectors[5];
  78.             arrayVertices[26].Tu = 0; arrayVertices[26].Tv = 1;
  79.             arrayVertices[27] = arrayVertices[24];
  80.             arrayVertices[28].Position = pointVectors[0];
  81.             arrayVertices[28].Tu = 1; arrayVertices[28].Tv = 0;
  82.             arrayVertices[29] = arrayVertices[25];
  83.             //底部三角形
  84.             arrayVertices[30].Position = pointVectors[4];
  85.             arrayVertices[30].Tu = 0; arrayVertices[30].Tv = 0;
  86.             arrayVertices[31].Position = pointVectors[6];
  87.             arrayVertices[31].Tu = 1; arrayVertices[31].Tv = 1;
  88.             arrayVertices[32].Position = pointVectors[5];
  89.             arrayVertices[32].Tu = 0; arrayVertices[32].Tv = 1;
  90.             arrayVertices[33] = arrayVertices[30];
  91.             arrayVertices[34].Position = pointVectors[7];
  92.             arrayVertices[34].Tu = 1; arrayVertices[34].Tv = 0;
  93.             arrayVertices[35] = arrayVertices[31];
  94.             //设置索引,顶部三角形
  95.             for (Int16 i = 0; i < arrayIndices.Length; i++)
  96.             {
  97.                 arrayIndices[i] = i;
  98.             }
  99.             //设置属性
  100.             attributeRange.AttributeId = 0;
  101.             attributeRange.FaceStart = 0;
  102.             attributeRange.FaceCount = arrayIndices.Length / 3;
  103.             attributeRange.VertexStart = 0;
  104.             attributeRange.VertexCount = arrayVertices.Length;
  105.             //设置网格对象的索引缓冲和顶点缓冲数据
  106.             meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
  107.             meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
  108.             meshObject.SetAttributeTable(new AttributeRange[] { attributeRange });
  109.             meshtexture = meshTexture;
  110.             return meshObject;
  111.         }
  112.     }
  113. }