• Immutable Page
  • Info
  • Attachments

GLVector

A GLVector represents a normalized vector in real 3D space.

Class Definition

/**
 * Class which describes a point or vector
 * in 3D space
 */
class GLVector {
    public:
        typedef enum {
            x,
            y,
            z,
            w
        } VectorCoordsEnum;

        GLVector ();
        GLVector (float x, float y, float z, float w);

        /**
         * Returns a reference to the x, y, z or w value by using
         * 0, 1, 2, 3 as array-access items
         */
        float& operator[] (int item);

        /**
         * Returns a reference to the x, y, z or w value by using
         * x, y, z, w as array-access items
         */
        float& operator[] (VectorCoordsEnum coord);

        /**
         * Returns a readonly x, y, z or w value by using
         * 0, 1, 2, 3 as array-access items
         */
        const float operator[] (int item) const;

        /**
         * Returns a readonly x, y, z or w value by using
         * x, y, z, w as array-access items
         */
        const float operator[] (VectorCoordsEnum coord) const;

        /**
         * Adds all elements in a GLVector
         */
        GLVector& operator+= (const GLVector& rhs);

        /**
         * Subtracts all elements in a GLVector
         */
        GLVector& operator-= (const GLVector& rhs);

        /**
         * Scales all elements in a vector
         * @param k Scale factor
         */
        GLVector& operator*= (const float k);

        /**
         * Scales all elements in a vector by 1 / k
         * @param k Scale factor
         */
        GLVector& operator/= (const float k);
        GLVector& operator^= (const GLVector& rhs);

        /**
         * Returns the norm of this vector
         */
        float norm ();

        /**
         * Returns the normalized version of the vector
         */
        GLVector& normalize ();
        
        /**
         * Returns the homogenized version of the vector
         */
        GLVector& homogenize ();

    private:
        friend GLVector operator+ (const GLVector& lhs,
                                   const GLVector& rhs);
        friend GLVector operator- (const GLVector& lhs,
                                   const GLVector& rhs);
        friend GLVector operator- (const GLVector& vector);
        friend float operator* (const GLVector& lhs,
                                const GLVector& rhs);
        friend GLVector operator* (const float       k,
                                   const GLVector& vector);
        friend GLVector operator* (const GLVector& vector,
                                   const float       k);
        friend GLVector operator/ (const GLVector& lhs,
                                   const GLVector& rhs);
        friend GLVector operator^ (const GLVector& lhs,
                                   const GLVector& rhs);

        float v[4];
};

Working with a Vector

Most 4x1 vector operations are exposed through the various operator overloads. Most importantly, it is possible to multiply a GLVector by a [[Development/zero-nine/OpenGLClasses/GLMatrix||GLMatrix]] to find the transformed position of that vector with the matrix.

Finally, since GLVector is actually a 4x1 vector, it is necessary to ::homogenize () and ::normalize () the vector when using it to figure out orientation.

Development/zero-nine/OpenGLClasses/GLVector (last edited 2010-09-05 02:34:23 by dsl-124-150-53-112)