3D Basics and Vector Operations

Table of Contents:
Objects in 3Space
  • Vector Length
  • Vector Addition
  • Vector Subtraction
  • Scalar Multiplication
  • Dot Products
  • Cross Products

    Objects in 3Space

    Okay, now that we are all excited about making a 3D graphics engine, where do we start? Well, the first thing that we need is to be able to position our objects in the virtual world. If we don't know where things are, we will not be able to draw them. We will use the Cartesian coordinate system to describe positions in space.

    The Cartesian coordinate system is your standard X,Y,Z system (Shown in the image to the Right). It consists of three axises (shown in green), all perpendicular to each other. To specify a point in three space, you must specify three coordinates, the X,Y and Z coordinates. The Origin (O) of the coordinate system is special, it has coordinates (0,0,0). Each of the coordinates measure a distance parrellel to their respective axis from the origin to the point.

    The yellow tick marks on the diagram show a general idea, these are measured axises. You can measure along each axis a specified number of units to get to where you are going. Because each of the axis is perpendicular to each other, you can specify each 3D point with one unique coordinate triple. Each point has one and only one coordinate.

    To the right is one example, the red point. As you can see, it is located at point (3,3,0). This means that is is three units along the X axis, three units along the Y axis, and zero units along the Z axis. The orange arrow shows an alternative notation that is will be useful soon. It is vector notation. Instead of thinking of the point as a point, we think of it as a position vector. In practice, both are stored as (X,Y,Z) coordinates, but using vector ideas will make future ideas easier to understand.


    Vectors are a very nice mathematical idea. Understanding them is key to understanding 3D graphics. A vector is really a simple thing. It consists of a head, denoted with the arrow and a tail. A vector can be moved around in three space without changing it's properties. Because of this, we only need to specify where the head of the vector is. The tail then, we can assume is at the origin.

    Before we dive into vector manipulations, we need some notation to deal with them. Here is how we will define a vector:

      V = (x, y, z)

    This shows that each vector has three components, seperated by commas. Notice that there is a difference between vector and scalar numbers. A scalar number is just a number, such as 1 or 5 or 10.2. A vector is a three dimensional object. To describe a vector requires the three scalar values: X, Y, and Z.

    The main purpose of this article is to familiarize you with vectors and manipulations applicable to them. In the next article, we will start off with this foundational knowledge and start coding. Before we can really understand what is going on though, we must know the basics. Here are the main manipulations that we will want to do to vectors:

    1. Find Length
    2. Add Vectors
    3. Subtract Vectors
    4. Scalar Multiplication
    5. Dot Product
    6. Cross Product

    Vector Length:

    To find the length of this vector, we will use the Pythagorean Theorum. The P. Theorum gives you a way to determine the length of a line, when you know the lengths of the two (or three in 3D) perpendicular lines. The Perpendicular lines in our case happen to be the X and Y (and Z) coordinates of the vector. In two dimensions, the P. Theorum states the following:

    From here, three dimensions is an easy extension, just add an extra term:

    The length of a vector is annotated by putting the vector (V) inside of absolute values signs. Also, instead of using a, b, and c, we use X, Y, and Z:

    The length of a position vector is simply equal to the distance from the origin to the point. Note that the value that this formula is a simple scalar number. It is not a vector quantity.

    Adding Vectors:

    Adding vectors is really very intuitive. I will give an example in 2 space, because it is easier to draw. Say you have the following two vectors (Operation shown to the right):

      A = (1, 2)
      B = (4, 0)

    To add two vectors, you simply add each of the components of the vectors together. To add it graphically, you put the tail of one vector at the head of the other (The green vector is the result). Just like scalar addition, the order that you add the vectors does not matter. The result of A + B is:

      A + B = (1+4, 2+0) = (5, 2)

    Subtracting Vectors:

    Subtracting vectors is very much the same as adding them, except that you do it in the opposite order. Like scalar subtraction, if you subtract in the wrong order you get a negative answer (Negative vectors are really just vectors pointing 180 degrees from where they should be - They retain their correct length). To subtract vectors, you subtract components. To subtract them graphically, you put the vectors head to head with one tail at the origin. Subtracting vectors is much the same as adding a negative vector... like this:

      A - B = A + (-B)

    Negating a vector is a simple as rotating it 180 degrees. If you subracted A - B (Shown on the top to the right) you would get:

      A - B = (1-4, 2-0) = (-3, 2)

    Scalar Multiplication:

    There are three ways that you can multiply a vector. With the scalar multiplication method, you are multiplying a vector times a scalar value. This effectually increases the length by that amount, but does not alter the direction. Scalar multiplications are really easy to do and to understand:

      A*3 = (3*1, 3*2) = (3, 6)

    Scalar multiplications are very useful too. Some operations require a unit vector... a vector that is one unit long. To change an arbitrary vector into a unit vector, you divide the vector by it's length... Like this:

      Vunit = ---

    Okay, this is good and all, but let's get into some more interesting operations now...

    Dot Product:

    The dot product (which is very hard to draw :) is a very important operation in 3D graphics. The mathematical definition is shown below:

    As you can see, the operation is denoted with the * operator. It takes two vectors as input and returns a scalar value as output. What is it used for? Well, one very important thing is that we can find the angle between two vectors with it:

    Finding the angle between two vectors will be important when we need to do backface culling, and when we need to light our surfaces. In these cases, both of the vectors will be unit vectors, so the division drops out (|U||V| = 1). This makes it a very fast test.

    Also with the dot product, you can find the length of a projection of one vector on another. The picture to the right shows projAB. This stands for "The projection of A onto B". A projection can be thought of as the shadow cast onto B by A. The math behind this is:

    Okay, from this you can derive the entire vector. To get the vector, you simply multiply this by the unit vector in the direction of V. The projection would be this then:

    Projections can be used to calculate things such as shadows, and other goods stuff. Mainly though, we will be using the dot product to find the angle between vectors.

    Cross Product:

    Finally, the last major piece of foundation before we can start coding. The Cross product is undeniably the hardest one of these identities to calculate, but it can also be the most useful. The cross product's operator is the "x" operation. Here is the definition:

    As you can see, this is a very ugly equation. Where did it come from? Well if you are a matrices kind of person, it was calculated by taking the determinate of the matrix to the right. (With the i, j, and k seperating the individual components of the result). If you aren't a matrix kind of person, don't worry about it... we will just use the equation.

    This formula is VERY useful for calculating the normal vector to two other vectors. "Normal" in this sense means perpendicular. If you plug two vectors into this equation, you get back the vector that is 90 degrees to both of the original two vectors. Something that you might find useful to know is the the length of the cross product is equal to the area of the parrellelagram formed by the two original vectors. Although we won't use this part of the identity. It could be useful for finding out the area of a polygon.

    One very important use for the cross product will to find the normal vector of a plane. The normal vector to a plane is one that sticks straight out of it. We will find this by taking two vectors that run along the plane, and computing the cross product of them. This normal vector will be quite useful for things like shading the surface and determining visibility.


    Today we covered a lot of theoretical stuff that will be very important when we start coding in the next article. I hope that you fully understand the importance of vectors and some fundemental operations on them. Next article, I promise, we will start coding some 3D stuff. Until then, read through this thoroughly and make sure that you understand it.

  • Copyright © 1997 Chris Lattner