[ / main / writing / vectors ]

    3D Vectors

1997 Jeff Weeks and Codex software


Vectors are used extensively throughout 3d computer graphics, particularly in shading and visual surface determination. Because of its many uses I will present a short tutorial on what they are and how to use them.

    What is a Vector?

Simply put, a vector is a triple which posseses both magnitude and direction. It has an x, y and z component, and so is very much like a vertex. Because a vector posseses magnitude and direction it makes sence to calculate it from two vertices as follows:

    V = (x2 - x1, y2 - y1, z2 - z1)

The sign of each component will tell the direction of that component, and, ofcourse, the numerical part (the coefficient) of each component will tell its magnitude. The length of a vector can also be calculated very easily using the standard length of a line segment equation, extended to 3D:

    |V| = (V12 + V22 + V32)1/2

Now that we can find the length of a vector, I'll explore another action typically taken with vectors; Normalizing. To normalize a vector we make the vector length equal to one. This can be done by dividing each component by the length of the vector:

    U = ---

Adding vectors is just as simple as it sounds. Each vector component is added to the coresponding vector component of the other vector:

    X = V + W
      = (X1, X2, X3)
      = (V1 + W1, V2 + W2, V3 + W3)

What may not be outwardly obvious, however, is the geometry of the resultant vector. A simple way of interpreting X (in the above) is to place the head of vector V at the tail of vector W. Vector X will then be the vector from the tail of vector V to the head of vector W.

Next I'll talk about the cross product of two vectors, which is defined as:

    X = V x W
      = (X1, X2, X3)
      = (V2W3 - V3W2, V3W1 - V1W3, V1W2 - V2W1)

This cross product can be used to find the normal of a polygon in 3D. The normal of any surface is a vector perpendicular to the surface. The normal can be calculated by taking the cross product of two vectors (V1 and V2), which are defined from three (non-colinear) verticies of the polygon.

Because the normal is exactly perpendicular to the polygon, it can be used in such processes as light source shading and backface culling (discussed in other tutorials). However, certain processes (such as light source shading) require that the polygon normal be a unit vector, in which case you must normalize the polygon normal after the cross product calculation.

A typical approach, however, is to precalculate the polygon normal, normalize it, and be done with it. No more calculations are needed. The vector can be transformed like any other vertex as well; Simply multiply the transformation matrix to the vector. The code, however, looks a bit different than the vertex * matrix code because we don't add the translation values to our vector. That just makes sence.

    Vector Vector_Matrix_Multiply(Matrix mat, Vector vect) {
      Vector v;
      v.x = vect.x * mat.matrix[0][0] +
            vect.y * mat.matrix[0][1] +
            vect.z * mat.matrix[0][2];
      v.y = vect.x * mat.matrix[1][0] +
            vect.y * mat.matrix[1][1] +
            vect.z * mat.matrix[1][2];
      v.z = vect.x * mat.matrix[2][0] +
            vect.y * mat.matrix[2][1] +
            vect.z * mat.matrix[2][2];
      return v;

Lastly, I will introduce the dot product. This calculation is commonly used to find the measure of the angle between two vectors. Such a calculation requires that the vectors be unit vectors, however, or else the result will be inaccurate. For certain purposes, such as backface culling, the actual dot product is ignored and only it's sign is used. The reason for this is because the sign of the dot product has the following significance:

    If V . W > 0 then Ø < 900
    If V . W = 0 then Ø = 900
    If V . W < 0 then Ø > 900

Therefore, the sign of the dot product can tell you which side of the view vector the polygon resides in. The dot product of two vectors is defined as:

    X = V . W
      = V1W1 + V2W2 + V3W3