Jump to content
  • Advertisement
Sign in to follow this  
GeoLancer

OpenGL Half cylinders?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 iteration
glBegin(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 iteration
glBegin(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 iteration
glBegin(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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!