WilfÆs Workshop

Wilf Hey continues his article on vector graphics from the Hands On section of this monthÆs magazine.

When it comes to displaying three-dimensional objects on the computer screen there are two major things to keep in mind.

First: rotation is a much more involved affair than the equivalent procedure in two dimensions. When you rotate a shape in two dimensions you have to deal with a more complex translation than mere straight line movement. The calculation of each new point requires some trigonometry (fortunately not too much) and the new X ordinate is affected by both the old X and the old Y.

Similarly, the new Y ordinate is affected by both the old Z and the old Y. Things quickly become unmanageable when you try to sort out how each of X, Y and Z co-ordinates affect one another, all at the same time. Fortunately, there is a foolproof method: break each rotation into three separate operations û each using the same formula. Any rotary movement in three dimensions is effectively a combination of three movements: pitch, roll and yaw. We know already that roll is our old friend, rotation in two dimensions. Pitch is exactly the same, except that it is rotary movement in the Z-Y direction rather than the X-Y direction. Pitch rotation can be handled by the same two dimensional rotation formula, except that Z takes the place of each X in the formula. Yaw is also the same, except that it is rotary movement in the X-Z direction rather than X-Y û so we can use the same formula again û this time putting Z in place of Y.

So instead of Rotate2D we have the following neat set of routines:

Roll

New X = Old X * COS (angle) - Old Y * SIN (angle)
New Y = Old X * SIN (angle) + Old Y * COS (angle)
New Z = Old Z (unchanged)

Pitch

New Z = Old Z * COS (angle) - Old Y * SIN (angle)
New Y = Old Z * SIN (angle) + Old Y * COS (angle)
New X = Old X (unchanged)

Yaw

New X = Old X * COS (angle) - Old Z * SIN (angle)
New Z = Old X * SIN (angle) + Old Z * COS (angle)
New Y = Old Y (unchanged)

Second: We must do something with our Z ordinates. Where can we put them on the screen? One solution is to do nothing with Z. This is not wrong. After all, point (4, 7, -5) is exactly behind (4,7, 3): if you looked straight on them both, you would only see point (4,7). However we know that there are other things to do: we can look slightly to one side of an object, and see both (4, 7, -5) and (4, 7, 3). The problem is to cram three co-ordinates into two, and thus make the image displayable in two dimensions.

A good, easy compromise is to use what is called the æcabinetÆ projection. It needs two angle values û ALPHA and PHI. If you choose these wisely (try about 60 degrees for ALPHA, and 45 for PHI), you will get a pleasing three-dimensional effect. Note, though, that this is perspective without a vanishing point. All objects sketched with the same ALPHA and PHI will be in the same perspective û which is often fine, especially when the objects are meant to be fairly close to each other.

The secret to successful manipulation of objects in three dimensions is:

If you take these steps you do not need to keep the æcrammedÆ values of X and Y: simply re-display the object (using all three co-ordinates) by first passing it through the æcrammingÆ routine.

There are many other issues with regard to three-dimensional displays: how to make objects look solid (rather than displaying skeletons all the time); how to delete skeleton lines that are actually hidden; how to colour sides of objects so that they look realistic; how to incorporate into the display one, two or even all three æanishing points. All these are possible in most languages that support X-Y co-ordinates. Over coming months we will address these and similar issues, and even do a little animation.

Example Program On The SuperCD
You can copy and run the Visual Basic source provided û the project named 2-3DIMS û which has code to handle manipulation and display of both two dimensional and three dimensional images using a co-ordinate system.

The 2D (two dimensional) examples are Procedures Command1 through Command6; the 3D (three dimensional) examples are Command7 through Command12. They are as follows:

Command1
In this example an octagonal shape is drawn repeatedly, moved to the left (negative X) and upward (positive Y) and then æenlargedÆ by a factor of .95 (so shrinking 5%).

Command2 and Command3
These examples again display our octagonal shape repeatedly, moving and enlarging the shape in between each display.

Command4 and Command5
These examples perform no movement, but enlarge or shrink the shape between each of several successive displays.

Command6
This example shows the display of a shape three times, each time in a different colour and location; in one case the shape is rotated slightly before display.

Command7
This example is a display of a standard cube, featuring a æcabinetÆ (perspective) view with the alpha angle (depth) set at 70 degrees and the phi angle (vantage) set at minus 20 degrees.

Command8
This example is a display of a pyramid-like object with alpha at 60 degrees and phi at 40 degrees û an agreeable (and common) setting for viewing three dimensional objects.

Command9, Command10 and Command11
Here we see, through overlaid animation, the effect of ROLL, PITCH and YAW on the pyramid shape.

Command12
Here is a single static display of the pyramid after a æcomplexÆ rotation, involving all three of roll, pitch and yaw.

You can adapt the routines and methods used here so that you can accomplish both two and three-dimensional displays. Note especially the following routines:

The routines whose names begin with æXÆ are supportive, initialising arrays, setting example shapes and similar activities.

Draw2D is used to draw a two dimensional shape whose co-ordinates are recorded in the array Shape2D. Draw3D is very similar: it draws on the screen a two dimensional shape which is one side of a three dimensional object. Although it is a two dimensional shape (which means it is flat), it can be anywhere in three dimensional space - so each point defining a corner of the shape needs three co-ordinates.

Sketch is the primary procedure used for displaying three dimensional objects: it calls Draw3D repeatedly, which draws on the screen in turn each of the sides of the object. This needs two arrays: Shape3D, mentioned above, records co-ordinates for each corner of each side; Object3D records single numbers pointing to entries in Shape3D, indicating where the co-ordinates for each side are found.

 


The Sketch procedure uses two arrays: Object3D provides vectors for all sides of the object to be illustrated. Each vector points into Shape3D where co-ordinates for each side are recorded.

 


ALPHA is the measure of the angle between the vantage (viewerÆs place) and the horizon: the larger it is, the nearer the horizon appears to be û or to put it another way, the farther away the object seems to be. When ALPHA is high, foreshortening û the exaggerated distortion of nearby things û is greater.

 


PHI is the measure of the angle displayed between the object and the horizon. When PHI is smaller, the impression given is that the vantage is lower, and closer to the object itself. Note that your eye expects to see more detail on angled surfaces with low PHI values than with high PHI values.