## Building Virtual Worlds

#### Lecture 2: Introducing Computer Graphics: The Virtual Camera

Orienting the Observer
This lecture is based on Chapters 3.0, 3.1 and 3.2 of Vince.

Defining a Right Handed Coordinate System:

X right (almost all systems do this)
Y Up (some use Y down, like a Windows screen)
Z Out (goes with Y up and Right Hand Rule)

The Synthetic Camera is an imaginary device inside which an image must be formed, on its own screen coordinate system. To do that, the objects must be located in the camera coordinate system. So we have to construct rules which take world 3D coordinates (x,y,z) into Camera (or Virtual Observer ) coordinate system (xv, yv, zv), and then into screen coords (xs, ys).

The VO is located in space at some point named VO=(xv,yv,zv). That's easy.

It is oriented in some way involving three degrees of freedom. That's hard. We discuss 3 ways of doing it.

Direction Cosines. If the world coordinate system has three orthogonal unit vectors I, J, K as its bases, and the transformed system has bases L, M and N, the idea of direction cosines is to express the base vector L in terms of I, J and K. (And likewise, M and N.) For instance,

If  L is parallel to I and perpendicular to J and K, then L's direction cosines are (1,0,0) in the IJK system.
If L is antiparallel to J, then L's direction cosines are (0,-1,0) in the IJK system.

The cases with superimposed axes, on page 27, are simple to understand. If things are rotated so that the axes don't align, then we gotta do some math. Here's a review:

REVIEW: Cosine. Cosine of an angle is the measure of their parallel-ness. Cos(0)=1, meaning if the angle between 2 vectors is 0, Cosine is maximized. Cos(pi/2)=cos(90 degrees)=0, meaning if the angle between two vectors is perpendicular, cosine is zero. Cos (pi)=cos(180 degrees)=-1. Your calculator, or a lookup table, will provide values of Cosine. The commonly needed values are these:

Cos(pi/4)= (sqrt 2)/2. Cos (pi/3) = cos(60 degrees) = 1/2. Cos(0)=0. Cos(pi)=-1 Cos(pi/2)=1.

Unit vectors are just vectors of length 1, such as (1,0,0) or (0,0,1) or ((sqrt 2)/2, (sqrt 2)/2, 0). This last vector ofcourse falls exactly between the x and y axes, and in the z=zero plane.

QUERY 2.1: Figure out the direction cosines (the three components) of a unit vector which falls exactly between the x,y and z axes. Hint: Its three components will be identical.

The text begins with a confusing statement on page 26: "These direction cosines enable any point P(x,y,z) in one frame of reference to be transformed into P' (x',y',z') in another frame of reference." This is literally true, but "enable" does not mean "provide all the necessary information." You need both the relative ROTATION of the two FORs, and the relative TRANSLATION of them.

Homogenous Coordinates are introduced on page 28. This means using a 4 x 4 matrix operation to represent three dimensional geometric transformations - a neat trick. The fourth column provides an additivie component, when we augment our 3d vectors with a fourth element of value 1.

REVIEW: Length of a vector. A vector V= (a,b,c) has length |V| = sqrt( a**2 + b**2 + c**2). Pythagorean Theorem.

REVIEW: Dot Product of vectors. Two vectors V=(a,b,c,d) and W=(e,f,g,h) can be multiplied  together by forming the sum (ae+bf+cg+dh). Input: 2 vectors; output: one scalar number. This "dot product" also computes the value of |V||W|cos Theta, where Theta is the angle between the 2 vectors. A miracle, no doubt....

REVIEW: Matrix Multiplication. Two matrices placed side by side can be multiplied, if the left one has the same number of columns, as the right one has rows. For instance, a 4x4 and a 4 x 1 can be multiplied (we always tell the number of rows, first.) The 4 x 1 is called a column vector.

To multiply a 4 x 4 and a 4 x 1, take the dot product of the first row of the 4x4 with the 4x1 column vector. This is the first (top) scalar in the resulting 4 x 1 column vector. In other words

