Search code examples
opengl3dopengl-3

Graphics : How to create a 3D cylinder around a line segment in opengl?


I have line segment with 2 end points, I wanted to create a cylinder around it with some radius r.


Solution

  • There are enough examples on the Web, here's a simple description. Think of the cylinder as like a CD-rack. Consider the "bottom surface" of the cylinder. You know the center of this surface is one end of the line segment. Now the edge vertices of this surface, are just the vertices of a group of triangles (say 8) made with one vertex as this center of the surface. To make the cylinder complete, make a stack of such surfaces till the other end of the line segment.

    How to find the vertices of each line intersecting the surface (ex, there are 8 lines in below figure) ? The loop goes like this:

    for(each line)
    {
        float angle_degrees = 360 * (id of line) / (number of lines);
    
        float x = radius * cos(angle_radians);
        float y = radius * sin(angle_radians);
    }
    

    zwiggler drawing:

    enter image description here

    Some other discussions on this subject: Number of Sides Required to draw a circle in OpenGL