Vector

The main input to OpenGL is vertex, which can uniquely represents a position in xyz coordinate space. An xyz triplet can be represented by a vector. A vector has two important values: a direction and a magnitude.

  • The direction of a vector is from the origin toward a point in space.

  • The magnitude of a vector is the length of the vector.

Unit Vector: A vector with a length of 1.

Normalization: Changing a none unit vector into a unit vector.

The vmath library provided with this book's source code contains definition about three-component and four-component vectors.

template <typename T, const int len>
class vecN
template <typename T>
class Tvec3 : public vecN<T,3>
template <typename T>
class Tvec4 : public vecN<T,4>

Component-wise Operations

Addition:

Addition

    inline vecN operator+(const vecN& that) const
    {
        my_type result;
        int n;
        for (n = 0; n < len; n++)
            result.data[n] = data[n] + that.data[n];
        return result;
    }

Subtraction:

Subtraction

    inline vecN operator-(const vecN& that) const
    {
        my_type result;
        int n;
        for (n = 0; n < len; n++)
            result.data[n] = data[n] - that.data[n];
        return result;
    }

Negation:

Negation
Negation

    inline vecN operator-() const
    {
        my_type result;
        int n;
        for (n = 0; n < len; n++)
            result.data[n] = -data[n];
        return result;
    }

Dot Product

The dot product (inner product) between two vectors returns a scalar that is the cosine of the angle between the two vectors scaled by the product of their lengths.

We can use dot product in following two situations:

  • Find the angle between two vectors.
  • Determine perpendicularity between two vectors.

The dot product of two vectors V1 and V2 is calculated as:

Dot Product

    template <typename T, int len>
    static inline T dot(const vecN<T,len>& a, const vecN<T,len>& b)
    {
        int n;
        T total = T(0);
        for (n = 0; n < len; n++)
        {
            total += a[n] * b[n];
        }
        return total;
    }

Cross Product

The cross product (vector product) between two vectors is a third vector that is perpendicular to the plane in which the first two vectors lie.

There are two important properties about cross product:

  • The cross product of two vector is another vector that is right angles to both.
  • The magnitude of the cross product equals the are of a parallelogram with vectors for sides.

The cross product of two vectors is defined as:

cross product

where n is the unit vector that is perpendicular to both V1 and V2. If V1 and V2 are both unit length, and are known to be perpendicular to each other, then the result will also be unit length.

The cross product of two-dimensional vectors V1 and V2 can be calculated as:

图片说明

    template <typename T, int len>
    static inline T dot(const vecN<T,len>& a, const vecN<T,len>& b)
    {
        int n;
        T total = T(0);
        for (n = 0; n < len; n++)
        {
            total += a[n] * b[n];
        }
        return total;
    }

Length

The magnitude of a vector is also known as its length. The magnitude of a three-dimensional vector can be found by using the following equation:

Magnitude

    template <typename T, int len>
    static inline T length(const vecN<T,len>& v)
    {
        T result(0);

        for (int i = 0; i < v.size(); ++i)
        {
            result += v[i] * v[i];
        }

        return (T)sqrt(result);
    }

Reflection

The math for calculating reflection is

Reflection

    template <typename T, const int S>
    static inline vecN<T,S> reflect(const vecN<T,S>& I, const vecN<T,S>& N)
    {
        return I - 2 * dot(N, I) * N;
    }