# Generate Cylinder Geometry

This topic is 5000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to find some code that will generate triangle geometry for a cylinder. Something similar to gluCylinder (the source code for gluCylinder would work perfectly). Does anyone know where I can find this? Thanks a lot! Kelly

##### Share on other sites
Off the top of my head:

float angle = 0.0f;float step = TWO_PI / numsteps;float hh = height * 0.5f;for (int i = 0; i < numsteps; i++){    float s = sinf(angle);    float c = cosf(angle);    verts[numverts++].Set(s * radius, c * radius, -hh);    verts[numverts++].Set(s * radius, c * radius, hh);    angle += step;}

numsteps, radius and height are input parameters. numsteps determines the resolution, or level of detail, of the cylinder.

The cylinder is centered at the origin and oriented along the z axis. After construction, it can then be repositioned and oriented using the transformation technique of your choice. Given three axes and a position, it can also be constructed in place with a little more verbose code.

Note that you could use separate x and y radii, and create an ellipsoidal prism if desired.

Triangulation I leave up to you, but if you have trouble with it I can give you that code too.

Note that with some additional coding, you can get automatic level of detail by storing several sets of indices for progressively coarser versions of the cylinder.

Does that help?

##### Share on other sites
you use simple trig to do it,
void DrawCyl(float height){for(float x=0; x<360; x+=10){float radianA=(x/180)*M_PI;float radianB=((x+10)/180)*M_PI;//first trianglevertex( cos(radianA),height/2,sin(radianA) );vertex( cos(radianA),-height/2,sin(radianA) );vertex( cos(radianB),-height/2,sin(radianB) );//next trianglevertex( cos(radianA),height/2,sin(radianA) );vertex( cos(radianB),height/2,sin(radianB) );vertex( cos(radianB),-height/2,sin(radianB) );}}

just a sample off the top of my head

##### Share on other sites
Thanks for the reply. However, I also need to specify base and top radius. If one of them is 0, then a cone should be generated. Sorry I didn't specify this in the original post (gluCylinder takes in baseRadius, topRadius, height, slices, and stacks; slices are sub-divisions around the z-axis, stacks are sub-divisions along the z-axis). I will see if I can convert the code you gave to work in this fashion. Thanks again for the reply!

Kelly

EDIT: Sorry, I missed the part about using separate x and y radii. I will work on that.

##### Share on other sites
Xero-X2 - thank you as well for the code. I will see if I can make use of it.

Kelly

##### Share on other sites
previously defined vertex[MAX_NUM]
int i = 0;
int n = 0;
float sidestep;
sidestep = NUM_SIDES / (2 * PI);
float CYLINDER_HEIGHT;
CYLINDER_HEIGHT = whatever!
while (i < NUM_SIDES)
{
vertex[n].x = sin(i * sidestep) * radius;
vertex[n].y = 0;
vertex[n].z = cos(i * sidestep) * radius;
vertex[n + 1].x = sin((i + 1) * sidestep) * radius;
vertex[n + 1].y = 0;
vertex[n + 1].z = cos(i * sidestep) * radius;
vertex[n + 2].x = sin(i * sidestep) * radius;
vertex[n + 2].y = CYLINDER_HEIGHT;
vertex[n + 2].z = cos(i * sidestep) * radius;
vertex[n + 3].x = sin(((i + 1) * sidestep) * radius;
vertex[n + 3].y = 0;
vertex[n + 3].z = cos(i * sidestep) * radius;
vertex[n + 4].x = sin((i + 1) * sidestep) * radius;
vertex[n + 4].y = CYLINDER_HEIGHT;
vertex[n + 4].z = cos((i + 1) * sidestep) * radius;
vertex[n + 5].x = sin(((i + 1) * sidestep) * radius;
vertex[n + 5].y = CYLINDER_HEIGHT;
vertex[n + 5].z = cos(i * sidestep) * radius;
n = n + 5;
i++;
}

Think that should work - I coded this without thinking!

Good Luck!

##### Share on other sites
// DrawCyl(total height, bottom radius, top raidus);void DrawCyl(float height, float bottom, float top){for(float x=0; x<360; x+=10){float radianA=(x/180)*M_PI;float radianB=((x+10)/180)*M_PI;//first triangle vertex( cos(radianA)*top,height/2,sin(radianA)*top );vertex( cos(radianA)*bottom,-height/2,sin(radianA)*bottom );vertex( cos(radianB)*bottom,-height/2,sin(radianB)*bottom );//next trianglevertex( cos(radianA)*top,height/2,sin(radianA)*top );vertex( cos(radianB)*top,height/2,sin(radianB)*top );vertex( cos(radianB)*bottom,-height/2,sin(radianB)*bottom );}}

An alternation of my above code that will meet you new needs.

##### Share on other sites
Looks like between all our posts you probably have the info you need. For my code, just use a different radius value for the hh and -hh verts to get a different top and bottom radius. For 'stacks', step through the z axis as well as around the perimeter. There are some triangulation details in there you'll have to figure out, but it shouldn't be too bad.

Rather than having a bunch of coincident verts, I would detect radius == 0 as a special case (cone) and construct a circle at one end connected to a point at the other.

##### Share on other sites
Thanks for all the replies. I can figure it out from here. Thanks again!!

Kelly

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×