how to draw cylinder between points
- From: "bob the builder" <brulsmurf@xxxxxxxxxxx>
- Date: 12 Mar 2006 05:57:49 -0800
Hello, i would like to draw a cylinder beween two coordinates. I have
found the following idea in this group and tried to program it. But it
doenst work. maybe i have done something wrong with degrees and rads or
something. if you can point out the mistake(s) i made, please do so.
/*
Let P1=(x1,y1,z1) and P2=(x2,y2,z2) be your endpoints:
1) Calculate V (directional vector of your axis);
V=(v1,v2,v3)=((x2-x1),(y2-y1),(z2-z1));
2) Calculate L = lenght of V = sqrt(v1*v1+v2*v2+v3*v3) (this will the
lenght of your cylinder);
3) Calculate the cosine angle between between V and the z-axis (wich is
the default cylinder axis in Opengl)
c=cos(theta)=v3/L;
4) Calculate the N, the normal vector to both V and the Z-axis (this
the the normal direction to the plane they span, it will be
your rotation axis);
N= V X K = (n1,n2,n3)
=(v2,-v1,0);
5) Calculate the sin between between V and the z-axis:
s=sin(theta)= sqrt(v1*v1+v2*v2 )/sqrt(v1*v1+v2*v2+v3*v3 );
6) Calculate the angle: theta =arctan(s/c);
7) Your are ready to proceed:
.... glrotatef(theta,v2,-v1,0);
gltranslatef(x1,y1,z1);
gluCylinder(cyl,radius,radius,L,lat,long);
*/
void drawPipeT(float x1, float y1, float z1, float x2, float y2, float
z2)
{
float v1, v2, v3;
float n1, n2, n3;
float vLenght;
float c;
float s;
float theta;
v1 = x2-x1;
v2 = y2-y1;
v3 = z2-z1;
vLenght = sqrt(v1*v1 + v2*v2 + v3*v3);
c = v3/vLenght;
n1 = v2; n2 = -v1; n3 = 0;
s = sqrt(v1*v1 + v2*v2)/sqrt(v1*v1 + v2*v2 + v3*v3);
theta = atan(s/c);
GLUquadricObj *cylinder1;
cylinder1 = gluNewQuadric();
gluQuadricDrawStyle(cylinder1,GLU_FILL);
glColor3d(0,0,1);
glPushMatrix();
glRotatef(theta,n1,n2,n3);
//glTranslated(x1,y1,z1); need to get rotation ok first
gluCylinder(cylinder1,0.1,0.1,vLenght,10,1);
glPopMatrix();
// from here code find the errors
// draw N
glPushMatrix();
glLineWidth(4.0); // fat line
glColor3d(1,0,0);
glBegin( GL_LINES );
glVertex3f( 0, 0, 0);
glVertex3f( n1, n2, n3);
glEnd();
glPopMatrix();
// draw endpoints
glPushMatrix();
glTranslated(x1,y1,z1);
glutSolidSphere(0.3,12,12);
glPopMatrix();
glPushMatrix();
glTranslated(x2,y2,z2);
glutSolidSphere(0.3,12,12);
glPopMatrix();
}
.
- Follow-Ups:
- Re: how to draw cylinder between points
- From: bob the builder
- Re: how to draw cylinder between points
- Prev by Date: Re: lightmap editor
- Next by Date: Re: Does This Look Even?
- Previous by thread: lightmap editor
- Next by thread: Re: how to draw cylinder between points
- Index(es):
Relevant Pages
|