procedure CylinderGL(p1,p2:point3d;r1,r2:double;precision:integer);
var quadratic:pGLUquadricObj;
height,dx,dy,dz:double;
v,vx,vy,vz,rx,ry,rz,zero,ax,r2d:double;
begin
r2d:=180/pi;
//length of cylinder
dx:=p1.x-p2.x;
dy:=p1.y-p2.y;
dz:=p1.z-p2.z;
height:=sqrt(dx*dx+dy*dy+dz*dz);
glpushmatrix;
gltranslatef(p1.x,p1.y,p1.z);
//now rotate the matrix into position
// orientation vectors
vx:=p2.x-p1.x; // component in x-direction
vy:=p2.y-p1.y; // component in y-direction
vz:=p2.z-p1.z; // component in z-direction
v:=sqrt(vx*vx+vy*vy+vz*vz); // cylinder length
// rotation vector, z x r
rx:=-vy*vz;
ry:=+vx*vz;
ax:=0.0;
if vz=0 then
begin
ax:=r2d*arccos(vx/v); // rotation angle in x-y plane
if vx<=0 then ax:=-ax;
end
else
begin
ax:=r2d*arccos(vz/v); // rotation angle
if vz<=0 then ax:=-ax;
end;
if vz=0 then
begin
glRotated(90.0, 0, 1, 0.0); // Rotate & align with x axis
glRotated(ax, -1.0, 0.0, 0.0); // Rotate to point 2 in x-y plane
end
else
begin
glRotated(ax, rx, ry, 0.0); // Rotate about rotation vector
end;
quadratic:=gluNewQuadric; // Create A Pointer To The Quadric Object ( NEW )
gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals ( NEW )
gluQuadricTexture(quadratic, FALSE); // Create Texture Coords ( NEW )
gluCylinder(quadratic,r1,r2,height,precision,precision); // Draw A cylinder
glpopmatrix;
end;