I have a problem with trying to render a glucylinder between two 3d points.
Based on the code from here
http://home.neo.rr.com/jparris/OpenGL%20-%20draw%20your%20own%20cylinder%20between%202%20pts.htm
This seems to work for all cases except when the cylinder is aligned to the Y axis. ie from (0,-5,0) to (0,+5,0). In that case the Y axis aligned cylinder either flickers (like its rotation is being flipped around each frame) or it renders from the first point out in space the opposite direction to the second point.
Can anyone help with the right “if then” code to fix the Y axis problem? I have done a lot of searching on these forums and the web trying to find working code. It will definitely help me and I am sure others who come here looking for the solution in the future.
Thanks,
Jason.
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;