`a b c d      u`
`e f g h      v`
`i j k l      w`
`m n p q      x`
Multiplying this matrix by this column vector yields a new column vector whose first term is
`    au + bv + cw + dx`
and whose other terms are
`    eu + fv + gw + hx`
`    iu + jv + kw + lx`
`    mu + nv + pw + qx`
Got that?

Query 2.2: Construct the transformation matrix which describes a VO located at 10,0,10, with its Yvo parallel to the Yw axis and its Zvo pointing straight at the origin. Verify your matrix's correctness by applying it to the world coordinate points (0,0,0) and (10,0,0). Draw pictures as necessary to convince yourself (and me) that the world coordinate points have the right coordinate values in the VO coordinate system.

XYZ Fixed Angles. Note that the terms "roll", "pitch" and "yaw", imported from aviation, relate to positive rotation around the VO's z, x and y axes respectively. You have to be careful to think in aircraft terms rather than coordinate axis terms, as other people might not line z up with the plane's long axis, but role is always thus defined. Likewise pitch always means "nose up" and yaw means "nose left".

Matrices for roll, pitch & yaw are provided in the text on page 30.

Matrix multiplication is non-commutative, so you gotta have an "official" order for these operations. We just named it: roll, pitch & yaw, because these are the standard most-to-least common aviation operations.

Combining the matrices in this order yields the monstrous equation 3.14 with its sixteen terms explained on page 31. Note that this matrix works if all 3 angles are specified in world coordinates. But this is seldom the way a pilot or a modeler thinks.

Euler Angles are similar but you always figure the angle based on the current object's coordinate system, moving forward one step at a time. The objective in any case is to get from a given pose, to some other pose. There are usually more than one way to describe the steps involved, and all you want is to get ONE matrix that reliably does the job.

Query 2.3. A whale is swimming north in normal whale position (belly down.) Construct a transformation matrix which would reposition this whale so that it is now swimming West and belly-up. Test the matrix on a very small whale "data set".

HINT: The small whale data set can consist of the points (0,0,0) for its tail, (0,0,-10) for its nose, and (-2,0,-5) for the tip of its left fin ("front paw"). The coordinates are in meters.

Query 2.4. The whale we described happened to be located with its tail at the origin when we did our flip-around. What if the whale had had its tail at 10 kilometers north of the origin? Correct your test data and see what the matrix does in this case.

In the following two queries, "pseudo-code" means that you can code the algorithms in English, C, Java, Pascal or anything you want as long as the loops, loop counters and computed values are explicitly named as variables. None of that "then multiply the value by the cosine"... vagueness.

Query 2.5. Write a pseudo-code procedure which has three input values (a,b,c) which represent Euler angles for roll, pitch and yaw. The procedure yields a 3 x 3 matrix M[0..2, 0..2] where the first coordinate denotes the row, which performs the stated transformation. You may find it convenient in your code to call the procedure you're about to write in the next Query.

Query 2.6. Write a pseudo-code procedure which accepts two 3 x 3 matrices (M, N) and produces their matrix product P.

Quaternions. The intellectual's rotational system.

Flipping whales or submarines is easy if the operations relate to easily located axes like north-south or east-west. But what if you have to rotate something around an arbitrary vector? There are also problems with euler angles called "singularities". What if you need to compute the set of euler angles to get an object from one orientation to another, (working backwards, as it were from "beginning and ending positions" and figuring out the Euler angles to map you from one to the other.) There are cases in which the math thereof leads to divide-by-zero. This corresponds to the geometric case where there are two equally plausible sequences of steps, and the math can't decide which one to take.

We won't spend too much energy on Quaternions; but we will learn how to build one and apply it to a rotation problem. Then we'll apply it in the final query, as follows:

Query 2.7: Consider again the unit vector we developed in Query 2.1. Develop a quaternion which represents a 120 degree rotation around this vector, clockwise looking outward from the origin. Verify its operation by applying it to the point (1,0,0). You can "template" your computations off of those on page 42 and 43.

Back to the course index
Back to the course syllabus
Back to the previous lecture
Onward to the next lecture