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

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 DrawSphereClass
  18.     {
  19.         /// <summary>
  20.         /// <param name="device">device:设备对象</param>
  21.         /// </summary>
  22.         public Device device;//定义设备
  23.         /// <summary>
  24.         /// <param name="shpereCenterVectors">shpereCenterVectors:球心坐标</param>
  25.         /// </summary>
  26.         public Vector3 shpereCenterVectors;//定义球心坐标
  27.         ///<summary>
  28.         ///<param name="radius">radius:球体半径</param>
  29.         ///</summary>
  30.         public float radius;//定义球体半径
  31.         /// <summary>
  32.         /// <param name="mNumber">mNumber:球体在水平方向的分块数目</param>
  33.         /// </summary>
  34.         public int mNumber;//定义球体在水平方向的分块数目
  35.         /// <summary>
  36.         /// <param name="nNumber">nNumber:球体在竖直方向的分块数目</param>
  37.         /// </summary>
  38.         public int nNumber;//定义球体在竖直方向的分块数目
  39.         private CustomVertex.PositionColored[] vertices;//定义球体网格顶点
  40.         private int[] indexData;//定义球体网格中三角形索引
  41.         /// <summary>
  42.         /// 定义一个绘制球体对象
  43.         /// </summary>
  44.         /// <param name="_device">设备对象</param>
  45.         /// <param name="_shpereCenterVectors">球心坐标</param>
  46.         /// <param name="_radius">球体半径</param>
  47.         /// <param name="_mNumber">球体在水平方向的分块数目</param>
  48.         /// <param name="_nNumber">球体在竖直方向的分块数目</param>
  49.         public DrawSphereClass(Device _device,Vector3 _shpereCenterVectors,float _radius,int _mNumber,int _nNumber)
  50.         {
  51.             device = _device;
  52.             shpereCenterVectors = _shpereCenterVectors;
  53.             radius = _radius;
  54.             mNumber = _mNumber;
  55.             nNumber = _nNumber;
  56.             VertexDeclaration();//定义顶点
  57.             IndicesDeclaration();//定义索引
  58.         }
  59.         private void VertexDeclaration()//定义顶点
  60.         {
  61.             vertices=new CustomVertex.PositionColored[(mNumber+1)*(nNumber+1)];
  62.             float theta = (float)Math.PI / nNumber;
  63.             float alpha = 2 * (float)Math.PI / mNumber;
  64.             for (int i = 0; i < nNumber+1; i++)
  65.             {
  66.                 for (int j = 0; j < mNumber+1; j++)
  67.                 {
  68.                     vertices[j + i * (mNumber + 1)].X = shpereCenterVectors.X + radius * (float)Math.Sin(i*theta) * (float)Math.Cos(j*alpha);
  69.                     vertices[j + i * (mNumber + 1)].Y = shpereCenterVectors.Y + radius * (float)Math.Cos(i * theta);
  70.                     vertices[j + i * (mNumber + 1)].Z = shpereCenterVectors.Z + radius * (float)Math.Sin(i * theta) * (float)Math.Sin(j*alpha);
  71.                     vertices[j + i * (mNumber + 1)].Color = Color.Yellow.ToArgb();
  72.                 }
  73.             }
  74.         }
  75.         private void IndicesDeclaration()//定义索引
  76.         {
  77.             indexData=new int[6*mNumber*nNumber];
  78.             for (int i = 0; i < nNumber; i++)
  79.             {
  80.                 for (int j = 0; j < mNumber; j++)
  81.                 {
  82.                     indexData[6 * (j + i * mNumber)] = j + i * (mNumber + 1);
  83.                     indexData[6 * (j + i * mNumber) + 1] = j + i * (mNumber + 1)+1;
  84.                     indexData[6 * (j + i * mNumber) + 2] = j + (i + 1) * (mNumber + 1);
  85.                     indexData[6 * (j + i * mNumber) + 3] = j + i * (mNumber + 1) + 1;
  86.                     indexData[6 * (j + i * mNumber) + 4] = j + (i + 1) * (mNumber + 1)+1;
  87.                     indexData[6 * (j + i * mNumber) + 5] = j + (i + 1) * (mNumber + 1);
  88.                 }
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// 绘制球体网格
  93.         /// </summary>
  94.         public void DrawSphere()
  95.         {
  96.             device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, (mNumber+1) * (nNumber+1), 2 * mNumber * nNumber, indexData, false, vertices);
  97.         }
  98.     }
  99. }