public void AddSphere(Point3D center, double radius, int thetaDiv = 20, int phiDiv = 10)
{
int index0 = this.Positions.Count;
double dt = 2 * Math.PI / thetaDiv;
double dp = Math.PI / phiDiv;
for (int pi = 0; pi <= phiDiv; pi++)
{
double phi = pi * dp;
for (int ti = 0; ti <= thetaDiv; ti++)
{
// we want to start the mesh on the x axis
double theta = ti * dt;
// Spherical coordinates
// http://mathworld.wolfram.com/SphericalCoordinates.html
double x = Math.Cos(theta) * Math.Sin(phi);
double y = Math.Sin(theta) * Math.Sin(phi);
double z = Math.Cos(phi);
var p = new Point3D(center.X + (radius * x), center.Y + (radius * y), center.Z + (radius * z));
this.positions.Add(p);
if (this.normals != null)
{
var n = new Vector3D(x, y, z);
this.normals.Add(n);
}
if (this.textureCoordinates != null)
{
var uv = new Point(theta / (2 * Math.PI), phi / Math.PI);
this.textureCoordinates.Add(uv);
}
}
}
this.AddRectangularMeshTriangleIndices(index0, phiDiv + 1, thetaDiv + 1, true);
}