# 3D Vectors

vector matrix mat vect product vectors polygon normal component

**Introduction**

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 = (x_{2} - x_{1}, y_{2} - y_{1}, z_{2} - z_{1})

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:

|V| = (V_{1}^{2} + V_{2}^{2} + V_{3}^{2})^{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:

V

U = ---

|V|

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

= (X_{1}, X_{2}, X_{3})

= (V_{1} + W_{1}, V_{2} + W_{2}, V_{3} + W_{3})

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

X = V x W

= (X_{1}, X_{2}, X_{3})

= (V_{2}W_{3} - V_{3}W_{2}, V_{3}W_{1} - V_{1}W_{3}, V_{1}W_{2} - V_{2}W_{1})

_{1}and V

_{2}), which are defined from three (non-colinear) vertices 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 sense.

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 ~~0~~ < 90^{0}

If V ^{.} W = 0 then ~~0~~ = 90^{0}

If V ^{.} W < 0 then ~~0~~ > 90^{0}

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

= V_{1}W_{1} + V_{2}W_{2} + V_{3}W_{3}

## 0 Comments

Note: GameDev.net moderates article comments.