Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced_Renderman_Book[torrents.ru]

.pdf
Скачиваний:
1714
Добавлен:
30.05.2015
Размер:
38.84 Mб
Скачать

Trigonometry and Vector Algebra

Figure 2.1 A simple right triangle and another triangle with three unremarkable angles.

the length of the opposite side to the length of the adjacent side. Three easy to remember equations:

Of course, if we know the lengths of the sides, or even just the ratio of the lengths of any two of the sides, we can compute the angle by using one of the inverse functions, arcsine, arccosine, or arctangent, which are generally abbreviated asin, acos, and atan.

Quickly glancing at the graphs of the sin and cos functions (Figure 2.2) tells us some. interesting things about them, sin and cos have the same graph, just offset by 90 degrees. They are periodic, repeating every 360 degrees, and they always stay within the range of -1.0 to 1.0. tan is also periodic, repeating every 180 degrees, but it runs from -oo to co on each cycle.

Interestingly, trigonometricians note that for small angles, less than about 15 degrees, the values of radians(B), tan(9), and sin(B) are all nearly equal (to within a couple percent). For this reason, they sometimes refer to the small angle approximation, where the tangent and sine can be approximated by the angle itself.

Sometimes, we have the length of two sides of a triangle and need the third, but the triangle isn't a right triangle so we can't apply the Pythagorean theorem. Never fear, there are two useful trigonometric formulas to our rescue (but we do need to know at least one of the angles, too). As illustrated on the right side of Figure 2.1, we'll use the notation that the angle opposite a side a isIf we know the length of two sides, a and b, and the angle between those sides, we can compute the law of cosines:

You'll recognize this as a generalization of the Pythagorean theorem, since cos (90') is 0.0. Alternatively, if we know the length of a and b and one of the opposing

20

2 Review of Mathematics and Computer Graphics Concepts

Figure 2.2 Graphs of the three standard trigonometric functions.

angles, our tool is the law of lines:

Given this, and the fact that the angles of every triangle add up to 180°, we can derive both of the other angles, and hence the length of the third side.

2.1.2Vectors 101

In computer graphics, we use vectors to represent many important concepts: positions, directions, tangents, surface normals, colors, and so on. Therefore, a working knowledge of vector algebra, the manipulation of vectors, is quite important.

A vector is an array of numbers. When we want to emphasize the fact that a single individual number is used in a particular calculation, we use the term scalar. Positions in 3D are represented by vectors known as points. Points have three coordinates, which refer to the distances from the origin to that location in each of the three dimensions (by convention known as x, y, and z). Directions in 3D are represented by direction vectors. Direction vectors are often loosely just called vectors, which inevitably leads to certain confusion. Fortunately, it is usually obvious from context whether vector means general mathematical vectors or direction vectors specifically.

Direction vectors also have three coordinates, which refer to the distance they represent in each of the three dimensions. Notice that vectors have both "direction" and "length," where the length of a vector is computed as the square root of the

Trigonometry and Vector Algebra

Figure 2.3 Scaling, adding, and subtracting direction vectors.

