Page 70 - Computer Graphics Handout
P. 70
// subdivide all but inner triangle
divide_triangle(a, ab, ac, k-1);
divide_triangle(c, ac, bc, k-1);
divide_triangle(b, bc, ab, k-1);
}
else triangle(a,b,c); /* draw triangle at end of recursion */
}
13
The display function is now almost trivial. It uses a global value of n determined by the main programto fix the number of
subdivision steps we would like, and it calls divide_triangle once with the single function call
divide_triangle(v[0], v[1], v[2], Ndivisions);
where Ndivisions is the number of times we want to subdivide the original triangle If we do not account for vertices shared by two
vertices and treat each triangle independently, then each subdivision triples the number of vertices, giving us
Nvertices = 3Ndivisions+1.
We set up the buffer object exactly as we did previously, and we can then render allthe triangles by
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, Nvertices);
glFlush();
}
The rest of the program is almost identical to our previous gasket program. Output for five subdivision steps is shown in Figure 2.39.
The complete program is given in Appendix A.
2.10 THE THREE-DIMENSIONAL GASKET
13
Note that often we have no convenient way to pass variables to GLUT callbacks other than through
global parameters. Although we prefer not to pass values in such a manner, because the formof these
functions is fixed, we have no good alternative.
70

