View Full Version : 3d formula for solid tube / pipe

Malakai2007

12-01-2005, 09:50 AM

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

RigidBody

12-01-2005, 10:21 AM

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

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);

glLoadIdentity();

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

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

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

glEnable(GL_DEPTH_TEST);

glShadeModel(GL_FLAT);

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 .

Malakai2007

12-01-2005, 10:58 AM

Hello,

Thanks for your answer... but i don´t want a plain cylinder...

I mean a curved cylinder - which is build from a path of points... i think this is slighly more advanced. I have code for a plain cylinder already in my app but i don´t know how to realize the curved cylinder.

i recall the graphics gems series has something on the line... game prog gems too.

well, starting from a 3D path made of points, naively I would start by taking the first segment, finding the perpendicular plane, generating some 16/64 points along a circle, take the second segment, do the same and also connect the first series of points to the second... an so on.

if the path do not bend or twist too much, it gives good results... otherwise look for fiber bundles / hopft fibration applied to graphics.

tomorrow i will be more specific. :)

ScottManDeath

12-06-2005, 05:50 PM

Well, easiest would be to do a curve, e.g. Bezier or Catmul-Rom and then generate the Frenet frame at each segment and connect circles transformed by it.

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.