使用OpenGL绘制圆环体(Torus)
本篇介绍一下使用OpenGL绘制圆环体的方法。程序是在C#和OpenTK环境下编译的。
代码:
/// <summary> /// 绘制圆环体 /// </summary> /// <param name="Radius">圆环体半径</param> /// <param name="TubeRadius">圆环体段半径</param> /// <param name="Sides">圆环体段的侧面数</param> /// <param name="Rings">圆环体段数</param> void DrawTorus(double Radius = 50, double TubeRadius= 10, int Sides = 20, int Rings = 30) { double sideDelta = 2.0 * Math.PI / Sides; double ringDelta = 2.0 * Math.PI / Rings; double theta = 0; double cosTheta = 1.0; double sinTheta = 0.0; double phi, sinPhi, cosPhi; double dist; GL.Color3(1.00f, 0.0f, 0.0f); for (int i = 0; i < Rings; i++) { double theta1 = theta + ringDelta; double cosTheta1 = Math.Cos(theta1); double sinTheta1 = Math.Sin(theta1); GL.Begin(BeginMode.QuadStrip); phi = 0; for (int j = 0; j <= Sides; j++) { phi = phi + sideDelta; cosPhi = Math.Cos(phi); sinPhi = Math.Sin(phi); dist = Radius + (TubeRadius * cosPhi); GL.Normal3(cosTheta * cosPhi, sinTheta * cosPhi, sinPhi); GL.Vertex3(cosTheta * dist, sinTheta * dist, TubeRadius * sinPhi); GL.Normal3(cosTheta1 * cosPhi, sinTheta1 * cosPhi, sinPhi); GL.Vertex3(cosTheta1 * dist, sinTheta1 * dist, TubeRadius * sinPhi); } GL.End(); theta = theta1; cosTheta = cosTheta1; sinTheta = sinTheta1; } }