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:
The sign of each component will tell the direction of that component, and, of course, 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:
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:
Adding vectors is just as simple as it sounds. Each vector component is added to the coresponding vector component of the other vector:
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:
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.
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:
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: