opengl: cylinder rendering
how can i render a cylinder which takes two points A(x1,y1,z1),B(x2,y2,z2) as its bottom and top's center point? when using glucylinder() function, it always gives me a cylinder along the Z-axis.
Thanks for help
[225 byte] By [
murasaki] at [2007-11-18 1:35:33]

# 2 Re: opengl: cylinder rendering
thanks, i know the basic things.
But the fact is, i have with me a bunch of lines of which two end points are known.
What i wanted to do was to draw cylinders along all those lines.
i took in one of each line's end points as the cylinder's bottom center, then of course using glTranlated() can move the cylinder to where it is supposed to be.
But when trying to determine the cylinder's orientation, i had problems, suppose A(x1,y1,z1),B(x2,y2,z2) my coding is :
for(...)// loop thru all the lines stored in a list
{
glTranslated(x1,y1,z1);
double L;
if(z2>z1)
m_yRotation = tanh((x2-x1)/(z2-z1))*180.0/PI;
else
m_yRotation = tanh((x2-x1)/(z2-z1))*180.0/PI +180.0;
L = sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1));
m_xRotation = -tanh((y2-y1)/L)*180.0/PI;
//glRotated(m_yRotation,0.0,1.0,0.0);// Rotate On The y axis
//glRotated(m_xRotation,1.0,0.0,0.0);// Rotate On The X axis
glucylinder(...);
//glRotated(-m_xRotation,1.0,0.0,0.0);// Rotate On The X axis
//glRotated(-m_yRotation,0.0,1.0,0.0);// Rotate On The y axis
glTranslated(-x1,-y1,-z1);
}
but when rendering, those cylinders somehow out of place, though generally they are where they should be, some of them got bad orientation.
# 3 Re: opengl: cylinder rendering
I would restore the state completely after each cylinder:
for(...)// loop thru all the lines stored in a list
{
glPushMatrix();
glTranslated(x1,y1,z1);
double L;
if(z2>z1)
m_yRotation = tanh((x2-x1)/(z2-z1))*180.0/PI;
else
m_yRotation = tanh((x2-x1)/(z2-z1))*180.0/PI +180.0;
L = sqrt((x2-x1)*(x2-x1)+(z2-z1)*(z2-z1));
m_xRotation = -tanh((y2-y1)/L)*180.0/PI;
glRotated(m_yRotation,0.0,1.0,0.0);// Rotate On The y axis
glRotated(m_xRotation,1.0,0.0,0.0);// Rotate On The X axis
glucylinder(...);
glPopMatrix();
}
I don't help you with math, but if the your first cylinder is OK,
now all others will be OK too.
Alex F at 2007-11-10 8:55:57 >

# 4 Re: opengl: cylinder rendering
thanks Alex.
it actually makes no difference at all whether restores it completely or not. Result is the same.
i was wondering maybe my calculation got some precision problem thanks to the sqrt() and tanh() function.
Because the result is somehow a bit odd.
My case is like this. The whole list of line i have actually includes some lines that are of the opposite orientation.
Say for an instance, line AB and line BC, actually point B is just the middle point of line AC, but AB and BC are of different color, which is why they are dealt with seperatedly.
But it is odd that even when rendering cylinders around AB and BC, the two cylinders did not join together.
i was wondering isn't there a way to draw cylinder around a given line?
Can help me think of another way to do this?