DrawSphereClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:5k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // 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 绘制基本几何体
- {
- class DrawSphereClass
- {
- /// <summary>
- /// <param name="device">device:设备对象</param>
- /// </summary>
- public Device device;//定义设备
- /// <summary>
- /// <param name="shpereCenterVectors">shpereCenterVectors:球心坐标</param>
- /// </summary>
- public Vector3 shpereCenterVectors;//定义球心坐标
- ///<summary>
- ///<param name="radius">radius:球体半径</param>
- ///</summary>
- public float radius;//定义球体半径
- /// <summary>
- /// <param name="mNumber">mNumber:球体在水平方向的分块数目</param>
- /// </summary>
- public int mNumber;//定义球体在水平方向的分块数目
- /// <summary>
- /// <param name="nNumber">nNumber:球体在竖直方向的分块数目</param>
- /// </summary>
- public int nNumber;//定义球体在竖直方向的分块数目
- private CustomVertex.PositionColored[] vertices;//定义球体网格顶点
- private int[] indexData;//定义球体网格中三角形索引
- /// <summary>
- /// 定义一个绘制球体对象
- /// </summary>
- /// <param name="_device">设备对象</param>
- /// <param name="_shpereCenterVectors">球心坐标</param>
- /// <param name="_radius">球体半径</param>
- /// <param name="_mNumber">球体在水平方向的分块数目</param>
- /// <param name="_nNumber">球体在竖直方向的分块数目</param>
- public DrawSphereClass(Device _device,Vector3 _shpereCenterVectors,float _radius,int _mNumber,int _nNumber)
- {
- device = _device;
- shpereCenterVectors = _shpereCenterVectors;
- radius = _radius;
- mNumber = _mNumber;
- nNumber = _nNumber;
- VertexDeclaration();//定义顶点
- IndicesDeclaration();//定义索引
- }
- private void VertexDeclaration()//定义顶点
- {
- vertices=new CustomVertex.PositionColored[(mNumber+1)*(nNumber+1)];
- float theta = (float)Math.PI / nNumber;
- float alpha = 2 * (float)Math.PI / mNumber;
- for (int i = 0; i < nNumber+1; i++)
- {
- for (int j = 0; j < mNumber+1; j++)
- {
- vertices[j + i * (mNumber + 1)].X = shpereCenterVectors.X + radius * (float)Math.Sin(i*theta) * (float)Math.Cos(j*alpha);
- vertices[j + i * (mNumber + 1)].Y = shpereCenterVectors.Y + radius * (float)Math.Cos(i * theta);
- vertices[j + i * (mNumber + 1)].Z = shpereCenterVectors.Z + radius * (float)Math.Sin(i * theta) * (float)Math.Sin(j*alpha);
- vertices[j + i * (mNumber + 1)].Color = Color.Yellow.ToArgb();
- }
- }
- }
- private void IndicesDeclaration()//定义索引
- {
- indexData=new int[6*mNumber*nNumber];
- for (int i = 0; i < nNumber; i++)
- {
- for (int j = 0; j < mNumber; j++)
- {
- indexData[6 * (j + i * mNumber)] = j + i * (mNumber + 1);
- indexData[6 * (j + i * mNumber) + 1] = j + i * (mNumber + 1)+1;
- indexData[6 * (j + i * mNumber) + 2] = j + (i + 1) * (mNumber + 1);
- indexData[6 * (j + i * mNumber) + 3] = j + i * (mNumber + 1) + 1;
- indexData[6 * (j + i * mNumber) + 4] = j + (i + 1) * (mNumber + 1)+1;
- indexData[6 * (j + i * mNumber) + 5] = j + (i + 1) * (mNumber + 1);
- }
- }
- }
- /// <summary>
- /// 绘制球体网格
- /// </summary>
- public void DrawSphere()
- {
- device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, (mNumber+1) * (nNumber+1), 2 * mNumber * nNumber, indexData, false, vertices);
- }
- }
- }