DrawTaperClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:8k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // 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 DrawTaperClass
- {
- /// <summary>
- /// <param name="device">device:设备对象</param>
- /// </summary>
- public Device device;//定义设备
- /// <summary>
- /// <param name="circleCenterVectors">circleCenterVectors:圆锥底部圆心坐标</param>
- /// </summary>
- public Vector3 circleCenterVectors;//定义圆锥底部圆心坐标
- ///<summary>
- ///<param name="radius">radius:圆锥底部圆半径</param>
- ///</summary>
- public float radius;//定义圆锥底部圆半径
- /// <summary>
- /// <param name="taperTopVectors">taperTopVectors:圆锥顶部点坐标</param>
- /// </summary>
- public Vector3 taperTopVectors;//定义圆锥顶部点坐标
- /// <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="_circleCenterVectors">圆心坐标</param>
- /// <param name="_radius">圆半径</param>
- /// <param name="_taperTopVectors">圆锥顶部点坐标</param>
- /// <param name="_mNumber">沿切向的分块数目</param>
- /// <param name="_nNumber">沿径向的分块数目</param>
- public DrawTaperClass(Device _device, Vector3 _circleCenterVectors, float _radius, Vector3 _taperTopVectors, int _mNumber, int _nNumber)
- {
- device = _device;
- circleCenterVectors = _circleCenterVectors;
- radius = _radius;
- taperTopVectors = _taperTopVectors;
- mNumber = _mNumber;
- nNumber = _nNumber;
- VertexDeclaration();//定义顶点
- IndicesDeclaration();//定义索引
- }
- private void VertexDeclaration()//定义顶点
- {
- Vector3 circleNormalVectors = Vector3.Subtract(taperTopVectors,circleCenterVectors);
- vertices = new CustomVertex.PositionColored[2*(mNumber + 1) * (nNumber + 1)];
- float cosAlpha = circleNormalVectors.Y / circleNormalVectors.Length();
- float sinAlpha = (float)Math.Sqrt(1 - cosAlpha * cosAlpha);
- float sinBeta = circleNormalVectors.Z / (float)Math.Sqrt(circleNormalVectors.X * circleNormalVectors.X + circleNormalVectors.Z * circleNormalVectors.Z);
- float cosBeta = (float)Math.Sqrt(1 - sinBeta * sinBeta);
- Vector3 R0Vector = new Vector3();
- R0Vector.X = radius * cosAlpha * cosBeta;
- R0Vector.Y = -radius * sinAlpha;
- R0Vector.Z = radius * cosAlpha * sinBeta;
- float theta = (float)Math.PI * 2 / mNumber;
- //定义圆锥四周网格节点坐标
- for (int i = 0; i < mNumber + 1; i++)
- {
- Vector4 tempRVector = Vector3.Transform(R0Vector, Matrix.RotationAxis(circleNormalVectors, theta * i));
- for (int j = 0; j < nNumber + 1; j++)
- {
- float scaleFactor = (float)j / nNumber;
- Vector3 tempOOVector = Vector3.Scale(circleNormalVectors,1f - scaleFactor);
- vertices[j + i * (nNumber + 1)].X = circleCenterVectors.X + tempRVector.X * scaleFactor + tempOOVector.X;
- vertices[j + i * (nNumber + 1)].Y = circleCenterVectors.Y + tempRVector.Y * scaleFactor + tempOOVector.Y;
- vertices[j + i * (nNumber + 1)].Z = circleCenterVectors.Z + tempRVector.Z * scaleFactor + tempOOVector.Z;
- vertices[j + i * (nNumber + 1)].Color = Color.Yellow.ToArgb();
- }
- }
- //定义圆锥底部网格节点坐标
- int baseIndex = (mNumber + 1) * (nNumber + 1);
- for (int i = 0; i < mNumber + 1; i++)
- {
- Vector4 tempRVector = Vector3.Transform(R0Vector, Matrix.RotationAxis(circleNormalVectors, theta * i));
- for (int j = 0; j < nNumber + 1; j++)
- {
- float scaleFactor = (float)j / nNumber;
- vertices[j + i * (nNumber + 1) + baseIndex].X = circleCenterVectors.X + tempRVector.X * scaleFactor;
- vertices[j + i * (nNumber + 1) + baseIndex].Y = circleCenterVectors.Y + tempRVector.Y * scaleFactor;
- vertices[j + i * (nNumber + 1) + baseIndex].Z = circleCenterVectors.Z + tempRVector.Z * scaleFactor;
- vertices[j + i * (nNumber + 1) + baseIndex].Color = Color.Yellow.ToArgb();
- }
- }
- }
- private void IndicesDeclaration()//定义索引
- {
- indexData = new int[12 * mNumber * nNumber];
- //定义圆锥周围索引
- for (int i = 0; i < mNumber; i++)
- {
- for (int j = 0; j < nNumber; j++)
- {
- indexData[6 * (j + i * nNumber)] = j + i * (nNumber + 1);
- indexData[6 * (j + i * nNumber) + 1] = j + i * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 2] = j + (i + 1) * (nNumber + 1);
- indexData[6 * (j + i * nNumber) + 3] = j + i * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 4] = j + (i + 1) * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 5] = j + (i + 1) * (nNumber + 1);
- }
- }
- //定义圆锥底部索引
- int baseIndex01 = 6*mNumber* nNumber;
- int baseIndex02 = (mNumber + 1) * (nNumber + 1);
- for (int i = 0; i < mNumber; i++)
- {
- for (int j = 0; j < nNumber; j++)
- {
- indexData[6 * (j + i * nNumber) + baseIndex01] = j + i * (nNumber + 1) + baseIndex02;
- indexData[6 * (j + i * nNumber) + 1 + baseIndex01] = j + (i + 1) * (nNumber + 1) + baseIndex02;
- indexData[6 * (j + i * nNumber) + 2 + baseIndex01] = j + i * (nNumber + 1) + 1 + baseIndex02;
- indexData[6 * (j + i * nNumber) + 3 + baseIndex01] = j + i * (nNumber + 1) + 1 + baseIndex02;
- indexData[6 * (j + i * nNumber) + 4 + baseIndex01] = j + (i + 1) * (nNumber + 1) + baseIndex02;
- indexData[6 * (j + i * nNumber) + 5 + baseIndex01] = j + (i + 1) * (nNumber + 1) + 1 + baseIndex02;
- }
- }
- }
- /// <summary>
- /// 绘制圆锥
- /// </summary>
- public void DrawTaper()
- {
- device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, 2*(mNumber + 1) * (nNumber + 1), 4 * mNumber * nNumber, indexData, false, vertices);
- }
- }
- }