3D Vectors

Published July 05, 2000 by Jeff Weeks, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement
[size="5"]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.


[size="5"]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:

[font="Courier New"][color="#000080"] V = (x[sub]2[/sub] - x[sub]1[/sub], y[sub]2[/sub] - y[sub]1[/sub], z[sub]2[/sub] - z[sub]1[/sub]) [/color][/font]

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:

[font="Courier New"][color="#000080"] |V| = (V[sub]1[/sub][sup]2[/sup] + V[sub]2[/sub][sup]2[/sup] + V[sub]3[/sub][sup]2[/sup])[sup]1/2[/sup] [/color][/font]

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:

[font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp]V
U = ---
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][/color][/font][font="Courier New"][color="#000080"]|V|[/color][/font]

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

[font="Courier New"][color="#000080"] X = V + W
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"]= (X[sub]1[/sub], X[sub]2[/sub], X[sub]3[/sub])
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"]= (V[sub]1[/sub] + W[sub]1[/sub], V[sub]2[/sub] + W[sub]2[/sub], V[sub]3[/sub] + W[sub]3[/sub]) [/color][/font]


vector_add.gif


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:

[font="Courier New"][color="#000080"] X = V x W
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"]= (X[sub]1[/sub], X[sub]2[/sub], X[sub]3[/sub])
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"]= (V[sub]2[/sub]W[sub]3[/sub] - V[sub]3[/sub]W[sub]2[/sub], V[sub]3[/sub]W[sub]1[/sub] - V[sub]1[/sub]W[sub]3[/sub], V[sub]1[/sub]W[sub]2[/sub] - V[sub]2[/sub]W[sub]1[/sub]) [/color][/font]


cross_product.gif


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 (V[sub]1[/sub] and V[sub]2[/sub]), 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:

[font="Courier New"][color="#000080"] If V [sup].[/sup] W > 0 then [s]0[/s] < 90[sup]0[/sup]
If V [sup].[/sup] W = 0 then [s]0[/s] = 90[sup]0[/sup]
If V [sup].[/sup] W < 0 then [s]0[/s] > 90[sup]0[/sup] [/color][/font]

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:

[font="Courier New"][color="#000080"] X = V [sup].[/sup] W
[/color][/font][font="Courier New"][color="#000080"][nbsp][/color][/font][font="Courier New"][color="#000080"][nbsp]= V[sub]1[/sub]W[sub]1[/sub] + V[sub]2[/sub]W[sub]2[/sub] + V[sub]3[/sub]W[sub]3[/sub] [/color][/font]
Cancel Save
1 Likes -1 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement