572 {
573 if (radius < 0.0f)
574 {
575 radius = -radius;
576 }
577
578 if (radius == 0.0f)
579 {
580 throw new DivideByZeroException("DrawSphere: Radius cannot be zero.");
581 }
582
583 if (precision == 0)
584 {
585 throw new DivideByZeroException("DrawSphere: Precision of 8 or greater is required.");
586 }
587
588 float halfPI = (float) (Math.PI * 0.5);
589 float oneThroughPrecision = 1.0f / precision;
590 float twoPIThroughPrecision = (float) (Math.PI * 2.0 * oneThroughPrecision);
591
592 float theta1, theta2, theta3;
594
595 for (uint j = 0; j < precision / 2; j++)
596 {
597 theta1 = j * twoPIThroughPrecision - halfPI;
598 theta2 = (j + 1) * twoPIThroughPrecision - halfPI;
599
600 GL.Begin(BeginMode.TriangleStrip);
601 for (uint i = 0; i <= precision; i++)
602 {
603 theta3 = i * twoPIThroughPrecision;
604
605 norm._x = (float) (Math.Cos(theta2) * Math.Cos(theta3));
606 norm._y = (float) Math.Sin(theta2);
607 norm._z = (float) (Math.Cos(theta2) * Math.Sin(theta3));
608 pos._x = center._x + radius * norm.
_x;
609 pos._y = center._y + radius * norm.
_y;
610 pos._z = center._z + radius * norm.
_z;
611
612 GL.Normal3(norm.
_x, norm.
_y, norm.
_z);
613 GL.TexCoord2(i * oneThroughPrecision, 2.0f * (j + 1) * oneThroughPrecision);
614 GL.Vertex3(pos.
_x, pos.
_y, pos.
_z);
615
616 norm._x = (float) (Math.Cos(theta1) * Math.Cos(theta3));
617 norm._y = (float) Math.Sin(theta1);
618 norm._z = (float) (Math.Cos(theta1) * Math.Sin(theta3));
619 pos._x = center._x + radius * norm.
_x;
620 pos._y = center._y + radius * norm.
_y;
621 pos._z = center._z + radius * norm.
_z;
622
623 GL.Normal3(norm.
_x, norm.
_y, norm.
_z);
624 GL.TexCoord2(i * oneThroughPrecision, 2.0f * j * oneThroughPrecision);
625 GL.Vertex3(pos.
_x, pos.
_y, pos.
_z);
626 }
627
628 GL.End();
629 }
630 }