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

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 绘制基本几何体
  16. {
  17.     class DrawTaperClass
  18.     {
  19.          /// <summary>
  20.         /// <param name="device">device:设备对象</param>
  21.         /// </summary>
  22.         public Device device;//定义设备
  23.         /// <summary>
  24.         /// <param name="circleCenterVectors">circleCenterVectors:圆锥底部圆心坐标</param>
  25.         /// </summary>
  26.         public Vector3 circleCenterVectors;//定义圆锥底部圆心坐标
  27.         ///<summary>
  28.         ///<param name="radius">radius:圆锥底部圆半径</param>
  29.         ///</summary>        
  30.         public float radius;//定义圆锥底部圆半径
  31.         /// <summary>
  32.         /// <param name="taperTopVectors">taperTopVectors:圆锥顶部点坐标</param>
  33.         /// </summary>
  34.         public Vector3 taperTopVectors;//定义圆锥顶部点坐标
  35.         /// <summary>
  36.         /// <param name="mNumber">mNumber:沿切向的分块数目</param>
  37.         /// </summary>
  38.         public int mNumber;//定义沿切向的分块数目
  39.         /// <summary>
  40.         /// <param name="nNumber">nNumber:沿径向的分块数目</param>
  41.         /// </summary>
  42.         public int nNumber;//定义沿径向的分块数目
  43.         private CustomVertex.PositionColored[] vertices;//定义圆锥网格顶点
  44.         private int[] indexData;//定义圆锥网格中三角形索引
  45.         /// <summary>
  46.         /// 定义一个绘制圆锥对象
  47.         /// </summary>
  48.         /// <param name="_device">设备对象</param>
  49.         /// <param name="_circleCenterVectors">圆心坐标</param>
  50.         /// <param name="_radius">圆半径</param>
  51.         /// <param name="_taperTopVectors">圆锥顶部点坐标</param>
  52.         /// <param name="_mNumber">沿切向的分块数目</param>
  53.         /// <param name="_nNumber">沿径向的分块数目</param>
  54.         public DrawTaperClass(Device _device, Vector3 _circleCenterVectors, float _radius, Vector3 _taperTopVectors, int _mNumber, int _nNumber)
  55.         {
  56.             device = _device;
  57.             circleCenterVectors = _circleCenterVectors;
  58.             radius = _radius;
  59.             taperTopVectors = _taperTopVectors;
  60.             mNumber = _mNumber;
  61.             nNumber = _nNumber;
  62.             VertexDeclaration();//定义顶点
  63.             IndicesDeclaration();//定义索引
  64.         }
  65.         private void VertexDeclaration()//定义顶点
  66.         {
  67.             Vector3 circleNormalVectors = Vector3.Subtract(taperTopVectors,circleCenterVectors);
  68.             vertices = new CustomVertex.PositionColored[2*(mNumber + 1) * (nNumber + 1)];
  69.             float cosAlpha = circleNormalVectors.Y / circleNormalVectors.Length();
  70.             float sinAlpha = (float)Math.Sqrt(1 - cosAlpha * cosAlpha);
  71.             float sinBeta = circleNormalVectors.Z / (float)Math.Sqrt(circleNormalVectors.X * circleNormalVectors.X + circleNormalVectors.Z * circleNormalVectors.Z);
  72.             float cosBeta = (float)Math.Sqrt(1 - sinBeta * sinBeta);
  73.             Vector3 R0Vector = new Vector3();
  74.             R0Vector.X = radius * cosAlpha * cosBeta;
  75.             R0Vector.Y = -radius * sinAlpha;
  76.             R0Vector.Z = radius * cosAlpha * sinBeta;
  77.             float theta = (float)Math.PI * 2 / mNumber;
  78.             //定义圆锥四周网格节点坐标      
  79.             for (int i = 0; i < mNumber + 1; i++)
  80.             {
  81.                 Vector4 tempRVector = Vector3.Transform(R0Vector, Matrix.RotationAxis(circleNormalVectors, theta * i));
  82.                 for (int j = 0; j < nNumber + 1; j++)
  83.                 {
  84.                     float scaleFactor = (float)j / nNumber;
  85.                     Vector3 tempOOVector = Vector3.Scale(circleNormalVectors,1f - scaleFactor);
  86.                     vertices[j + i * (nNumber + 1)].X = circleCenterVectors.X + tempRVector.X * scaleFactor + tempOOVector.X;
  87.                     vertices[j + i * (nNumber + 1)].Y = circleCenterVectors.Y + tempRVector.Y * scaleFactor + tempOOVector.Y;
  88.                     vertices[j + i * (nNumber + 1)].Z = circleCenterVectors.Z + tempRVector.Z * scaleFactor + tempOOVector.Z;
  89.                     vertices[j + i * (nNumber + 1)].Color = Color.Yellow.ToArgb();
  90.                 }
  91.             }
  92.             //定义圆锥底部网格节点坐标
  93.             int baseIndex = (mNumber + 1) * (nNumber + 1);
  94.             for (int i = 0; i < mNumber + 1; i++)
  95.             {
  96.                 Vector4 tempRVector = Vector3.Transform(R0Vector, Matrix.RotationAxis(circleNormalVectors, theta * i));
  97.                 for (int j = 0; j < nNumber + 1; j++)
  98.                 {
  99.                     float scaleFactor = (float)j / nNumber;
  100.                     vertices[j + i * (nNumber + 1) + baseIndex].X = circleCenterVectors.X + tempRVector.X * scaleFactor;
  101.                     vertices[j + i * (nNumber + 1) + baseIndex].Y = circleCenterVectors.Y + tempRVector.Y * scaleFactor;
  102.                     vertices[j + i * (nNumber + 1) + baseIndex].Z = circleCenterVectors.Z + tempRVector.Z * scaleFactor;
  103.                     vertices[j + i * (nNumber + 1) + baseIndex].Color = Color.Yellow.ToArgb();
  104.                 }
  105.             } 
  106.         }
  107.         private void IndicesDeclaration()//定义索引
  108.         {
  109.             indexData = new int[12 * mNumber * nNumber];
  110.             //定义圆锥周围索引
  111.             for (int i = 0; i < mNumber; i++)
  112.             {
  113.                 for (int j = 0; j < nNumber; j++)
  114.                 {
  115.                     indexData[6 * (j + i * nNumber)] = j + i * (nNumber + 1);
  116.                     indexData[6 * (j + i * nNumber) + 1] = j + i * (nNumber + 1) + 1;
  117.                     indexData[6 * (j + i * nNumber) + 2] = j + (i + 1) * (nNumber + 1);
  118.                     indexData[6 * (j + i * nNumber) + 3] = j + i * (nNumber + 1) + 1;
  119.                     indexData[6 * (j + i * nNumber) + 4] = j + (i + 1) * (nNumber + 1) + 1;
  120.                     indexData[6 * (j + i * nNumber) + 5] = j + (i + 1) * (nNumber + 1);
  121.                 }
  122.             }
  123.             //定义圆锥底部索引
  124.             int baseIndex01 = 6*mNumber* nNumber;
  125.             int baseIndex02 = (mNumber + 1) * (nNumber + 1);
  126.             for (int i = 0; i < mNumber; i++)
  127.             {
  128.                 for (int j = 0; j < nNumber; j++)
  129.                 {
  130.                     indexData[6 * (j + i * nNumber) + baseIndex01] = j + i * (nNumber + 1) + baseIndex02;
  131.                     indexData[6 * (j + i * nNumber) + 1 + baseIndex01] = j + (i + 1) * (nNumber + 1) + baseIndex02;
  132.                     indexData[6 * (j + i * nNumber) + 2 + baseIndex01] = j + i * (nNumber + 1) + 1 + baseIndex02;
  133.                     indexData[6 * (j + i * nNumber) + 3 + baseIndex01] = j + i * (nNumber + 1) + 1 + baseIndex02;
  134.                     indexData[6 * (j + i * nNumber) + 4 + baseIndex01] = j + (i + 1) * (nNumber + 1) + baseIndex02;
  135.                     indexData[6 * (j + i * nNumber) + 5 + baseIndex01] = j + (i + 1) * (nNumber + 1) + 1 + baseIndex02;
  136.                 }
  137.             }
  138.         }
  139.         /// <summary>
  140.         /// 绘制圆锥
  141.         /// </summary>
  142.         public void DrawTaper()
  143.         {
  144.             device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, 2*(mNumber + 1) * (nNumber + 1), 4 * mNumber * nNumber, indexData, false, vertices);
  145.         }
  146.     }
  147. }