Spaghetti Programmer
12-09-2009, 09:32 AM
Hi all.
I try to summarize the problem.
I'm triyng to draw 2 spheres connected by a cylinder.
If I only perfom x-axe rotation it works (the 2 sphere are perfectly connected). The same if I only perform the y-axe rotation.
Unfortunately if I perform 2 consequently rotation the
second sphere is not correcly aligned with the cylinder.
Here the code:
..
// draw first sphere
glPushMatrix();
glTranslatef(x1, y1, z1);
glutSolidSphere(10.0f, 15, 15);
// draw cylinder
glPushMatrix();
rx = atan2f((x2-x1), (z2-z1)) * (360.0f / (2.0f * PI));
ry = atan2f((y1-y2), (z2-z1)) * (360.0f / (2.0f * PI));
glRotatef(rx, 0.0f, 1.0f, 0.0f);
glRotatef(ry, 1.0f, 0.0f, 0.0f);
gluCylinder(cyl, 2.0, 2.0, height, 15, 15);
glPopMatrix();
glPopMatrix();
// draw second sphere
glTranslatef(x2, y2, z2);
glutSolidSphere(10.0f, 15, 15);
..
Anyone can explain me why it doesn't work?
Thanks
S.
I try to summarize the problem.
I'm triyng to draw 2 spheres connected by a cylinder.
If I only perfom x-axe rotation it works (the 2 sphere are perfectly connected). The same if I only perform the y-axe rotation.
Unfortunately if I perform 2 consequently rotation the
second sphere is not correcly aligned with the cylinder.
Here the code:
..
// draw first sphere
glPushMatrix();
glTranslatef(x1, y1, z1);
glutSolidSphere(10.0f, 15, 15);
// draw cylinder
glPushMatrix();
rx = atan2f((x2-x1), (z2-z1)) * (360.0f / (2.0f * PI));
ry = atan2f((y1-y2), (z2-z1)) * (360.0f / (2.0f * PI));
glRotatef(rx, 0.0f, 1.0f, 0.0f);
glRotatef(ry, 1.0f, 0.0f, 0.0f);
gluCylinder(cyl, 2.0, 2.0, height, 15, 15);
glPopMatrix();
glPopMatrix();
// draw second sphere
glTranslatef(x2, y2, z2);
glutSolidSphere(10.0f, 15, 15);
..
Anyone can explain me why it doesn't work?
Thanks
S.