DrawHexClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:4k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace Mesh网格
- {
- public class DrawHexClass
- {
- public Mesh MeshObject;
- public Device device;
- public Vector3[] pointVectors;
- public DrawHexClass(Device device, Vector3[] pointVectors)
- {
- this.device = device;
- this.pointVectors = pointVectors;
- MeshObject = DrawHexObject(device, pointVectors);//定义网格
- }
- public static Mesh DrawHexObject(Device device, Vector3[] pointVectors)//定义网格
- {
- Mesh meshObject;
- //定义顶点
- CustomVertex.PositionColored[] arrayVertices= new CustomVertex.PositionColored[8];
- //定义索引
- Int16[] arrayIndices = new Int16[36];
- //定义属性
- AttributeRange attributeRange = new AttributeRange();
- //实例化网格对象
- meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionColored.Format, device);
- //设置顶点坐标值
- arrayVertices[0].Position = pointVectors[0];
- arrayVertices[0].Color = Color.Yellow.ToArgb();
- arrayVertices[1].Position = pointVectors[1];
- arrayVertices[1].Color = Color.Red.ToArgb();
- arrayVertices[2].Position = pointVectors[2];
- arrayVertices[2].Color = Color.Green.ToArgb();
- arrayVertices[3].Position = pointVectors[3];
- arrayVertices[3].Color = Color.Gold.ToArgb();
- arrayVertices[4].Position = pointVectors[4];
- arrayVertices[4].Color = Color.GhostWhite.ToArgb();
- arrayVertices[5].Position = pointVectors[5];
- arrayVertices[5].Color = Color.LightPink.ToArgb();
- arrayVertices[6].Position = pointVectors[6];
- arrayVertices[6].Color = Color.Maroon.ToArgb();
- arrayVertices[7].Position = pointVectors[7];
- //设置索引,顶部三角形
- arrayIndices[0] = 0; arrayIndices[1] = 1; arrayIndices[2] = 3;
- arrayIndices[3] = 3; arrayIndices[4] = 1; arrayIndices[5] = 2;
- //周围三角形
- arrayIndices[6] = 4; arrayIndices[7] = 0; arrayIndices[8] = 7;
- arrayIndices[9] = 7; arrayIndices[10] = 0; arrayIndices[11] = 3;
- arrayIndices[12] = 7; arrayIndices[13] = 3; arrayIndices[14] = 6;
- arrayIndices[15] = 6; arrayIndices[16] = 3; arrayIndices[17] = 2;
- arrayIndices[18] = 6; arrayIndices[19] = 2; arrayIndices[20] = 5;
- arrayIndices[21] = 5; arrayIndices[22] = 2; arrayIndices[23] = 1;
- arrayIndices[24] = 5; arrayIndices[25] = 1; arrayIndices[26] = 4;
- arrayIndices[27] = 4; arrayIndices[28] = 1; arrayIndices[29] = 0;
- //底部三角形
- arrayIndices[30] = 5; arrayIndices[31] = 4; arrayIndices[32] = 7;
- arrayIndices[33] = 7; arrayIndices[34] = 6; arrayIndices[35] = 5;
- //设置属性
- attributeRange.AttributeId = 0;
- attributeRange.FaceStart = 0;
- attributeRange.FaceCount = arrayIndices.Length / 3;
- attributeRange.VertexStart = 0;
- attributeRange.VertexCount = arrayVertices.Length;
- //设置网格对象的索引缓冲和顶点缓冲数据
- meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
- meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
- meshObject.SetAttributeTable(new AttributeRange[] { attributeRange });
- return meshObject;
- }
- }
- }