## Lab 10: Adding the Third Dimension to xModels

REAL OBJECTS EXIST in three dimensions, and realistic computer graphics must be able to deal with three-dimensional objects. In the previous lab, you used the xModels applet to create two-dimensional scenes and animations. However, the applet can also use three-dimensional objects and three-dimensional geometric transformations. In this lab, you will add the third dimension to your work with xModels.

Before starting this lab, you should make sure that you are familiar with using the xModels applet to write programs and to view scenes. You should understand the basics of the scene description language, including the use of the define command to create new objects. This material was covered in the previous lab and is not repeated here.

The exercises for this lab ask you to save some files in the "homework" directory in your Linux account. Under Windows, make sure that your math account is mounted as a network drive. Goto the Lab9 folder in the cpsc120 folder and double-click the icon named "Run xModels for Lab10". Assuming that your math account has been mounted, you will be able to directly save files into your homework directory. Please be sure to give your files obvious names, or make a sub-folder for them inside the homework folder.

You can also run xModels off the web by clicking the following button. However, if you do this , you will not be able to save your work:

(Sorry, your browser doesn't do Java!)

### In the Direction of Z

Geometric models can be constructed in three dimensions, as well as in two. Even for three-dimensional models, of course, the image still has to be displayed on a two-dimensional computer screen, but the model of the scene exists, at least in our imagination and the computer's memory, in three dimensions. Once you understand the basic ideas of geometric modeling in two dimensions, the step up to three dimensions is not so hard. Just remember that in addition to the x and y-coordinates that you are used to, there is also a z-coordinate.

Think of z as measuring distance in front of the computer screen (or, if z is negative, behind it). The x-axis is a horizontal line on the computer screen. The y-axis is a vertical line on the screen. The x- and y-axes intersect at a point called the origin, which is at the center of xModels' display area. The z-axis is a line perpendicular to the screen. Like the other two axes, the z-axis goes through the origin. The positive direction of the z-axis points at you out of the screen. Larger positive values are closer to you. Points behind the screen have negative z-values. Any point in three dimensions has three coordinates, giving its position with respect to the x, y, and z axes.

In the previous lab, you worked with two-dimensional objects that lay entirely in the xy-plane. In this lab, we add a z-coordinate, and you can work with objects that lie anywhere in xyz-space. The image that you see on the screen is obtained by projecting the three-dimensional objects onto the two dimensional computer screen. By default, objects appear as they would from the point (0,0,20), which is on the z-axis 20 units in front of the screen. (Any objects or parts of objects that are behind this point are not shown at all.) The number 20 is called the viewing distance. You can set a different viewing distance for a scene by using the viewDistance command in your scene description. For example, the command

viewDistance 50

sets the viewing distance to 50. This means that the objects are projected onto the xy-plane from the point (0,0,50). The viewDistance can only occur once in a scene description, and it will have the same effect no matter where it occurs.

(This description of viewing distance is somewhat misleading, since xModels will display the same region of the xy-plane, no matter what the viewing distance. You might expect to see a larger region of the xy-plane if you "back up," but this doesn't happen. Things will, however, look more squashed in the z-direction. Objects that actually lie in the xy-plane will look exactly the same no matter how the viewing distance is set.)

The xModels applet, which you launched above, is set up to load several sample programs. When it first starts up, it should be displaying a program called "Flaps." Click on the "RENDER!" button to see what this animation looks like. The object in the animation is made from eight rectangles, but the rectangles have been rotated out of the xy-plane so that the object as whole is three-dimensional. To understand the scene description, you'll have to read the next section of the lab. However, you might want to try adding a viewDistance command to the scene description. Try several different distance settings. In particular, you should try viewDistance 5, which places the viewing point inside the object. Try to understand what you see.

### Three-Dimensional Objects and Transformations

The basic building blocs of three-dimensional models in the xModels applet still include line, circle, square and polygon, which start out as two-dimensional objects in the xy-plane but which can be translated or rotated out of that plane. (You saw an example of this in the "Flaps" program above.)

There are also a few three-dimensional basic objects in xModels: A cube is a one-by-one-by-one unit cube, centered at the point (0,0,0). A cone is a cone that just fits inside the unit cube, and a cylinder is a similarly-sized cylinder. These objects are pretty small, but they can of course be scaled. For example, if you want to see what a cone looks like, try a scene description that contains the command:

cone scale 10

There is also a polygon_3D command, which constructs a polygon from a list of three-dimensional points instead of two-dimensional points. Later in the lab, you'll see two other commands, lathe and extrude, which can produce interesting objects.

Just as in two-dimensions, the geometric transformations in three dimensions include scaling, translation, and rotation.

The scale command can be used with one, two, or three parameters. With one parameter, as in scale 5, it magnifies an object equally in all three directions. If three parameters are given, they specify different scaling factors in each direction. For example,

```                       cube scale 5 0.5 2
```

makes a rectangular solid that is 5 units long in the x-direction, 0.5 units high in the y-direction, and 2 units thick in the z-direction. If a scale has only two parameters, then the same scaling factor is used in the z-direction as in the y-direction. (This version is provided mostly for use in two dimensions, where any scaling in the z-direction has no effect in any case.)

The translate command can also be used with one, two, or three parameters. In its most general form, with all three parameters, it specifies motion in all three directions. With one parameter, it specifies movement in the x-direction only, and with two parameters, it specifies movement in the x-direction and y-direction only. There is a command ztranslate for moving an object in the z-direction only. For example, "square ztranslate 5" represents a square that has been moved 5 units forward out of the screen. (You already saw the analogous commands xtranslate and ytranslate in the previous lab.)

Scaling and translation are pretty much the same in three dimensions as in two, but rotation in three dimensions is another matter. In two dimensions, an object is rotated about a point. A three-dimensional object must be rotated about a line, like a top spinning about its axis. In xModels, there are three rotations commands, for rotation about the x-axis, the y-axis, and the z-axis, respectively. The commands are called xrotate, yrotate, and zrotate. (The zrotate command is actually the same as the plain old rotate.)

It is easiest to understand rotation in three dimensions by looking at examples. Enter the following simple scene description into a new program in xModels, and render it:

```                    animate 60
square scale 10 yrotate 0:360
```

You will see a square rotating about its vertical axis. Note that the edge of the square that is farther from you looks shorter, as it should. Try the same scene with the square changed to a cube. Try using xrotate and zrotate instead of yrotate.

Next, try the following example, which illustrates a translation in the z direction followed by a rotation:

```                     animate 60
square scale 5 ztranslate 8 yrotate 0:360
```

The ztranslate command moves the square 8 units forward towards you. The yrotate command then sends it circling away from you and back. Try using xtranslate instead of ztranslate. Try xrotate and zrotate in place of yrotate. Make sure that you understand what you are seeing.

Of course, a lot of the power of xModels comes from its ability to construct complex, hierarchical models using the define command. You can use define to define three dimensional objects, just as you used it in two dimensions. You'll work with some defined objects in the exercises at the end of the lab.

The example program "NestedSquares3D" uses several levels of object definitions to create what might be the ultimate "nested squares" example. Each square rotates on its own, but it also takes part in the rotation of all the squares in which it is nested. I find the result visually interesting, kind of like a mobile.

### Lathing and Extrusion

Modeling real objects (like cars or faces) in 3D requires that they be approximated with large numbers of polygons, perhaps hundreds of polygons in one object. You won't want to do anything so complicated with xModels. But xModels does have two ways of producing certain types of complicated objects. The methods are called lathing and extrusion. These are standard operations in three-dimensional graphics. The idea is similar in each case: a specified figure is copied several times, and the vertices of the copies are connected with line segments. For lathing, the copies are obtained by rotating the original around the y-axis. For extrusion, the copies are obtained by translating the original in the z-direction. In xModels, the commands for performing lathing and extrusion are lathe and extrude.

The lathe command takes a sequence of points in the xy-plane and connects them with line segments. It then takes the resulting figure and makes several copies of it by rotating it around the y-axis. The first parameter of the lathe command indicates the number of copies. This is followed by a list of the x and y coordinates of the points in the xy-plane. This is easier to understand if you see an example. The command "lathe 12 3,3 7,-3" says that the line from the point (3,3) to the point (7,-3) is to be copied 12 times. The copies are evenly spread out around the y-axis. With 12 copies, they are spaced every 30 degrees. The endpoints of the 12 lines are then connected, giving an object that looks like a lampshade. To see it, type in the following scene description:

```                 animate 60
lathe  12  3,3  7,-3
yrotate 0:360
```

If you change the 12 to a 4, only 4 copies of the line will be made. The resulting object looks like a truncated pyramid. You could also try adding another point or two to the end of the lathe command.

The extrude command is similar to lathe. It also takes a sequence of points in the xy-plane, connects them with line segments, copies the resulting figure a specified number of times, and then joins the vertices of the copies with more line segments. However in this case, the copies are translated in the z direction rather than rotated around the y-axis. The copies are stacked up in front of and behind the screen. Each copy is separated from the next by a distance of one unit. The simple example "extrude 10 -1,0 1,0" will create a ladder-like object with 10 rungs. (It will lie along the z-axis, so if you want to see it, you should apply an xrotate 90 to it.)

The sample program "LatheAndExtrude" contains several examples of lathing and extrusion. You should read the scene description and render it.

### Exercises

These exercises are due next Friday, November 22. For Exercises 1, 2, 3, and 4, you should save a program in your homework directory. In the lab report that you turn in, you should tell me the file names for your programs, and if you are working with someone, you should tell me whose directory to look in to find the program. You should turn in your answers to Exercise 5 on paper.

Exercise 1: Define an object that consists of a large cube, ten units on a side, with a circle on each of its six faces. Each circle should be a different color. You will have to work to get all the circles properly oriented and positioned. You'll need some commands along the lines of "circle scale 8 xrotate 90 ytranslate 5". Make an animation that shows the entire object rotating as a whole. If you like, use the animated clock from the previous lab exercises instead of a circle.

Exercise 2: The sample program "Wagon," which was also used in the previous lab, shows a simple two-dimensional wagon. Convert it into a three dimensional wagon. Use four wheels. Use a cube instead of a square as the starting point for the body of the wagon. Try out your wagon in an animation with the command

```                wagon xtranslate -10:10 yrotate 60 xrotate 15
```

You should see a wagon that looks like it's driving away from you up a hill. Turn in your program for the animation.

Exercise 3: The sample program Goblet.txt draws a picture that looks something like a goblet or wine glass. Lathing can be used to draw a goblet since a goblet is radially symmetric. Write a program that uses lathing to draw a radially symmetric object such as a mushroom or bell (like those fold-out bell-shaped paper wedding decorations.)

Exercise 4: Use the xModels applet to make a 3D animation of your choice. Try to be creative and/or aesthetic.

Exercise 5: Write an essay discussing the differences that you have observed between two-dimensional and three-dimensional graphics. Why is three-D graphics harder? What additional skills do you need in order to work in three dimensions? Is it worth the extra effort?

David Eck, November 2002