Objectives:
CGT 215 Lab 6 Learning Functions
• Learn how to create and use functions.
• Understand the concept of reusability in programming by designing functions.
• Practice software design approach of breaking the task on independent units.
Description:
Create three different scenes. In the first, use a function for drawing a tree. Then use this to modify and write different version of the same function by using overloading. Finally, create and use functions to develop three different scenes.
The following code: generates the image on the right
void renderFrame(float lastFrameDuration) {
enable3d();
drawTree(100, ‐100);
drawPine(100, 100);
drawTree(‐100, ‐100);
drawPine(‐100, 100);
}
Outline:
Use the template that has three new functions defined. Two of them draw different kind of trees at the supplied coordinates. You need to provide the x and the z coordinates as their parameters.
Your task is to create three new functions that draw a scene.
Scene 1: Draw a tree at the center of the screen. Every 200 frames, the type of tree that is drawn should swap to the other. NOTE: one pass through the renderFrame function is one frame. You will need a global counter. The simplest way is:
static int counter;
counter++;
Scene 2: Draw a grid of trees. The dimensions of this grid must be the parameters of the function. So, if you call forest(2,2) it will make 4 trees, forest (3,5) will render 15. Additionally, the trees need to alternate (see the picture below). HINT: Think of odd and even for the alternating trees.
Scene 3: Do something interesting. Make a function that draws the trees in some interesting and unique way. You could use some mathematical function to find the coordinates, or you can try to spell a letter or something. Be creative (below is an example, ignore the teapot).
HINT: An array is a great way to store lots of data, particularly if it can’t be generated repetitively with a loop alone! An array can make very specific data to be more efficient!
Grading:
The key component of this task is to use functions, call, and reuse them correctly with parameters. If you pack everything into one function, or simply write everything straight through renderFrame, you will not receive full credit!