unit Figures;

interface
uses dglopengl;
function DrawSphere(CX, CY, CZ, Radius : TGLFloat; N : Integer) : Cardinal;

implementation


 function DrawSphere(CX, CY, CZ, Radius : TGLFloat; N : Integer) : Cardinal;
var
 i,j                  : Integer;
 theta1,theta2,theta3 : TGLFloat;
 X, Y, Z, px, py, pz  : TGLFloat;
begin
if Radius < 0 then
 Radius :=-Radius;
if n < 0 then
 n := -n;
if (n < 4) or (Radius <= 0) then
 begin
 glBegin(GL_POINTS);
  glVertex3f(CX, CY, CZ);
 glEnd;
 exit;
 end;
for j := 0 to n div 2 -1 do
 begin
 theta1 := J*2*PI/N - PI/2;
 theta2 := (J+1)*2*PI/n - PI/2;
 glBegin(GL_QUAD_STRIP);
 for i := 0 to n do
  begin
  theta3 := i*2*PI/N;
  x := cos(theta2) * cos(theta3);
  y := sin(theta2);
  z := cos(theta2) * sin(theta3);
  px := CX + Radius*x;
  py := CY + Radius*y;
  pz := CZ + Radius*z;
  glNormal3f(X,Y,Z);
  glTexCoord2f(1-I/n, 2*(J+1)/n);
  glVertex3f(px,py,pz);
  X := cos(theta1) * cos(theta3);
  Y := sin(theta1);
  Z := cos(theta1) * sin(theta3);
  px := CX + Radius*X;
  py := CY + Radius*Y;
  pz := CZ + Radius*Z;
  glNormal3f(X,Y,Z);
  glTexCoord2f(1-i/n, 2*j/n);
  glVertex3f(px,py,pz);
  end;
 glEnd;
 end;
end;

end.
