# Draw more complex 3D Models using Triangle Fans in WebGL

This time we look at the last drawing mode, triangle fan, which can be useful for drawing different types of 3d forms. Combine this mode with triangle strip to creating complex models.

This lesson is for members. Join us? **Get access to all 3,000+ tutorials + a community with expert developers around the world.**

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

WebGL

This time we look at the last drawing mode, triangle fan, which can be useful for drawing different types of 3d forms. Combine this mode with triangle strip to creating complex models.

Access all courses and lessons, track your progress, gain confidence and expertise.

Let's take a look at the last drawing mode available for the draw/erase function, GL_TRIANGLE_FAN. The name itself should give you a rough idea of what it does. If you add any paper folded fan, not an electrical rotary fan.

Say we have groups of points like this. The first point, point zero at the top, and the rest spread left to right across the bottom. A triangle fan takes points zero, one, and two, and creates the first triangle, just like both of the other triangle drawing methods. Then, it goes to zero, two, three, then zero, three, four, and so on.

The fan always has that initial starting point that connects all the triangles. It uses that initial point, plus each successive pair of points, to create each successive triangle. In a fan, every triangle shares an edge with at least one of the triangle, and all triangles share that initial point.

Let's see it in action. I'll start with the code we had for the triangle strip example. First, I'll create a center point, we can put all seven values into the array with one push. It will be at zero, zero, zero, and the color will be zero, zero, zero with one for alpha, solid black.

Now, I'll make this for loop go from zero to math pi*2 radians which is equivalent to 0to 360 degrees. I'll increment by 01, and I'll arrange the vertices around in a circle, using the same easy trick. X will be math cosine I, and Y will be math sine I. Z will be zero for now, and I'll set the colors with random values for red, green, and blue, plus one for alpha.

Down to the draw function, I'll just change this to points, just to see what we have. Yep, we have the center black point, and the rest arranged in a circle around that center point.

Let's change this to fan mode. You see we have a solid circle shape, you might notice that it's not quite closed though, we can handle that later. We can now do some more modeling with these vertices. Instead of being flat, let's change the Z positions somehow.

Let's have them go up and down in a sine wave. I'll say Z to math sine i*10*01 OK. That's more interesting. Now, the colors, for red, I'll use the same sine wave, but I'll multiply it by 05, so, it goes from -05 to +05. Then I'll add 05 to that, so, it arranges from 0to 1, which is a valid range for the color channels here. I'll do the same thing for green and blue, but I'll have them use i*8, and i*12 just to get a variation. Cool, these colors now flow nicely together.

Let's handle that little gap. I'll copy all the points here, and paste them down again, after the for loop. I'll save before that, i=math pi*2, that will pull one final point where it needs to be a closer circle. Looks good.

Now, to me this looks like some kind of shell, let's roll with that theme. I am going to try to move the center point up a bit, 09 on the Y-axis, and 03 on the Z-axis. I'll change the center color to white. Now, it looks even more like a shell, let's keep going.

I'll up the resolution by changing the increment to the for loop, then I'll set the clear color to a dark blue. Now, we've got something pretty cool going on. With triangle strip and fans you can start creating some pretty complex models. See what you can do with them.