CS 151: Lab #12

Lab Exercise 12: 3D Turtle

Main course page

The purpose of this lab is to give you one more chance to extend your drawing system. You should have a reasonable understanding of how all the pieces fit together, so now we're going to swap out the standard 2D turtle and put in a 3D turtle. All of your 2D turtle programs should continue to work just fine, but now you can make 3D shapes as well. In addition, you'll be able to rotate your completed drawings with the mouse.

Tasks

The lab and assignment consist of three parts.

The goal is to be able to make 3D shapes and scenes as easily as 2D. You should be able to create a Cube class, for example, as well as 3D trees and other L-system shapes that work exactly the same way as their 2D counterparts.

  1. Create a folder called Lab12. Download the 3D turtle file. Once you have it downloaded, run it by itself just to make sure the scene draws and rotates correctly when you click and drag the left mouse button. Feel free to look at the code. It's all in python. The test method of the Turtle3D class, for example, is just a set of turtle commands.
  2. Make a copy of your versions of the files transformer.py, shapes.py, and tree.py, and put them in the Lab12 folder.
  3. One difference between the standard turtle and the 3D turtle is that the latter is implemented as a class. Therefore, you need to create a single instance of the turtle object that will be used by all Transformer objects.

    In the Transformer file, first create a global variable called turtle at the top of the file. Assign it the value None.

    Second, in the Transformer init function, replace the turtleUtils.setup line with the following two lines. The first line tells python to use the global version of turtle variable, and the second line creates a Turtle3D object and tells it to make the initial window 800x800. By putting the Turtle3D object in a variable called turtle, we can still use things like turtle.forward, turtle.left, and turtle.right.

      global turtle
      turtle = turtleTk3D.Turtle3D(800, 800)
    
  4. So what else needs to be changed? You probably want to go through the following list for the Transformer file.
    • Add methods to the Transformer class called roll, pitch, and yaw, which call their turtle3D counterparts.
    • Add a method to the Transformer class called width, which just calls its turtled3D counterpart.
    • Edit the Transformer place function so it has an optional zpos argument with a default value of zero.
    • The turtle.position() method now returns (x, y, z) instead of just (x, y). There are likely some places in your Transformer forward method that need updating handle the z coordinate. As the turtle.goto function now has a third (optional) argument, you may want to jitter the 'broken' and 'jitter' cases in the z direction in exactly the same way you're jittering the x and y directions.
    • Add cases in your drawString method for pitch (& and ^) and for roll (\ and /). In each case the first symbol turns by -angle and the second symbol turns by +angle. Note that you will have to use the string '\\' for the forward slash because it is a special escape character.
  5. Now what needs to be changed in shapes.py? The answer is not much.
    • Add three optional fields to the draw function, placing the new arguments at the end of the argument list. The arguments should be called roll, pitch, and zpos. Each argument should have a default value of 0. (The orientation parameter already holds the yaw.)
    • Add zpos to the argument list of the Transformer place call.
    • Make a width field for the Shape class. Then make a setWidth accessor method. Then, in the Shape draw method, pass self.width into the width function of the Transformer object before you draw the string.
    • Right after you call the orient method, call the Transformer roll method with the roll argument. Immediately after, call the pitch Transformer method with the pitch argument.
  6. Try running the test file basicscene.py. This is a simple example of how to begin building a 3D scene.
  7. What needs to be changed in tree.py? Again, the answer is not much. Just add the roll, pitch, and zpos arguments and their default values (all 0) to the draw method. Then pass the roll, pitch, and zpos arguments on to the parent draw method.

    Download and run the following scene file using a 3D L-system.

  8. Make a new shape class that generates a 3D shape, something like a cube. Test it out using each of your drawing styles. Place several copies of the object into the scene, making use of the zpos argument to give depth to the scene.

When you are done with the lab exercises, go ahead and get started on the assignment.


Appendix: Turtle3D Documentation

The Turtle3D class implements a 3D turtle abstraction using the Tkinter package.

The Turtle3D class includes the following methods for public use.