sum of the squares of the coordinates (there's that Pythagorean theorem again) and is denoted by writing the vector between vertical bars.

There are an infinite number of vectors that point in the same direction but have different lengths. In situations where only the direction matters, any of those vectors could be used, but it is common to refer to the unique vector that has the right direction and has length equal to 1.0. Such a vector is said to be normalized. This is often noted by printing the name of the vector with a bar above it, such as N. Note that by convention, direction vectors that have negative length are considered to be pointing in the opposite direction.

Certain mathematical operations are considered to be "legal" on points and direction vectors. For example, it is legal to multiply a vector by a scalar. To do so, you simply multiply each vector coordinate by that number. The result of this is to multiply the length of the vector by that same number, without changing the direction, and so is said to scale the vector. Negating a vector makes it point in the opposite direction.

Vectors can be added together by adding the corresponding coordinates. We can visualize this as chaining the vectors together, the end of one attached to the start of the next. The resulting vector is identical to the vector that runs from the start of the first to the end of the last (see Figure 2.3). Subtracting vectors is also legal; it is just like adding the negated vector. A vector can also be added to a point, again by adding the matching coordinates, which creates a new point that is displaced from the first by the distance and direction of the vector.

It is not generally considered "legal" to add two points, because there is no geometrically logical result. Interestingly, it is possible to subtract two points, which creates a vector that is the distance between the points. Similarly, points cannot be

22 2 Review of Mathematics and Computer Graphics Concepts

scaled, as vectors can; however, it is acceptable to take the weighted average of a set of points, such as computing the midpoint or the centroid.

There are two other operations on vectors that are useful and that correspond to different types of multiplication. The first is dot product. This is computed by adding together the products of the corresponding components. The resulting value is a scalar that equals the cosine of the angle between the vectors times the length of each of the vectors.

If used on normalized vectors, of course, the lengths of the vectors are both 1.0, so the result is just the bare cosine.

This function is really useful because it converts directions into relative angles. Notice that cos(90') is 0.0. This means that dot product can be used to quickly test whether two vectors are perpendicular to each other, regardless of their magnitude (that is, as long as the vectors do not have 0 length to start with). Moreover, because cos(6) is positive for angles between -90 and 90 degrees, and negative for angles outside that range, the dot product can be used to quickly determine if two vectors point roughly in the same direction or roughly oppositely. If the dot product is positive, the angle between them is less than 90 degrees, and they can be said to point roughly in the same direction. If the dot product is negative, the angle between them is more than 90 degrees, and they can be said to point roughly in opposite directions.

The other version of vector multiplication is the cross product. The equation for the cross product is somewhat complicated, but the result is very useful. The cross product creates a new vector that is perpendicular to both of the original vectors. The length of the new vector is the product of the sine of the angle between the vectors and the lengths of the vectors.

Usually, we don't care about the length so much as the direction-creating a perpendicular vector is very useful in computer graphics for such things as rotation axes and surface normals.

Notice on the graphs in Figure 2.2 that sin(0') = sin(180') = 0.0, so the cross product can be used to quickly identify whether two vectors are parallel, regardless of length.

2.1.3Coordinate System Transformations

At the beginning, we noted that positions and direction vectors measure distances and directions from the "origin" but never mentioned where the origin was. Who legislated that the origin of the universe ought to be east of San Francisco and not north of Los Angeles? No one, in fact. To misquote Einstein, "Everything is relative."

2.1

Trigonometry and Vector Algebra

23

 

In computer graphics, we often use a plethora of 3D coordinate systems, each of which

 

has its own origin and three primary direction axes. A point that is (1, 2, 3) in one coordinate

 

system could easily be (14, -97.2, 3.94) in a different coordinate system. They are the same

 

point, but the values of the coordinates are different because they are each specified relative

 

to the origin of a different coordinate system. The relationship between one coordinate

 

system and another, and therefore the mathematical formula for converting one

 

representation of a point or vector into the other representation, is embodied in a coordinate

 

transformation. Because it is easy to convert from one coordinate system to another, it is

 

common to have a number of utility coordinate systems that are relative to interesting

 

features in the 3D environment, such as relative to the camera position, to each light source,

 

to key objects in the scene, and so on.

 

 

There are many useful and important coordinate transformations that fall into two

 

categories: those that map points, vectors, and parallel lines into unique equivalent versions

 

in the other space, called affine mappings; and those that compress a dimension out of the

 

space, called projections.' Affine mappings include the familiar transformations of

 

translation, scaling, and rotation. Projective mappings include perspective and orthographic

 

projection, as well as any other mapping that takes 3D coordinates and turns them into 2D

 

coordinates (such as a spherical projection).

 

 

Translations are simple offsets of one coordinate system from another. Translating a

 

point from one coordinate system to the other is done by adding a particular direction vector

 

to the point. Translation leaves the old and new axes parallel to each other and doesn't

 

change the size of anything. Scaling changes the size of the distance units of a coordinate

 

system without moving the origin. Scaling is most often done uniformly, scaling all

 

dimensions equally. If the dimensions are not scaled the same, it is called nonuniform

 

scaling. This transformation converts a cube into a rectangular box. Rotation spins the

 

coordinate system without moving the origin or changing size. Rotations can be specified in

 

several different equivalent ways, but RenderMan specifies rotation by an angle around an

 

arbitrary axis, not restricting rotations to be around one of the primary axes.

 

2.1.4 Matrices 101

Matrices are rectanglar arrays of n x m numbers (called elements), arranged as n rows with m elements in each row (or, equivalently, m columns with n elements in each column). In 3D computer graphics, it is very common to use 4-row, 4-column (4 x 4) matrices for a variety of purposes. A 4 x 4 matrix therefore has 16 values, arranged in a square. There are only a small number of mathematical operations

In the wider world of mathematics, other transformations exist that don't do either of these things, but we don't use them too often in computer graphics.

24

2 Review of Mathematics and Computer Graphics Concepts

that make sense on matrices and are useful to us, but these turn out to be very important. First, let's identify one very important special matrix, known as the identity matrix. The

elements on the diagonal of the identity matrix are all 1.0, and all of the other elements are 0.0. For example, the 4 x 4 identity matrix is

Now to work. A vector (that is, any mathematical vector type) can be multiplied by a matrix, V x M. The result is a vector, which is calculated by computing the dot products of the vector with each column of the matrix. Notice that the dot product computation requires two vectors that are the same length. This means that the number of elements in the original vector must equal the number of rows in the matrix. The number of elements in the result vector is equal to the number of columns in the matrix.

Conversely, you can multiply a matrix by a vector, M x V, resulting in a vector that is calculated by computing the dot products of the vector with each row of the matrix. In that case, the number of elements in the original vector must equal the number of columns in the matrix, and the number of elements in the result is equal to the number of rows.

The first type of vector-matrix multiply, V x M, is called premultiplying the vector by the matrix, because the vector appears before the matrix. By convention, vectors that premultiply matrices are written horizontally, as rows, and are therefore known as row vectors. The second type of multiply, M x V, is called postmultiplying the vector by the matrix, because the vector appears after the matrix. By convention, vectors that postmultiply matrices are written vertically, as columns, and are therefore known as column vectors.

Two matrices can be combined in an operation we call matrix multiplication,

The actual process of multiplying the two matrices together is somewhat complicated. It is not simply multiplying the corresponding elements together, but is actually computing

every possible dot product of rows of with columns of .The resulting "product" matrix is the combination of the two matrices. Using it (such as in vector-matrix, multiplication) is equivalent to sequentially using the first matrix, then the second. That is,

Multiplying a matrix by the identity matrix leaves the matrix unchanged. Similarly for a vector. This is the reason that the identity matrix is called the identity. It is important to note that in matrix multiplication, the order of the matrices matters. Reversing the order of the matrices changes the result. That is,

Another useful operation is taking the inverse of opposite of the original matrix and is often denoted

a matrix. The inverse matrix does the M-1. This is because

2.1 Trigonometry and Vector Algebra

25

multiplying a matrix by its inverse always results in the identity matrix, just as multiplying a number by its multiplicative inverse (the reciprocal) always yields 1.0.

There are, of course, many other useful operations on matrices, such as computing the transpose or the determinant of a matrix. We don't need these to discuss the computer graphics topics that we want to cover in this book, so we'll skip over them. Consult your friendly neighborhood mathematics textbook for details.

2.1.5Vectors 201

One of the reasons that vectors and matrices are important in discussions of computer graphics is that quite a few years ago, some of the early researchers in computer graphics demonstrated that a 4 x 4 matrix was a very convenient way to represent a 3D transformation. For example, there is a standard way to represent scaling, translation, and rotation as a 4 x 4 matrix. We call this a transformation matrix. At first, it may seem wasteful to represent a simple translation, which requires only 3 values, with a 16-element matrix, but there is a mathematical elegance that makes it worthwhile. For example, the transformation matrix that represents any combination of transformations is identical to the result of multiplying the simple transformation matrices together.

Similarly, transforming a point or a vector through any of these transformations, or any combination of these transformations, is easily computed by merely multiplying the vector by the transformation matrix. It turns out that all of the other interesting 3D transformations, such as shears and even perspective, can also be represented by 4 x 4 matrices. This means that any series of standard computer graphics transformations, no matter what order and no matter how complex, can be represented as a single matrix. Thus, it is now commonplace for graphics systems to use matrices to represent all of their transformations, because far from being wasteful, it is the most compact and elegant method of storing that data.

Recall from the previous discussion that vector by matrix multiplication imposes certain restrictions on the dimensions of the data, and, in particular, it seems illegal to multiply points and direction vectors (which have three elements) by a 4 x 4 matrix. However, there is an explanation. When transforming a point by a transformation matrix, the required "fourth element" is assumed to be 1.0. When transforming a direction vector by a transformation matrix, the required fourth element is assumed to be 0.0.

When points and vectors are written in this 4D notation, they are in homogeneous coordinates. Similarly, the 4 x 4 arrays that operate on them are known as homogeneous transformation matrices (to distinguish them from other matrices that might be 4 x 4 for other reasons).

26

2 Review of Mathematics and Computer Graphics Concepts

Figure 2.4 Sawtooth and other periodic functions can be created by modulo arithmetic.

The intrepid reader might notice that this also explains why adding and subtracting vectors is legal, adding vectors to points is legal, and subtracting two points yields a vector, while adding points is illegal. But we won't dwell on those pieces of trivia.

When using vectors to represent points and direction vectors, and matrices to represent transformations, the values you place in the matrix depend on whether you are going to premultiply row vectors or postmultiply column vectors. The results are equivalent either way, but it is important to be consistent about your notation. Unfortunately, some of the popular computer graphics textbooks use one notation, and others use the other. In RenderMan texts and documentation, we always premultiply row vectors by transformation matrices.

2.1.6Periodic Functions

Some mathematical functions are naturally periodic, meaning that their values repeat at

regular intervals. More specifically,

for all integers n..Two

examples are sine and cosine:

for all integers n..

Not all functions are periodic, of course, but sometimes it is very handy to create periodic versions of a segment of a function. For example, consider the sawtooth wave in Figure 2.4. Periodically repeating functions such as these are easy to create

2.2 Geometry

27

 

using the modulo function. Because modulo is itself a simple periodic function (in fact, it is the sawtooth function), using it to create the argument to any other function creates a periodic version of that function. For example, f (mod (x, period)) repeats the section of f from

0.0 through period.

2.2Geometry

The geometric primitives that RenderMan handles are 2D surfaces. They are thin sheets, like pieces of paper, or perhaps more like sheets of rubber, that are deformed and transformed into the 3D coordinate system that we intrinsically think of as being our modeling space. RenderMan does not have any 1D drawing or annotation primitives, nor a 2D drawing surface on which to place them. Therefore, we will not spend too much time describing 1D or 4D surfaces, which, while interesting in themselves, have nothing to do with the task at hand.

2.2.1 Primitive Equations

The curves and surfaces that are used in computer graphics are all derived from various types of mathematical equations. Plug values into the variables of the equations, and they identify which points are on the object, and all the rest are not. There are three types of equations that provide the basis for (most) computer graphics geometric primitives: explicit, implicit, and parametric.

An explicit equation is one that evaluates one coordinate of the position of the object from the values of the other coordinates. For example, z = 2x + y is the explicit equation for a plane. One of the features of the explicit equation is that it is mathematically a function, meaning that it only has one result value for each set of input coordinates. This means that it cannot layer over itself like, for example, a circle does.

An implicit equation is one in which certain values of input coordinates satisfy an equation: surface(x, y, z) = 0. Points that satisfy the equation are "on" the primitive, while others that do not are "not on" the primitive. For example,

is the implicit equation for a sphere. The point (0.6,0,0.8) is on the sphere because it satisfies the equation, whereas the point (0.4,0.8,0.2) is not because it does not. The points that are generated by complex implicit equations are not always connectedthere can be isolated points or small isolated regions that satisfy the equation. This makes it tricky to find them all.

A parametric surface is a surface generated by an equation with two variables: P = surface (u, v). For example, a sphere can be generated by the parametric equations

28

2 Review of Mathematics and Computer Graphics Concepts

Other valuable parametric surfaces include planes, all of the quadric surfaces, bilinear and bicubic patches, ruled surfaces, and NURBS.

Generally, we are not interested in infinite parametric surfaces, but rather that subset of the surface that is generated for values of the parameters within a certain range, typically between 0.0 and 1.0. Notice that it is simple to reparameterize equations with other parametric ranges by a simple replacing of variables, so this choice doesn't actually cause any restrictions. For example, rather than have the sphere in the preceding equations parameterized forbetween 0 and and between and ,we can change to equations of this form:

for values of u and v between 0.0 and 1.0. Some surfaces, such as spheres, can be defined implicitly as well as parametrically, but most parameteric equations cannot be easily implicitized.

Parametric surfaces have two interesting properties that make them useful in computer graphics. First, because the parametric equation can be evaluated directly, you can compute any point that is on the object by plugging in values for the parameters. It is easy to generate a few points that are on the surface and then approximate the rest of the surface by linear approximation. We call this technique tesselation. Second, because we have an equation that converts a 2D pair of parametric coordinates into 3D, the surface has a natural 2D coordinate system. This makes it easy to map other 2D data onto the surface, the most obvious example being texture maps.

2.2.2Tangents and Normals

Having mathematical equations for our geometric primitives allows us to use the machinery of analytic geometry and calculus to compute important properties of the equations, which in turn are important properties of the geometric primitives. Two of the most important of these are tangents and normals.

The tangent at a point on an object is a flat object that just touches the object at that point but doesn't cross or penetrate the object (at least, not in the immediate neighborhood of the point). It just skims the surface at that point. In two dimensions, the tangent to a curve is a line and is unique. The slope of the tangent line is considered to be the slope of the curve at that point. Generally, we are not interested in the tangent line per se, but in a direction vector that has the same slope. We call this the tangent vector. The value of the slope is computed by evaluating the derivative of the curve at that point, generally a very simple operation if we have an explicit, implicit, or parametric equation for the curve.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]