# OpenGL Half cylinders?

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

## Recommended Posts

Hi everyone! I am new to opengl and was wondering how exactly do i draw half a cylinder in opengl? I was doing it the rudimentary way like this: for( float j = 0; j < 500; j++ ) { glTranslatef( .005, 0, 0 ); glBegin( GL_POLYGON ); for( float i = 0; i < 360; i++ ) { float angle = (i*3.14)/360; glVertex3f( 0, cos(angle)/2.5, sin(angle)/2.5 ); } glEnd( ); } Meaning i was drawing about several hundred half circled polygons reall close to each other to fill up the gaps, thus creating a half cylinder. Ofcourse, this caused too much cpu consumption, i.e. LAG, so now i have no idea. Can anyone help me,please!!! thnkx

##### Share on other sites
First, please post code snippets in a special container by surrounding [ source ] and [ /source ] (but without the inner spaces). That enhances readability since it e.g. preserves indentation.

You should not render a stack of half circles to apprimate a cut cyclinder. Instead, split up the surface of the half cylinder into several surfaces, and draw them one-by-one. There is a top and a buttom cap, both a half circle. Render them similarly as you've done the stacked half circles before. Then there is the half "mantle" (I don't know the correct word in english, sorry) and the flat closing of the "mantle".

Use something like the following (notice that this is untested code and for explanation only):
int STEPS = 16;float DELTA = 3.1415/STEPS;float half_height = 10f;float radius = 2.0f;// rendering of top cap; one corner per iterationglBegin(GL_POLYGON);for(int step=0; step<=STEPS; ++step) { // notice the <=   float angle = step*DELTA;   glVertex3f(half_height,radius*cos(angle),radius*sin(angle));}glEnd();// rendering of bottom cap; one corner per iterationglBegin(GL_POLYGON);for(int step=0; step<=STEPS; ++step) { // notice the <=   float angle = step*DELTA;   glVertex3f(-half_height,radius*cos(angle),radius*sin(angle));}glEnd();// rendering of "mantle"; one quad per iterationglBegin(GL_QUADS);for(int step=0; step<STEPS; ++step) { // notice the < only   float angle1 = step*DELTA; // at this angle the quad starts   float angle2 = (step+1)*DELTA; // look ahead of next angle: at this angle the quad ends   float rsine1 = radius*sin(angle1);   float rcosine1 = radius*cos(angle1);   float rsine2 = radius*sin(angle2);   float rcosine2 = radius*cos(angle2);   glVertex3f(half_height,rcosine1,rsine1);   glVertex3f(half_height,rcosine2,rsine2);   glVertex3f(-half_height,rcosine2,rsine2);   glVertex3f(-half_height,rcosine1,rsine1);}glEnd();// rendering of closing of "mantle"glBegin(GL_QUADS);glVertex3f(half_height,radius,0);glVertex3f(half_height,-radius,radius);glVertex3f(-half_height,-radius,radius);glVertex3f(-half_height,radius,0);glEnd();

See that there are quadrangles rendered along the axis where your solution has the stacked half circles. So the "gaps" are closed totally but with much less effort. You may increase STEPS if you want the surfaces to become more "rounded".

( BTW: There are some general optimizations made as well. Using the pre-increment (++i) is a little bit more efficient than using the post-increment (i++). Furthurmore, repeated computations of the same sine or cosine are supressed, and also multiplication is preferred over division. However, you could ignore this if you want. )

EDIT: some minor bugs in the explanation code corrected, and comments made more verbose.

[Edited by - haegarr on November 12, 2005 6:46:32 AM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 9
• 36
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015681
×