Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Creating cylinder

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    9

    Creating cylinder

    Hi!

    I'm absolute new to OpenGl and have the following question:

    I'd like to draw a cylinder and specify the normal vectors for the lighting. I want to do this without the gluCylinder()-Methode.

    Now I'm not sure how to start defining the coordinates for the cylinder.

    Thanks for your help!

    enne

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2008
    Location
    USA
    Posts
    135

    Re: Creating cylinder

    A perfect cylinder is two circles (the ends) separated by some distance with the middle space filled in. The best way to render one is to approximate the circles with triangles. Pick the number of slices of a circle you want to use (think slices of a pie), pick a radius, and pick a length of the cylinder.
    Now, the coordinates of a circle are r*sin(theta) and r*cos(theta), so I'm going to use a for loop, increment theta, and use those values to construct the cylinder.
    Something like this:

    Code :
    float radius, halfLength;
    int slices;
    for(int i=0; i<slices; i++) {
    float theta = ((float)i)*2.0*M_PI;
    float nextTheta = ((float)i+1)*2.0*M_PI;
    glBegin(GL_TRIANGLE_STRIP);
    /*vertex at middle of end */ glVertex3f(0.0, halfLength, 0.0);
    /*vertices at edges of circle*/ glVertex3f(radius*cos(theta), halfLength, radius*sin(theta));
                                              glVertex3f (radius*cos(nextTheta), halfLength, radius*sin(nextTheta));
    /* the same vertices at the bottom of the cylinder*/
     glVertex3f (radius*cos(nextTheta), -halfLength, radius*sin(nextTheta));
    glVertex3f(radius*cos(theta), -halfLength, radius*sin(theta));
    glVertex3f(0.0, -halfLength, 0.0);
    glEnd();
    }

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    9

    Re: Creating cylinder

    Perfect, thanks todayman

    Cheers,

    enne

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    9

    Re: Creating cylinder

    One last question: I'm not sure how to create the normal-vectors. Can I use the same parameters as for the vertices?

  5. #5
    Junior Member Regular Contributor
    Join Date
    Dec 2008
    Location
    USA
    Posts
    135

    Re: Creating cylinder

    The sides of the cylinder have normals that point away from the center, along what would be a radius, so those are (cos(theta), 0.0, sin(theta)), and the top/bottom faces straight up/straight down so those normals are (0.0, (-)1.0, 0.0).

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    9

    Re: Creating cylinder

    Ok, thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •