一同学需要绘制cylinder来代替由折线模拟曲线,之前简单的用过glucylidner函数,现在是需要 已知空间中两个三维点(x1,y1,z1, x2,y2,z2)已知,绘制过这两点的cylinder。
方法不难,这里收集了些网页。
1、http://www.thjsmith.com/40/cylinder-between-two-points-opengl-cVector3D a, b; (the two points you want to draw between)
// This is the default direction for the cylinders to face in OpenGL
Vector3D z = Vector3D(0,0,1);
// Get diff between two points you want cylinder along
Vector3D p = (a - b);
// Get CROSS product (the axis of rotation)
Vector3D t = CROSS_PRODUCT (z , p);
// Get angle. LENGTH is magnitude of the vector
double angle = 180 / PI * acos ((DOT_PRODUCT(z, p) / p.LENGTH());
//glTranslated(b.x,b.y,b.z);
glTranslated(a.x,a.y,a.z);
glRotated(angle,t.x,t.y,t.z);
gluQuadricOrientation(YourQuadric,GLU_OUTSIDE);
gluCylinder(YourQuadric, RADIUS, RADIUS, p.LENGTH(), SEGS1, SEGS2);
2、http://www.opengl.org/discussion_boards/showthread.php/144437-glucylinder-between-two-points
procedure CylinderGL(p1,p2:point3d;r1,r2:double;precision:integer);
var quadratic:pGLUquadricObj;
height,dx,dy,dz:double;
v,vx,vy,vz,rx,ry,ax,r2d:double;
tmp:point3d;
begin
if (p1.x=p2.x) and (p1.z=p2.z) and (p1.y<p2.y) then
begin
tmp:=p1;
p1:=p2;
p2:=tmp;
end;
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);
// 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 glRotated(ax, rx, ry, 0); // Rotate about rotation vector
//create a pointer to the quadric object
quadratic:=gluNewQuadric;
//tell it to smooth normals
gluQuadricNormals(quadratic, GLU_SMOOTH);
//draw the clyinder
gluCylinder(quadratic,r1,r2,height,precision,precision); // Draw A cylinder
glpopmatrix;
//delete the pointer
gluDeleteQuadric(quadratic);
end;