# Drawing a cylinder in “raw” WebGL

We were given an assignment in Computer Graphics to draw a cylinder in WebGL using libraries such as gl-matrix and cuon. That is to say,  not using Three.js. We couldn’t find any tutorials that matched our problem, so hopefully this one will be of help to other students. If you don’t want to read all this, here is the code and here is the demo (sorry no controls, I wanted to keep it focused). :) I suppose this also works to show how to store color information in the same array as your vertices, and how to use .glsl files.

This example uses webgl-utils.js, webgl-debug.js, cuon-utils.js, and  cuon-matrix.js. I’m assuming you’re already comfortable with WebGL syntax, vertex buffers, and different ways of drawing, so I won’t go into binding buffers etc. We’ll be drawing the bottom and top of the cylinder with triangle fans and the walls with triangle strip. Because that was our assignment.

First: A reminder of the difference between triangle strips and triangle fans:

## Triangle Strip

Every triangle shares two vertices. You first define 3 vertices to create the first triangle,  you then only need to define one new vertex to create the next triangle, reusing the previous two vertices. If$N=$  the number of triangles you want to draw, you will need $N+2$ vertices.

## Triangle Fan

The name might be confusing as it does not actually draw a nice, circular fan by default; All the triangles of a triangle fan share a common, central, vertex (in the image, A). You need to define 3 vertices for the first triangle, then 2 new vertices for the next.  If$N=$  the number of triangles you want to draw, you will need $N+2$ vertices.

## The Math

The hard part about drawing a cylinder is figuring out how to draw a circle. Once you have a circle (the cylinder’s bottom), you can use the exact same values for $x$ and $z$, but with a different $y$ in order to define the cylinder’s top. Once you have the top and the bottom you can use the same vertices to draw the walls!

Any given circle with a radius $r$ can be described as $x^2+y^2=r^2$ with its origin in  $(a,b)$, where $x=a+rcos\Theta$ and $y=b+rsin\Theta$, and $0<\Theta<2\Pi$. I choose $a=b=0,r=1$ so those are now out of the equations.

Our parametric equations are now

$x(t)=cost,y(t)=sint$

Where t is the angle between the x-axis and the segments that make up the circle. First we need to decide how many segments (triangles) we should use.  4 segments will give you a cube, more than 4 will give you a pentagonal prism which becomes a cylinder if we use enough segments. The more segments, the smaller the angle will be per segment (imagine dividing a pie into 18 pieces vs 4 pieces).

I will create a function that will take a number of segments as input and generate the vertices needed for the top, bottom and sides.

## The Code

This is the function:

The second and third line is color and alpha values that I store in the same array as the vertices.

The fourth line defines the angle (in radians!) of each segment.  $\frac{360}{segments}$ gives an angle in degrees per segment. The formula for converting radians to degrees is $degs=rads*\frac{180}{\Pi}$. So to find the angle in radians we flip it around: $rads=\frac{\Pi}{180}*\frac{360}{segments}$. There’s our $\Theta$! Now we just need to iterate it from 0 to $2\Pi$.

Remember that  $cos0=1,cos\frac{\Pi}{2}=0,cos\Pi=-1,cos\frac{3\Pi}{2}=0,cos2\Pi=1$ and $sin0=0,sin\frac{\Pi}{2}=1,sin\Pi=0,sin\frac{3\Pi}{2}=-1,sin2\Pi=0$ .

If i = segments and segments = 18, our angle will be 20 and our theta will be $\frac{\Pi}{9}$. At i = segments = 18 we will get: $x=cos(\frac{\Pi}{9}*18)=cos2\Pi=1$ and we will have gone a full circle. We’ve filled some arrays with vertices, now you just need to draw them!

Take a look at the code here, and the demo here.