garyw

07-30-2008, 12:35 PM

So I've run into some coding trouble, and tried asking the folks at GameDev.net, but they don't seem to get it, so I thought I'd go for the experts....any help would be much appreciated! Description below. Thanks! Gary

So I am trying to draw a line with an arrow at the end. For the lines I am using GL_LINES, and for the arrow at the end, I am using gluCylinder(), with the base set to zero to make a cone for the arrow head. What I cannot for the life of me seem to figure out, is how to orient the cone so that it is coincident with the original line. This is the approach suggested in the Red Book, using a scalar and cross product to find the angle and axis of rotation.

Psuedocode looks something like:

1. Draw line

2. Calculate 3-vector coincident with line: N = TO - FROM

3. glTranslate from origin to TO

4. glRotate so the z-axis (where gluCylinder draws) is coincident with N

5. call gluCylinder()

Results: the point of the cone in every case is located at TO, but the angle of rotation for each cone is different, and not at all coincident with N. For the rotation I am using:

vector<float> normal(3);

float vx =tTo[0]-tFrom[0]; // tTo and tFrom are the endpoints of the line

float vy =tTo[1]-tFrom[1];

float vz =tTo[2]-tFrom[2];

float dr = sqrt(vx*vx+vy*vy+vz*vz); // use dr to normalize the vector

normal[0]=vx/dr;

normal[1]=vy/dr;

normal[2]=vz/dr;

...

...

float rTd = 180.0 / 3.14159265; // conversion from radians to degrees

glPushMatrix();

glTranslatef(x, y, z);

float mag_n = sqrt (n[1]*n[1]+n[2]*n[2]+n[0]*n[0]);

float theta = rTd*acos(n[2]/mag_n); // angle through which to rotate, from dot product

glRotatef(theta,-1.0*n[1],-1.0*n[0],0); // axis of rotation from cross product

gluCylinder(gluNewQuadric(), 0, .05, .2, 30, 20);

glPopMatrix();

So I am trying to draw a line with an arrow at the end. For the lines I am using GL_LINES, and for the arrow at the end, I am using gluCylinder(), with the base set to zero to make a cone for the arrow head. What I cannot for the life of me seem to figure out, is how to orient the cone so that it is coincident with the original line. This is the approach suggested in the Red Book, using a scalar and cross product to find the angle and axis of rotation.

Psuedocode looks something like:

1. Draw line

2. Calculate 3-vector coincident with line: N = TO - FROM

3. glTranslate from origin to TO

4. glRotate so the z-axis (where gluCylinder draws) is coincident with N

5. call gluCylinder()

Results: the point of the cone in every case is located at TO, but the angle of rotation for each cone is different, and not at all coincident with N. For the rotation I am using:

vector<float> normal(3);

float vx =tTo[0]-tFrom[0]; // tTo and tFrom are the endpoints of the line

float vy =tTo[1]-tFrom[1];

float vz =tTo[2]-tFrom[2];

float dr = sqrt(vx*vx+vy*vy+vz*vz); // use dr to normalize the vector

normal[0]=vx/dr;

normal[1]=vy/dr;

normal[2]=vz/dr;

...

...

float rTd = 180.0 / 3.14159265; // conversion from radians to degrees

glPushMatrix();

glTranslatef(x, y, z);

float mag_n = sqrt (n[1]*n[1]+n[2]*n[2]+n[0]*n[0]);

float theta = rTd*acos(n[2]/mag_n); // angle through which to rotate, from dot product

glRotatef(theta,-1.0*n[1],-1.0*n[0],0); // axis of rotation from cross product

gluCylinder(gluNewQuadric(), 0, .05, .2, 30, 20);

glPopMatrix();