# 3d formula for solid tube / pipe

Hi,

can someone tell me the formula for creating 3D tubes? (For example, to simulate a snake, pipes or a tube)

I´ve tried to figure it out on paper, but i´m not sure how to put it into practive…

Would be glad if you could give me some hints or a link to a website!

Thanks
Malakai

first of all- that’s not so advanced (someone else would have said that, if not me )

secondly- if it’s a straight cylinder, it could look like this:

``````void DrawCylinder(float R1, float R2, float H) {
int    i, j, steps = 32;
float  phi, dphi = 2.*M_PI / (float)(steps);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glOrtho(-10., 10., -10., 10., 1., 100.);

glMatrixMode(GL_MODELVIEW);
gluLookAt(10., 10., 5., 0., 0., 0., 0., 0., 1.);

glEnable(GL_DEPTH_TEST);

glBegin(GL_TRIANGLE_STRIP);

for(i = 0, phi = 0.; i <= steps; i ++, phi += dphi) {

if((i%2) == 0)
glColor3f(1., 0., 0.);
else
glColor3f(0., 1., 0.);

glVertex3f(R1*cos(phi), R1*sin(phi), -H/2.);
glVertex3f(R2*cos(phi), R2*sin(phi),  H/2.); }

glEnd(); }
``````

if R1==R2 it is a cylinder, otherwise a cone. you can use glTranslatef/glRotatef/glScalef to put it at any position you want .

Hello,

tomorrow i will be more specific. 