Commits

Eric Fredricksen committed fab9349

Upgrade glMatrix to 2.0

Comments (0)

Files changed (2)

 /**
- * @fileoverview gl-matrix - High performance matrix and vector operations for WebGL
+ * @fileoverview gl-matrix - High performance matrix and vector operations
  * @author Brandon Jones
  * @author Colin MacKenzie IV
- * @version 1.3.7
+ * @version 2.0.1
  */
 
-/*
- * Copyright (c) 2012 Brandon Jones, Colin MacKenzie IV
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- *    1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- *
- *    2. Altered source versions must be plainly marked as such, and must not
- *    be misrepresented as being the original software.
+/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+  * Redistributions of source code must retain the above copyright notice, this
+    list of conditions and the following disclaimer.
+  * Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation 
+    and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+
+
+(function() {
+  "use strict";
+
+  var shim = {};
+  if (typeof(exports) === 'undefined') {
+    if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
+      shim.exports = {};
+      define(function() {
+        return shim.exports;
+      });
+    } else {
+      // gl-matrix lives in a browser, define its namespaces in global
+      shim.exports = window;
+    }    
+  }
+  else {
+    // gl-matrix lives in commonjs, define its namespaces in exports
+    shim.exports = exports;
+  }
+
+  (function(exports) {
+    /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+  * Redistributions of source code must retain the above copyright notice, this
+    list of conditions and the following disclaimer.
+  * Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation 
+    and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+
+
+if(!GLMAT_EPSILON) {
+    var GLMAT_EPSILON = 0.000001;
+}
+
+if(!GLMAT_ARRAY_TYPE) {
+    var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
+}
+
+/**
+ * @class Common utilities
+ * @name glMatrix
+ */
+var glMatrix = {};
+
+/**
+ * Sets the type of array used when creating new vectors and matricies
  *
- *    3. This notice may not be removed or altered from any source
- *    distribution.
+ * @param {Type} type Array type, such as Float32Array or Array
  */
+glMatrix.setMatrixArrayType = function(type) {
+    GLMAT_ARRAY_TYPE = type;
+}
+
+if(typeof(exports) !== 'undefined') {
+    exports.glMatrix = glMatrix;
+}
+;
+/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+  * Redistributions of source code must retain the above copyright notice, this
+    list of conditions and the following disclaimer.
+  * Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation 
+    and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
-// Updated to use a modification of the "returnExportsGlobal" pattern from https://github.com/umdjs/umd
+/**
+ * @class 2 Dimensional Vector
+ * @name vec2
+ */
 
-(function (root, factory) {
-    if (typeof exports === 'object') {
-        // Node. Does not work with strict CommonJS, but
-        // only CommonJS-like enviroments that support module.exports,
-        // like Node.
-        module.exports = factory(global);
-    } else if (typeof define === 'function' && define.amd) {
-        // AMD. Register as an anonymous module.
-        define([], function () {
-            return factory(root);
-        });
-    } else {
-        // Browser globals
-        factory(root);
-    }
-}(this, function (root) {
-    "use strict";
-
-    // Tweak to your liking
-    var FLOAT_EPSILON = 0.000001;
-
-    var glMath = {};
-    (function() {
-        if (typeof(Float32Array) != 'undefined') {
-            var y = new Float32Array(1);
-            var i = new Int32Array(y.buffer);
-
-            /**
-             * Fast way to calculate the inverse square root,
-             * see http://jsperf.com/inverse-square-root/5
-             *
-             * If typed arrays are not available, a slower
-             * implementation will be used.
-             *
-             * @param {Number} number the number
-             * @returns {Number} Inverse square root
-             */
-            glMath.invsqrt = function(number) {
-              var x2 = number * 0.5;
-              y[0] = number;
-              var threehalfs = 1.5;
-
-              i[0] = 0x5f3759df - (i[0] >> 1);
-
-              var number2 = y[0];
-
-              return number2 * (threehalfs - (x2 * number2 * number2));
-            };
-        } else {
-            glMath.invsqrt = function(number) { return 1.0 / Math.sqrt(number); };
-        }
-    })();
+var vec2 = {};
 
-    /**
-     * @class System-specific optimal array type
-     * @name MatrixArray
-     */
-    var MatrixArray = null;
-    
-    // explicitly sets and returns the type of array to use within glMatrix
-    function setMatrixArrayType(type) {
-        MatrixArray = type;
-        return MatrixArray;
-    }
+/**
+ * Creates a new, empty vec2
+ *
+ * @returns {vec2} a new 2D vector
+ */
+vec2.create = function() {
+    var out = new GLMAT_ARRAY_TYPE(2);
+    out[0] = 0;
+    out[1] = 0;
+    return out;
+};
 
-    // auto-detects and returns the best type of array to use within glMatrix, falling
-    // back to Array if typed arrays are unsupported
-    function determineMatrixArrayType() {
-        MatrixArray = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
-        return MatrixArray;
-    }
-    
-    determineMatrixArrayType();
-
-    /**
-     * @class 3 Dimensional Vector
-     * @name vec3
-     */
-    var vec3 = {};
-     
-    /**
-     * Creates a new instance of a vec3 using the default array type
-     * Any javascript array-like objects containing at least 3 numeric elements can serve as a vec3
-     *
-     * @param {vec3} [vec] vec3 containing values to initialize with
-     *
-     * @returns {vec3} New vec3
-     */
-    vec3.create = function (vec) {
-        var dest = new MatrixArray(3);
-
-        if (vec) {
-            dest[0] = vec[0];
-            dest[1] = vec[1];
-            dest[2] = vec[2];
-        } else {
-            dest[0] = dest[1] = dest[2] = 0;
-        }
+/**
+ * Creates a new vec2 initialized with values from an existing vector
+ *
+ * @param {vec2} a vector to clone
+ * @returns {vec2} a new 2D vector
+ */
+vec2.clone = function(a) {
+    var out = new GLMAT_ARRAY_TYPE(2);
+    out[0] = a[0];
+    out[1] = a[1];
+    return out;
+};
 
-        return dest;
-    };
+/**
+ * Creates a new vec2 initialized with the given values
+ *
+ * @param {Number} x X component
+ * @param {Number} y Y component
+ * @returns {vec2} a new 2D vector
+ */
+vec2.fromValues = function(x, y) {
+    var out = new GLMAT_ARRAY_TYPE(2);
+    out[0] = x;
+    out[1] = y;
+    return out;
+};
 
-    /**
-     * Creates a new instance of a vec3, initializing it with the given arguments
-     *
-     * @param {number} x X value
-     * @param {number} y Y value
-     * @param {number} z Z value
+/**
+ * Copy the values from one vec2 to another
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the source vector
+ * @returns {vec2} out
+ */
+vec2.copy = function(out, a) {
+    out[0] = a[0];
+    out[1] = a[1];
+    return out;
+};
 
-     * @returns {vec3} New vec3
-     */
-    vec3.createFrom = function (x, y, z) {
-        var dest = new MatrixArray(3);
+/**
+ * Set the components of a vec2 to the given values
+ *
+ * @param {vec2} out the receiving vector
+ * @param {Number} x X component
+ * @param {Number} y Y component
+ * @returns {vec2} out
+ */
+vec2.set = function(out, x, y) {
+    out[0] = x;
+    out[1] = y;
+    return out;
+};
 
-        dest[0] = x;
-        dest[1] = y;
-        dest[2] = z;
+/**
+ * Adds two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.add = function(out, a, b) {
+    out[0] = a[0] + b[0];
+    out[1] = a[1] + b[1];
+    return out;
+};
 
-        return dest;
-    };
+/**
+ * Subtracts two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.subtract = function(out, a, b) {
+    out[0] = a[0] - b[0];
+    out[1] = a[1] - b[1];
+    return out;
+};
 
-    /**
-     * Copies the values of one vec3 to another
-     *
-     * @param {vec3} vec vec3 containing values to copy
-     * @param {vec3} dest vec3 receiving copied values
-     *
-     * @returns {vec3} dest
-     */
-    vec3.set = function (vec, dest) {
-        dest[0] = vec[0];
-        dest[1] = vec[1];
-        dest[2] = vec[2];
-
-        return dest;
-    };
+/**
+ * Alias for {@link vec2.subtract}
+ * @function
+ */
+vec2.sub = vec2.subtract;
 
-    /**
-     * Compares two vectors for equality within a certain margin of error
-     *
-     * @param {vec3} a First vector
-     * @param {vec3} b Second vector
-     *
-     * @returns {Boolean} True if a is equivalent to b
-     */
-    vec3.equal = function (a, b) {
-        return a === b || (
-            Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
-            Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
-            Math.abs(a[2] - b[2]) < FLOAT_EPSILON
-        );
-    };
+/**
+ * Multiplies two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.multiply = function(out, a, b) {
+    out[0] = a[0] * b[0];
+    out[1] = a[1] * b[1];
+    return out;
+};
 
-    /**
-     * Performs a vector addition
-     *
-     * @param {vec3} vec First operand
-     * @param {vec3} vec2 Second operand
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.add = function (vec, vec2, dest) {
-        if (!dest || vec === dest) {
-            vec[0] += vec2[0];
-            vec[1] += vec2[1];
-            vec[2] += vec2[2];
-            return vec;
-        }
+/**
+ * Alias for {@link vec2.multiply}
+ * @function
+ */
+vec2.mul = vec2.multiply;
 
-        dest[0] = vec[0] + vec2[0];
-        dest[1] = vec[1] + vec2[1];
-        dest[2] = vec[2] + vec2[2];
-        return dest;
-    };
+/**
+ * Divides two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.divide = function(out, a, b) {
+    out[0] = a[0] / b[0];
+    out[1] = a[1] / b[1];
+    return out;
+};
 
-    /**
-     * Performs a vector subtraction
-     *
-     * @param {vec3} vec First operand
-     * @param {vec3} vec2 Second operand
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.subtract = function (vec, vec2, dest) {
-        if (!dest || vec === dest) {
-            vec[0] -= vec2[0];
-            vec[1] -= vec2[1];
-            vec[2] -= vec2[2];
-            return vec;
-        }
+/**
+ * Alias for {@link vec2.divide}
+ * @function
+ */
+vec2.div = vec2.divide;
 
-        dest[0] = vec[0] - vec2[0];
-        dest[1] = vec[1] - vec2[1];
-        dest[2] = vec[2] - vec2[2];
-        return dest;
-    };
+/**
+ * Returns the minimum of two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.min = function(out, a, b) {
+    out[0] = Math.min(a[0], b[0]);
+    out[1] = Math.min(a[1], b[1]);
+    return out;
+};
 
-    /**
-     * Performs a vector multiplication
-     *
-     * @param {vec3} vec First operand
-     * @param {vec3} vec2 Second operand
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.multiply = function (vec, vec2, dest) {
-        if (!dest || vec === dest) {
-            vec[0] *= vec2[0];
-            vec[1] *= vec2[1];
-            vec[2] *= vec2[2];
-            return vec;
-        }
+/**
+ * Returns the maximum of two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec2} out
+ */
+vec2.max = function(out, a, b) {
+    out[0] = Math.max(a[0], b[0]);
+    out[1] = Math.max(a[1], b[1]);
+    return out;
+};
 
-        dest[0] = vec[0] * vec2[0];
-        dest[1] = vec[1] * vec2[1];
-        dest[2] = vec[2] * vec2[2];
-        return dest;
-    };
+/**
+ * Scales a vec2 by a scalar number
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the vector to scale
+ * @param {Number} b amount to scale the vector by
+ * @returns {vec2} out
+ */
+vec2.scale = function(out, a, b) {
+    out[0] = a[0] * b;
+    out[1] = a[1] * b;
+    return out;
+};
 
-    /**
-     * Negates the components of a vec3
-     *
-     * @param {vec3} vec vec3 to negate
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.negate = function (vec, dest) {
-        if (!dest) { dest = vec; }
-
-        dest[0] = -vec[0];
-        dest[1] = -vec[1];
-        dest[2] = -vec[2];
-        return dest;
-    };
+/**
+ * Calculates the euclidian distance between two vec2's
+ *
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {Number} distance between a and b
+ */
+vec2.distance = function(a, b) {
+    var x = b[0] - a[0],
+        y = b[1] - a[1];
+    return Math.sqrt(x*x + y*y);
+};
 
-    /**
-     * Multiplies the components of a vec3 by a scalar value
-     *
-     * @param {vec3} vec vec3 to scale
-     * @param {number} val Value to scale by
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.scale = function (vec, val, dest) {
-        if (!dest || vec === dest) {
-            vec[0] *= val;
-            vec[1] *= val;
-            vec[2] *= val;
-            return vec;
-        }
+/**
+ * Alias for {@link vec2.distance}
+ * @function
+ */
+vec2.dist = vec2.distance;
 
-        dest[0] = vec[0] * val;
-        dest[1] = vec[1] * val;
-        dest[2] = vec[2] * val;
-        return dest;
-    };
+/**
+ * Calculates the squared euclidian distance between two vec2's
+ *
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {Number} squared distance between a and b
+ */
+vec2.squaredDistance = function(a, b) {
+    var x = b[0] - a[0],
+        y = b[1] - a[1];
+    return x*x + y*y;
+};
 
-    /**
-     * Generates a unit vector of the same direction as the provided vec3
-     * If vector length is 0, returns [0, 0, 0]
-     *
-     * @param {vec3} vec vec3 to normalize
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.normalize = function (vec, dest) {
-        if (!dest) { dest = vec; }
-
-        var x = vec[0], y = vec[1], z = vec[2],
-            len = Math.sqrt(x * x + y * y + z * z);
-
-        if (!len) {
-            dest[0] = 0;
-            dest[1] = 0;
-            dest[2] = 0;
-            return dest;
-        } else if (len === 1) {
-            dest[0] = x;
-            dest[1] = y;
-            dest[2] = z;
-            return dest;
-        }
+/**
+ * Alias for {@link vec2.squaredDistance}
+ * @function
+ */
+vec2.sqrDist = vec2.squaredDistance;
 
-        len = 1 / len;
-        dest[0] = x * len;
-        dest[1] = y * len;
-        dest[2] = z * len;
-        return dest;
-    };
+/**
+ * Calculates the length of a vec2
+ *
+ * @param {vec2} a vector to calculate length of
+ * @returns {Number} length of a
+ */
+vec2.length = function (a) {
+    var x = a[0],
+        y = a[1];
+    return Math.sqrt(x*x + y*y);
+};
 
-    /**
-     * Generates the cross product of two vec3s
-     *
-     * @param {vec3} vec First operand
-     * @param {vec3} vec2 Second operand
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.cross = function (vec, vec2, dest) {
-        if (!dest) { dest = vec; }
-
-        var x = vec[0], y = vec[1], z = vec[2],
-            x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
-
-        dest[0] = y * z2 - z * y2;
-        dest[1] = z * x2 - x * z2;
-        dest[2] = x * y2 - y * x2;
-        return dest;
-    };
+/**
+ * Alias for {@link vec2.length}
+ * @function
+ */
+vec2.len = vec2.length;
 
-    /**
-     * Caclulates the length of a vec3
-     *
-     * @param {vec3} vec vec3 to calculate length of
-     *
-     * @returns {number} Length of vec
-     */
-    vec3.length = function (vec) {
-        var x = vec[0], y = vec[1], z = vec[2];
-        return Math.sqrt(x * x + y * y + z * z);
-    };
+/**
+ * Calculates the squared length of a vec2
+ *
+ * @param {vec2} a vector to calculate squared length of
+ * @returns {Number} squared length of a
+ */
+vec2.squaredLength = function (a) {
+    var x = a[0],
+        y = a[1];
+    return x*x + y*y;
+};
 
-    /**
-     * Caclulates the squared length of a vec3
-     *
-     * @param {vec3} vec vec3 to calculate squared length of
-     *
-     * @returns {number} Squared Length of vec
-     */
-    vec3.squaredLength = function (vec) {
-        var x = vec[0], y = vec[1], z = vec[2];
-        return x * x + y * y + z * z;
-    };
+/**
+ * Alias for {@link vec2.squaredLength}
+ * @function
+ */
+vec2.sqrLen = vec2.squaredLength;
 
-    /**
-     * Caclulates the dot product of two vec3s
-     *
-     * @param {vec3} vec First operand
-     * @param {vec3} vec2 Second operand
-     *
-     * @returns {number} Dot product of vec and vec2
-     */
-    vec3.dot = function (vec, vec2) {
-        return vec[0] * vec2[0] + vec[1] * vec2[1] + vec[2] * vec2[2];
-    };
+/**
+ * Negates the components of a vec2
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a vector to negate
+ * @returns {vec2} out
+ */
+vec2.negate = function(out, a) {
+    out[0] = -a[0];
+    out[1] = -a[1];
+    return out;
+};
 
-    /**
-     * Generates a unit vector pointing from one vector to another
-     *
-     * @param {vec3} vec Origin vec3
-     * @param {vec3} vec2 vec3 to point to
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.direction = function (vec, vec2, dest) {
-        if (!dest) { dest = vec; }
-
-        var x = vec[0] - vec2[0],
-            y = vec[1] - vec2[1],
-            z = vec[2] - vec2[2],
-            len = Math.sqrt(x * x + y * y + z * z);
-
-        if (!len) {
-            dest[0] = 0;
-            dest[1] = 0;
-            dest[2] = 0;
-            return dest;
-        }
+/**
+ * Normalize a vec2
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a vector to normalize
+ * @returns {vec2} out
+ */
+vec2.normalize = function(out, a) {
+    var x = a[0],
+        y = a[1];
+    var len = x*x + y*y;
+    if (len > 0) {
+        //TODO: evaluate use of glm_invsqrt here?
+        len = 1 / Math.sqrt(len);
+        out[0] = a[0] * len;
+        out[1] = a[1] * len;
+    }
+    return out;
+};
 
-        len = 1 / len;
-        dest[0] = x * len;
-        dest[1] = y * len;
-        dest[2] = z * len;
-        return dest;
-    };
+/**
+ * Calculates the dot product of two vec2's
+ *
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {Number} dot product of a and b
+ */
+vec2.dot = function (a, b) {
+    return a[0] * b[0] + a[1] * b[1];
+};
 
-    /**
-     * Performs a linear interpolation between two vec3
-     *
-     * @param {vec3} vec First vector
-     * @param {vec3} vec2 Second vector
-     * @param {number} lerp Interpolation amount between the two inputs
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.lerp = function (vec, vec2, lerp, dest) {
-        if (!dest) { dest = vec; }
-
-        dest[0] = vec[0] + lerp * (vec2[0] - vec[0]);
-        dest[1] = vec[1] + lerp * (vec2[1] - vec[1]);
-        dest[2] = vec[2] + lerp * (vec2[2] - vec[2]);
-
-        return dest;
-    };
+/**
+ * Computes the cross product of two vec2's
+ * Note that the cross product must by definition produce a 3D vector
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @returns {vec3} out
+ */
+vec2.cross = function(out, a, b) {
+    var z = a[0] * b[1] - a[1] * b[0];
+    out[0] = out[1] = 0;
+    out[2] = z;
+    return out;
+};
 
-    /**
-     * Calculates the euclidian distance between two vec3
-     *
-     * Params:
-     * @param {vec3} vec First vector
-     * @param {vec3} vec2 Second vector
-     *
-     * @returns {number} Distance between vec and vec2
-     */
-    vec3.dist = function (vec, vec2) {
-        var x = vec2[0] - vec[0],
-            y = vec2[1] - vec[1],
-            z = vec2[2] - vec[2];
-            
-        return Math.sqrt(x*x + y*y + z*z);
-    };
+/**
+ * Performs a linear interpolation between two vec2's
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the first operand
+ * @param {vec2} b the second operand
+ * @param {Number} t interpolation amount between the two inputs
+ * @returns {vec2} out
+ */
+vec2.lerp = function (out, a, b, t) {
+    var ax = a[0],
+        ay = a[1];
+    out[0] = ax + t * (b[0] - ax);
+    out[1] = ay + t * (b[1] - ay);
+    return out;
+};
 
-    // Pre-allocated to prevent unecessary garbage collection
-    var unprojectMat = null;
-    var unprojectVec = new MatrixArray(4);
-    /**
-     * Projects the specified vec3 from screen space into object space
-     * Based on the <a href="http://webcvs.freedesktop.org/mesa/Mesa/src/glu/mesa/project.c?revision=1.4&view=markup">Mesa gluUnProject implementation</a>
-     *
-     * @param {vec3} vec Screen-space vector to project
-     * @param {mat4} view View matrix
-     * @param {mat4} proj Projection matrix
-     * @param {vec4} viewport Viewport as given to gl.viewport [x, y, width, height]
-     * @param {vec3} [dest] vec3 receiving unprojected result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    vec3.unproject = function (vec, view, proj, viewport, dest) {
-        if (!dest) { dest = vec; }
-
-        if(!unprojectMat) {
-            unprojectMat = mat4.create();
-        }
+/**
+ * Transforms the vec2 with a mat2
+ *
+ * @param {vec2} out the receiving vector
+ * @param {vec2} a the vector to transform
+ * @param {mat2} m matrix to transform with
+ * @returns {vec2} out
+ */
+vec2.transformMat2 = function(out, a, m) {
+    var x = a[0],
+        y = a[1];
+    out[0] = x * m[0] + y * m[1];
+    out[1] = x * m[2] + y * m[3];
+    return out;
+};
 
-        var m = unprojectMat;
-        var v = unprojectVec;
-        
-        v[0] = (vec[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
-        v[1] = (vec[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
-        v[2] = 2.0 * vec[2] - 1.0;
-        v[3] = 1.0;
-        
-        mat4.multiply(proj, view, m);
-        if(!mat4.inverse(m)) { return null; }
-        
-        mat4.multiplyVec4(m, v);
-        if(v[3] === 0.0) { return null; }
+/**
+ * Perform some operation over an array of vec2s.
+ *
+ * @param {Array} a the array of vectors to iterate over
+ * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
+ * @param {Number} offset Number of elements to skip at the beginning of the array
+ * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
+ * @param {Function} fn Function to call for each vector in the array
+ * @param {Object} [arg] additional argument to pass to fn
+ * @returns {Array} a
+ * @function
+ */
+vec2.forEach = (function() {
+    var vec = vec2.create();
 
-        dest[0] = v[0] / v[3];
-        dest[1] = v[1] / v[3];
-        dest[2] = v[2] / v[3];
-        
-        return dest;
-    };
+    return function(a, stride, offset, count, fn, arg) {
+        var i, l;
+        if(!stride) {
+            stride = 2;
+        }
 
-    var xUnitVec3 = vec3.createFrom(1,0,0);
-    var yUnitVec3 = vec3.createFrom(0,1,0);
-    var zUnitVec3 = vec3.createFrom(0,0,1);
-
-    var tmpvec3 = vec3.create();
-    /**
-     * Generates a quaternion of rotation between two given normalized vectors
-     *
-     * @param {vec3} a Normalized source vector
-     * @param {vec3} b Normalized target vector
-     * @param {quat4} [dest] quat4 receiving operation result.
-     *
-     * @returns {quat4} dest if specified, a new quat4 otherwise
-     */
-    vec3.rotationTo = function (a, b, dest) {
-        if (!dest) { dest = quat4.create(); }
+        if(!offset) {
+            offset = 0;
+        }
         
-        var d = vec3.dot(a, b);
-        var axis = tmpvec3;
-        if (d >= 1.0) {
-            quat4.set(identityQuat4, dest);
-        } else if (d < (0.000001 - 1.0)) {
-            vec3.cross(xUnitVec3, a, axis);
-            if (vec3.length(axis) < 0.000001)
-                vec3.cross(yUnitVec3, a, axis);
-            if (vec3.length(axis) < 0.000001)
-                vec3.cross(zUnitVec3, a, axis);
-            vec3.normalize(axis);
-            quat4.fromAngleAxis(Math.PI, axis, dest);
+        if(count) {
+            l = Math.min((count * stride) + offset, a.length);
         } else {
-            var s = Math.sqrt((1.0 + d) * 2.0);
-            var sInv = 1.0 / s;
-            vec3.cross(a, b, axis);
-            dest[0] = axis[0] * sInv;
-            dest[1] = axis[1] * sInv;
-            dest[2] = axis[2] * sInv;
-            dest[3] = s * 0.5;
-            quat4.normalize(dest);
+            l = a.length;
         }
-        if (dest[3] > 1.0) dest[3] = 1.0;
-        else if (dest[3] < -1.0) dest[3] = -1.0;
-        return dest;
-    };
-
-    /**
-     * Returns a string representation of a vector
-     *
-     * @param {vec3} vec Vector to represent as a string
-     *
-     * @returns {string} String representation of vec
-     */
-    vec3.str = function (vec) {
-        return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ']';
-    };
 
-    /**
-     * @class 3x3 Matrix
-     * @name mat3
-     */
-    var mat3 = {};
-
-    /**
-     * Creates a new instance of a mat3 using the default array type
-     * Any javascript array-like object containing at least 9 numeric elements can serve as a mat3
-     *
-     * @param {mat3} [mat] mat3 containing values to initialize with
-     *
-     * @returns {mat3} New mat3
-     */
-    mat3.create = function (mat) {
-        var dest = new MatrixArray(9);
-
-        if (mat) {
-            dest[0] = mat[0];
-            dest[1] = mat[1];
-            dest[2] = mat[2];
-            dest[3] = mat[3];
-            dest[4] = mat[4];
-            dest[5] = mat[5];
-            dest[6] = mat[6];
-            dest[7] = mat[7];
-            dest[8] = mat[8];
-        } else {
-            dest[0] = dest[1] =
-            dest[2] = dest[3] =
-            dest[4] = dest[5] =
-            dest[6] = dest[7] =
-            dest[8] = 0;
+        for(i = offset; i < l; i += stride) {
+            vec[0] = a[i]; vec[1] = a[i+1];
+            fn(vec, vec, arg);
+            a[i] = vec[0]; a[i+1] = vec[1];
         }
-
-        return dest;
+        
+        return a;
     };
+})();
 
-    /**
-     * Creates a new instance of a mat3, initializing it with the given arguments
-     *
-     * @param {number} m00
-     * @param {number} m01
-     * @param {number} m02
-     * @param {number} m10
-     * @param {number} m11
-     * @param {number} m12
-     * @param {number} m20
-     * @param {number} m21
-     * @param {number} m22
-
-     * @returns {mat3} New mat3
-     */
-    mat3.createFrom = function (m00, m01, m02, m10, m11, m12, m20, m21, m22) {
-        var dest = new MatrixArray(9);
-
-        dest[0] = m00;
-        dest[1] = m01;
-        dest[2] = m02;
-        dest[3] = m10;
-        dest[4] = m11;
-        dest[5] = m12;
-        dest[6] = m20;
-        dest[7] = m21;
-        dest[8] = m22;
-
-        return dest;
-    };
+/**
+ * Returns a string representation of a vector
+ *
+ * @param {vec2} vec vector to represent as a string
+ * @returns {String} string representation of the vector
+ */
+vec2.str = function (a) {
+    return 'vec2(' + a[0] + ', ' + a[1] + ')';
+};
+
+if(typeof(exports) !== 'undefined') {
+    exports.vec2 = vec2;
+}
+;
+/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+  * Redistributions of source code must retain the above copyright notice, this
+    list of conditions and the following disclaimer.
+  * Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation 
+    and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
-    /**
-     * Calculates the determinant of a mat3
-     *
-     * @param {mat3} mat mat3 to calculate determinant of
-     *
-     * @returns {Number} determinant of mat
-     */
-    mat3.determinant = function (mat) {
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2],
-            a10 = mat[3], a11 = mat[4], a12 = mat[5],
-            a20 = mat[6], a21 = mat[7], a22 = mat[8];
-
-        return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
-    };
+/**
+ * @class 3 Dimensional Vector
+ * @name vec3
+ */
 
-    /**
-     * Calculates the inverse matrix of a mat3
-     *
-     * @param {mat3} mat mat3 to calculate inverse of
-     * @param {mat3} [dest] mat3 receiving inverse matrix. If not specified result is written to mat
-     *
-     * @param {mat3} dest is specified, mat otherwise, null if matrix cannot be inverted
-     */
-    mat3.inverse = function (mat, dest) {
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2],
-            a10 = mat[3], a11 = mat[4], a12 = mat[5],
-            a20 = mat[6], a21 = mat[7], a22 = mat[8],
-
-            b01 = a22 * a11 - a12 * a21,
-            b11 = -a22 * a10 + a12 * a20,
-            b21 = a21 * a10 - a11 * a20,
-
-            d = a00 * b01 + a01 * b11 + a02 * b21,
-            id;
-
-        if (!d) { return null; }
-        id = 1 / d;
-
-        if (!dest) { dest = mat3.create(); }
-
-        dest[0] = b01 * id;
-        dest[1] = (-a22 * a01 + a02 * a21) * id;
-        dest[2] = (a12 * a01 - a02 * a11) * id;
-        dest[3] = b11 * id;
-        dest[4] = (a22 * a00 - a02 * a20) * id;
-        dest[5] = (-a12 * a00 + a02 * a10) * id;
-        dest[6] = b21 * id;
-        dest[7] = (-a21 * a00 + a01 * a20) * id;
-        dest[8] = (a11 * a00 - a01 * a10) * id;
-        return dest;
-    };
-    
-    /**
-     * Performs a matrix multiplication
-     *
-     * @param {mat3} mat First operand
-     * @param {mat3} mat2 Second operand
-     * @param {mat3} [dest] mat3 receiving operation result. If not specified result is written to mat
-     *
-     * @returns {mat3} dest if specified, mat otherwise
-     */
-    mat3.multiply = function (mat, mat2, dest) {
-        if (!dest) { dest = mat; }
-        
+var vec3 = {};
 
-        // Cache the matrix values (makes for huge speed increases!)
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2],
-            a10 = mat[3], a11 = mat[4], a12 = mat[5],
-            a20 = mat[6], a21 = mat[7], a22 = mat[8],
+/**
+ * Creates a new, empty vec3
+ *
+ * @returns {vec3} a new 3D vector
+ */
+vec3.create = function() {
+    var out = new GLMAT_ARRAY_TYPE(3);
+    out[0] = 0;
+    out[1] = 0;
+    out[2] = 0;
+    return out;
+};
 
-            b00 = mat2[0], b01 = mat2[1], b02 = mat2[2],
-            b10 = mat2[3], b11 = mat2[4], b12 = mat2[5],
-            b20 = mat2[6], b21 = mat2[7], b22 = mat2[8];
+/**
+ * Creates a new vec3 initialized with values from an existing vector
+ *
+ * @param {vec3} a vector to clone
+ * @returns {vec3} a new 3D vector
+ */
+vec3.clone = function(a) {
+    var out = new GLMAT_ARRAY_TYPE(3);
+    out[0] = a[0];
+    out[1] = a[1];
+    out[2] = a[2];
+    return out;
+};
 
-        dest[0] = b00 * a00 + b01 * a10 + b02 * a20;
-        dest[1] = b00 * a01 + b01 * a11 + b02 * a21;
-        dest[2] = b00 * a02 + b01 * a12 + b02 * a22;
+/**
+ * Creates a new vec3 initialized with the given values
+ *
+ * @param {Number} x X component
+ * @param {Number} y Y component
+ * @param {Number} z Z component
+ * @returns {vec3} a new 3D vector
+ */
+vec3.fromValues = function(x, y, z) {
+    var out = new GLMAT_ARRAY_TYPE(3);
+    out[0] = x;
+    out[1] = y;
+    out[2] = z;
+    return out;
+};
 
-        dest[3] = b10 * a00 + b11 * a10 + b12 * a20;
-        dest[4] = b10 * a01 + b11 * a11 + b12 * a21;
-        dest[5] = b10 * a02 + b11 * a12 + b12 * a22;
+/**
+ * Copy the values from one vec3 to another
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the source vector
+ * @returns {vec3} out
+ */
+vec3.copy = function(out, a) {
+    out[0] = a[0];
+    out[1] = a[1];
+    out[2] = a[2];
+    return out;
+};
 
-        dest[6] = b20 * a00 + b21 * a10 + b22 * a20;
-        dest[7] = b20 * a01 + b21 * a11 + b22 * a21;
-        dest[8] = b20 * a02 + b21 * a12 + b22 * a22;
+/**
+ * Set the components of a vec3 to the given values
+ *
+ * @param {vec3} out the receiving vector
+ * @param {Number} x X component
+ * @param {Number} y Y component
+ * @param {Number} z Z component
+ * @returns {vec3} out
+ */
+vec3.set = function(out, x, y, z) {
+    out[0] = x;
+    out[1] = y;
+    out[2] = z;
+    return out;
+};
 
-        return dest;
-    };
+/**
+ * Adds two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.add = function(out, a, b) {
+    out[0] = a[0] + b[0];
+    out[1] = a[1] + b[1];
+    out[2] = a[2] + b[2];
+    return out;
+};
 
-    /**
-     * Transforms the vec2 according to the given mat3.
-     *
-     * @param {mat3} matrix mat3 to multiply against
-     * @param {vec2} vec    the vector to multiply
-     * @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
-     *
-     * @returns {vec2} The multiplication result
-     **/
-    mat3.multiplyVec2 = function(matrix, vec, dest) {
-      if (!dest) dest = vec;
-      var x = vec[0], y = vec[1];
-      dest[0] = x * matrix[0] + y * matrix[3] + matrix[6];
-      dest[1] = x * matrix[1] + y * matrix[4] + matrix[7];
-      return dest;
-    };
+/**
+ * Subtracts two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.subtract = function(out, a, b) {
+    out[0] = a[0] - b[0];
+    out[1] = a[1] - b[1];
+    out[2] = a[2] - b[2];
+    return out;
+};
 
-    /**
-     * Transforms the vec3 according to the given mat3
-     *
-     * @param {mat3} matrix mat3 to multiply against
-     * @param {vec3} vec    the vector to multiply
-     * @param {vec3} [dest] an optional receiving vector. If not given, vec is used.
-     *
-     * @returns {vec3} The multiplication result
-     **/
-    mat3.multiplyVec3 = function(matrix, vec, dest) {
-      if (!dest) dest = vec;
-      var x = vec[0], y = vec[1], z = vec[2];
-      dest[0] = x * matrix[0] + y * matrix[3] + z * matrix[6];
-      dest[1] = x * matrix[1] + y * matrix[4] + z * matrix[7];
-      dest[2] = x * matrix[2] + y * matrix[5] + z * matrix[8];
-      
-      return dest;
-    };
+/**
+ * Alias for {@link vec3.subtract}
+ * @function
+ */
+vec3.sub = vec3.subtract;
 
-    /**
-     * Copies the values of one mat3 to another
-     *
-     * @param {mat3} mat mat3 containing values to copy
-     * @param {mat3} dest mat3 receiving copied values
-     *
-     * @returns {mat3} dest
-     */
-    mat3.set = function (mat, dest) {
-        dest[0] = mat[0];
-        dest[1] = mat[1];
-        dest[2] = mat[2];
-        dest[3] = mat[3];
-        dest[4] = mat[4];
-        dest[5] = mat[5];
-        dest[6] = mat[6];
-        dest[7] = mat[7];
-        dest[8] = mat[8];
-        return dest;
-    };
+/**
+ * Multiplies two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.multiply = function(out, a, b) {
+    out[0] = a[0] * b[0];
+    out[1] = a[1] * b[1];
+    out[2] = a[2] * b[2];
+    return out;
+};
 
-    /**
-     * Compares two matrices for equality within a certain margin of error
-     *
-     * @param {mat3} a First matrix
-     * @param {mat3} b Second matrix
-     *
-     * @returns {Boolean} True if a is equivalent to b
-     */
-    mat3.equal = function (a, b) {
-        return a === b || (
-            Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
-            Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
-            Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
-            Math.abs(a[3] - b[3]) < FLOAT_EPSILON &&
-            Math.abs(a[4] - b[4]) < FLOAT_EPSILON &&
-            Math.abs(a[5] - b[5]) < FLOAT_EPSILON &&
-            Math.abs(a[6] - b[6]) < FLOAT_EPSILON &&
-            Math.abs(a[7] - b[7]) < FLOAT_EPSILON &&
-            Math.abs(a[8] - b[8]) < FLOAT_EPSILON
-        );
-    };
+/**
+ * Alias for {@link vec3.multiply}
+ * @function
+ */
+vec3.mul = vec3.multiply;
 
-    /**
-     * Sets a mat3 to an identity matrix
-     *
-     * @param {mat3} dest mat3 to set
-     *
-     * @returns dest if specified, otherwise a new mat3
-     */
-    mat3.identity = function (dest) {
-        if (!dest) { dest = mat3.create(); }
-        dest[0] = 1;
-        dest[1] = 0;
-        dest[2] = 0;
-        dest[3] = 0;
-        dest[4] = 1;
-        dest[5] = 0;
-        dest[6] = 0;
-        dest[7] = 0;
-        dest[8] = 1;
-        return dest;
-    };
+/**
+ * Divides two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.divide = function(out, a, b) {
+    out[0] = a[0] / b[0];
+    out[1] = a[1] / b[1];
+    out[2] = a[2] / b[2];
+    return out;
+};
 
-    /**
-     * Transposes a mat3 (flips the values over the diagonal)
-     *
-     * Params:
-     * @param {mat3} mat mat3 to transpose
-     * @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
-     *
-     * @returns {mat3} dest is specified, mat otherwise
-     */
-    mat3.transpose = function (mat, dest) {
-        // If we are transposing ourselves we can skip a few steps but have to cache some values
-        if (!dest || mat === dest) {
-            var a01 = mat[1], a02 = mat[2],
-                a12 = mat[5];
-
-            mat[1] = mat[3];
-            mat[2] = mat[6];
-            mat[3] = a01;
-            mat[5] = mat[7];
-            mat[6] = a02;
-            mat[7] = a12;
-            return mat;
-        }
+/**
+ * Alias for {@link vec3.divide}
+ * @function
+ */
+vec3.div = vec3.divide;
 
-        dest[0] = mat[0];
-        dest[1] = mat[3];
-        dest[2] = mat[6];
-        dest[3] = mat[1];
-        dest[4] = mat[4];
-        dest[5] = mat[7];
-        dest[6] = mat[2];
-        dest[7] = mat[5];
-        dest[8] = mat[8];
-        return dest;
-    };
+/**
+ * Returns the minimum of two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.min = function(out, a, b) {
+    out[0] = Math.min(a[0], b[0]);
+    out[1] = Math.min(a[1], b[1]);
+    out[2] = Math.min(a[2], b[2]);
+    return out;
+};
 
-    /**
-     * Copies the elements of a mat3 into the upper 3x3 elements of a mat4
-     *
-     * @param {mat3} mat mat3 containing values to copy
-     * @param {mat4} [dest] mat4 receiving copied values
-     *
-     * @returns {mat4} dest if specified, a new mat4 otherwise
-     */
-    mat3.toMat4 = function (mat, dest) {
-        if (!dest) { dest = mat4.create(); }
-
-        dest[15] = 1;
-        dest[14] = 0;
-        dest[13] = 0;
-        dest[12] = 0;
-
-        dest[11] = 0;
-        dest[10] = mat[8];
-        dest[9] = mat[7];
-        dest[8] = mat[6];
-
-        dest[7] = 0;
-        dest[6] = mat[5];
-        dest[5] = mat[4];
-        dest[4] = mat[3];
-
-        dest[3] = 0;
-        dest[2] = mat[2];
-        dest[1] = mat[1];
-        dest[0] = mat[0];
-
-        return dest;
-    };
+/**
+ * Returns the maximum of two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.max = function(out, a, b) {
+    out[0] = Math.max(a[0], b[0]);
+    out[1] = Math.max(a[1], b[1]);
+    out[2] = Math.max(a[2], b[2]);
+    return out;
+};
 
-    /**
-     * Returns a string representation of a mat3
-     *
-     * @param {mat3} mat mat3 to represent as a string
-     *
-     * @param {string} String representation of mat
-     */
-    mat3.str = function (mat) {
-        return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] +
-            ', ' + mat[3] + ', ' + mat[4] + ', ' + mat[5] +
-            ', ' + mat[6] + ', ' + mat[7] + ', ' + mat[8] + ']';
-    };
+/**
+ * Scales a vec3 by a scalar number
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the vector to scale
+ * @param {Number} b amount to scale the vector by
+ * @returns {vec3} out
+ */
+vec3.scale = function(out, a, b) {
+    out[0] = a[0] * b;
+    out[1] = a[1] * b;
+    out[2] = a[2] * b;
+    return out;
+};
 
-    /**
-     * @class 4x4 Matrix
-     * @name mat4
-     */
-    var mat4 = {};
-
-    /**
-     * Creates a new instance of a mat4 using the default array type
-     * Any javascript array-like object containing at least 16 numeric elements can serve as a mat4
-     *
-     * @param {mat4} [mat] mat4 containing values to initialize with
-     *
-     * @returns {mat4} New mat4
-     */
-    mat4.create = function (mat) {
-        var dest = new MatrixArray(16);
-
-        if (mat) {
-            dest[0] = mat[0];
-            dest[1] = mat[1];
-            dest[2] = mat[2];
-            dest[3] = mat[3];
-            dest[4] = mat[4];
-            dest[5] = mat[5];
-            dest[6] = mat[6];
-            dest[7] = mat[7];
-            dest[8] = mat[8];
-            dest[9] = mat[9];
-            dest[10] = mat[10];
-            dest[11] = mat[11];
-            dest[12] = mat[12];
-            dest[13] = mat[13];
-            dest[14] = mat[14];
-            dest[15] = mat[15];
-        }
+/**
+ * Calculates the euclidian distance between two vec3's
+ *
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {Number} distance between a and b
+ */
+vec3.distance = function(a, b) {
+    var x = b[0] - a[0],
+        y = b[1] - a[1],
+        z = b[2] - a[2];
+    return Math.sqrt(x*x + y*y + z*z);
+};
 
-        return dest;
-    };
+/**
+ * Alias for {@link vec3.distance}
+ * @function
+ */
+vec3.dist = vec3.distance;
 
-    /**
-     * Creates a new instance of a mat4, initializing it with the given arguments
-     *
-     * @param {number} m00
-     * @param {number} m01
-     * @param {number} m02
-     * @param {number} m03
-     * @param {number} m10
-     * @param {number} m11
-     * @param {number} m12
-     * @param {number} m13
-     * @param {number} m20
-     * @param {number} m21
-     * @param {number} m22
-     * @param {number} m23
-     * @param {number} m30
-     * @param {number} m31
-     * @param {number} m32
-     * @param {number} m33
-
-     * @returns {mat4} New mat4
-     */
-    mat4.createFrom = function (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
-        var dest = new MatrixArray(16);
-
-        dest[0] = m00;
-        dest[1] = m01;
-        dest[2] = m02;
-        dest[3] = m03;
-        dest[4] = m10;
-        dest[5] = m11;
-        dest[6] = m12;
-        dest[7] = m13;
-        dest[8] = m20;
-        dest[9] = m21;
-        dest[10] = m22;
-        dest[11] = m23;
-        dest[12] = m30;
-        dest[13] = m31;
-        dest[14] = m32;
-        dest[15] = m33;
-
-        return dest;
-    };
+/**
+ * Calculates the squared euclidian distance between two vec3's
+ *
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {Number} squared distance between a and b
+ */
+vec3.squaredDistance = function(a, b) {
+    var x = b[0] - a[0],
+        y = b[1] - a[1],
+        z = b[2] - a[2];
+    return x*x + y*y + z*z;
+};
 
-    /**
-     * Copies the values of one mat4 to another
-     *
-     * @param {mat4} mat mat4 containing values to copy
-     * @param {mat4} dest mat4 receiving copied values
-     *
-     * @returns {mat4} dest
-     */
-    mat4.set = function (mat, dest) {
-        dest[0] = mat[0];
-        dest[1] = mat[1];
-        dest[2] = mat[2];
-        dest[3] = mat[3];
-        dest[4] = mat[4];
-        dest[5] = mat[5];
-        dest[6] = mat[6];
-        dest[7] = mat[7];
-        dest[8] = mat[8];
-        dest[9] = mat[9];
-        dest[10] = mat[10];
-        dest[11] = mat[11];
-        dest[12] = mat[12];
-        dest[13] = mat[13];
-        dest[14] = mat[14];
-        dest[15] = mat[15];
-        return dest;
-    };
+/**
+ * Alias for {@link vec3.squaredDistance}
+ * @function
+ */
+vec3.sqrDist = vec3.squaredDistance;
 
-    /**
-     * Compares two matrices for equality within a certain margin of error
-     *
-     * @param {mat4} a First matrix
-     * @param {mat4} b Second matrix
-     *
-     * @returns {Boolean} True if a is equivalent to b
-     */
-    mat4.equal = function (a, b) {
-        return a === b || (
-            Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
-            Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
-            Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
-            Math.abs(a[3] - b[3]) < FLOAT_EPSILON &&
-            Math.abs(a[4] - b[4]) < FLOAT_EPSILON &&
-            Math.abs(a[5] - b[5]) < FLOAT_EPSILON &&
-            Math.abs(a[6] - b[6]) < FLOAT_EPSILON &&
-            Math.abs(a[7] - b[7]) < FLOAT_EPSILON &&
-            Math.abs(a[8] - b[8]) < FLOAT_EPSILON &&
-            Math.abs(a[9] - b[9]) < FLOAT_EPSILON &&
-            Math.abs(a[10] - b[10]) < FLOAT_EPSILON &&
-            Math.abs(a[11] - b[11]) < FLOAT_EPSILON &&
-            Math.abs(a[12] - b[12]) < FLOAT_EPSILON &&
-            Math.abs(a[13] - b[13]) < FLOAT_EPSILON &&
-            Math.abs(a[14] - b[14]) < FLOAT_EPSILON &&
-            Math.abs(a[15] - b[15]) < FLOAT_EPSILON
-        );
-    };
+/**
+ * Calculates the length of a vec3
+ *
+ * @param {vec3} a vector to calculate length of
+ * @returns {Number} length of a
+ */
+vec3.length = function (a) {
+    var x = a[0],
+        y = a[1],
+        z = a[2];
+    return Math.sqrt(x*x + y*y + z*z);
+};
 
-    /**
-     * Sets a mat4 to an identity matrix
-     *
-     * @param {mat4} dest mat4 to set
-     *
-     * @returns {mat4} dest
-     */
-    mat4.identity = function (dest) {
-        if (!dest) { dest = mat4.create(); }
-        dest[0] = 1;
-        dest[1] = 0;
-        dest[2] = 0;
-        dest[3] = 0;
-        dest[4] = 0;
-        dest[5] = 1;
-        dest[6] = 0;
-        dest[7] = 0;
-        dest[8] = 0;
-        dest[9] = 0;
-        dest[10] = 1;
-        dest[11] = 0;
-        dest[12] = 0;
-        dest[13] = 0;
-        dest[14] = 0;
-        dest[15] = 1;
-        return dest;
-    };
+/**
+ * Alias for {@link vec3.length}
+ * @function
+ */
+vec3.len = vec3.length;
 
-    /**
-     * Transposes a mat4 (flips the values over the diagonal)
-     *
-     * @param {mat4} mat mat4 to transpose
-     * @param {mat4} [dest] mat4 receiving transposed values. If not specified result is written to mat
-     *
-     * @param {mat4} dest is specified, mat otherwise
-     */
-    mat4.transpose = function (mat, dest) {
-        // If we are transposing ourselves we can skip a few steps but have to cache some values
-        if (!dest || mat === dest) {
-            var a01 = mat[1], a02 = mat[2], a03 = mat[3],
-                a12 = mat[6], a13 = mat[7],
-                a23 = mat[11];
-
-            mat[1] = mat[4];
-            mat[2] = mat[8];
-            mat[3] = mat[12];
-            mat[4] = a01;
-            mat[6] = mat[9];
-            mat[7] = mat[13];
-            mat[8] = a02;
-            mat[9] = a12;
-            mat[11] = mat[14];
-            mat[12] = a03;
-            mat[13] = a13;
-            mat[14] = a23;
-            return mat;
-        }
+/**
+ * Calculates the squared length of a vec3
+ *
+ * @param {vec3} a vector to calculate squared length of
+ * @returns {Number} squared length of a
+ */
+vec3.squaredLength = function (a) {
+    var x = a[0],
+        y = a[1],
+        z = a[2];
+    return x*x + y*y + z*z;
+};
 
-        dest[0] = mat[0];
-        dest[1] = mat[4];
-        dest[2] = mat[8];
-        dest[3] = mat[12];
-        dest[4] = mat[1];
-        dest[5] = mat[5];
-        dest[6] = mat[9];
-        dest[7] = mat[13];
-        dest[8] = mat[2];
-        dest[9] = mat[6];
-        dest[10] = mat[10];
-        dest[11] = mat[14];
-        dest[12] = mat[3];
-        dest[13] = mat[7];
-        dest[14] = mat[11];
-        dest[15] = mat[15];
-        return dest;
-    };
+/**
+ * Alias for {@link vec3.squaredLength}
+ * @function
+ */
+vec3.sqrLen = vec3.squaredLength;
 
-    /**
-     * Calculates the determinant of a mat4
-     *
-     * @param {mat4} mat mat4 to calculate determinant of
-     *
-     * @returns {number} determinant of mat
-     */
-    mat4.determinant = function (mat) {
-        // Cache the matrix values (makes for huge speed increases!)
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
-            a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
-            a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
-            a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
-
-        return (a30 * a21 * a12 * a03 - a20 * a31 * a12 * a03 - a30 * a11 * a22 * a03 + a10 * a31 * a22 * a03 +
-                a20 * a11 * a32 * a03 - a10 * a21 * a32 * a03 - a30 * a21 * a02 * a13 + a20 * a31 * a02 * a13 +
-                a30 * a01 * a22 * a13 - a00 * a31 * a22 * a13 - a20 * a01 * a32 * a13 + a00 * a21 * a32 * a13 +
-                a30 * a11 * a02 * a23 - a10 * a31 * a02 * a23 - a30 * a01 * a12 * a23 + a00 * a31 * a12 * a23 +
-                a10 * a01 * a32 * a23 - a00 * a11 * a32 * a23 - a20 * a11 * a02 * a33 + a10 * a21 * a02 * a33 +
-                a20 * a01 * a12 * a33 - a00 * a21 * a12 * a33 - a10 * a01 * a22 * a33 + a00 * a11 * a22 * a33);
-    };
+/**
+ * Negates the components of a vec3
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a vector to negate
+ * @returns {vec3} out
+ */
+vec3.negate = function(out, a) {
+    out[0] = -a[0];
+    out[1] = -a[1];
+    out[2] = -a[2];
+    return out;
+};
 
-    /**
-     * Calculates the inverse matrix of a mat4
-     *
-     * @param {mat4} mat mat4 to calculate inverse of
-     * @param {mat4} [dest] mat4 receiving inverse matrix. If not specified result is written to mat
-     *
-     * @param {mat4} dest is specified, mat otherwise, null if matrix cannot be inverted
-     */
-    mat4.inverse = function (mat, dest) {
-        if (!dest) { dest = mat; }
-
-        // Cache the matrix values (makes for huge speed increases!)
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
-            a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
-            a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
-            a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
-
-            b00 = a00 * a11 - a01 * a10,
-            b01 = a00 * a12 - a02 * a10,
-            b02 = a00 * a13 - a03 * a10,
-            b03 = a01 * a12 - a02 * a11,
-            b04 = a01 * a13 - a03 * a11,
-            b05 = a02 * a13 - a03 * a12,
-            b06 = a20 * a31 - a21 * a30,
-            b07 = a20 * a32 - a22 * a30,
-            b08 = a20 * a33 - a23 * a30,
-            b09 = a21 * a32 - a22 * a31,
-            b10 = a21 * a33 - a23 * a31,
-            b11 = a22 * a33 - a23 * a32,
-
-            d = (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06),
-            invDet;
-
-            // Calculate the determinant
-            if (!d) { return null; }
-            invDet = 1 / d;
-
-        dest[0] = (a11 * b11 - a12 * b10 + a13 * b09) * invDet;
-        dest[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
-        dest[2] = (a31 * b05 - a32 * b04 + a33 * b03) * invDet;
-        dest[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
-        dest[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
-        dest[5] = (a00 * b11 - a02 * b08 + a03 * b07) * invDet;
-        dest[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
-        dest[7] = (a20 * b05 - a22 * b02 + a23 * b01) * invDet;
-        dest[8] = (a10 * b10 - a11 * b08 + a13 * b06) * invDet;
-        dest[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
-        dest[10] = (a30 * b04 - a31 * b02 + a33 * b00) * invDet;
-        dest[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
-        dest[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
-        dest[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet;
-        dest[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
-        dest[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet;
-
-        return dest;
-    };
+/**
+ * Normalize a vec3
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a vector to normalize
+ * @returns {vec3} out
+ */
+vec3.normalize = function(out, a) {
+    var x = a[0],
+        y = a[1],
+        z = a[2];
+    var len = x*x + y*y + z*z;
+    if (len > 0) {
+        //TODO: evaluate use of glm_invsqrt here?
+        len = 1 / Math.sqrt(len);
+        out[0] = a[0] * len;
+        out[1] = a[1] * len;
+        out[2] = a[2] * len;
+    }
+    return out;
+};
 
-    /**
-     * Copies the upper 3x3 elements of a mat4 into another mat4
-     *
-     * @param {mat4} mat mat4 containing values to copy
-     * @param {mat4} [dest] mat4 receiving copied values
-     *
-     * @returns {mat4} dest is specified, a new mat4 otherwise
-     */
-    mat4.toRotationMat = function (mat, dest) {
-        if (!dest) { dest = mat4.create(); }
-
-        dest[0] = mat[0];
-        dest[1] = mat[1];
-        dest[2] = mat[2];
-        dest[3] = mat[3];
-        dest[4] = mat[4];
-        dest[5] = mat[5];
-        dest[6] = mat[6];
-        dest[7] = mat[7];
-        dest[8] = mat[8];
-        dest[9] = mat[9];
-        dest[10] = mat[10];
-        dest[11] = mat[11];
-        dest[12] = 0;
-        dest[13] = 0;
-        dest[14] = 0;
-        dest[15] = 1;
-
-        return dest;
-    };
+/**
+ * Calculates the dot product of two vec3's
+ *
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {Number} dot product of a and b
+ */
+vec3.dot = function (a, b) {
+    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
+};
 
-    /**
-     * Copies the upper 3x3 elements of a mat4 into a mat3
-     *
-     * @param {mat4} mat mat4 containing values to copy
-     * @param {mat3} [dest] mat3 receiving copied values
-     *
-     * @returns {mat3} dest is specified, a new mat3 otherwise
-     */
-    mat4.toMat3 = function (mat, dest) {
-        if (!dest) { dest = mat3.create(); }
-
-        dest[0] = mat[0];
-        dest[1] = mat[1];
-        dest[2] = mat[2];
-        dest[3] = mat[4];
-        dest[4] = mat[5];
-        dest[5] = mat[6];
-        dest[6] = mat[8];
-        dest[7] = mat[9];
-        dest[8] = mat[10];
-
-        return dest;
-    };
+/**
+ * Computes the cross product of two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @returns {vec3} out
+ */
+vec3.cross = function(out, a, b) {
+    var ax = a[0], ay = a[1], az = a[2],
+        bx = b[0], by = b[1], bz = b[2];
 
-    /**
-     * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
-     * The resulting matrix is useful for calculating transformed normals
-     *
-     * Params:
-     * @param {mat4} mat mat4 containing values to invert and copy
-     * @param {mat3} [dest] mat3 receiving values
-     *
-     * @returns {mat3} dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
-     */
-    mat4.toInverseMat3 = function (mat, dest) {
-        // Cache the matrix values (makes for huge speed increases!)
-        var a00 = mat[0], a01 = mat[1], a02 = mat[2],
-            a10 = mat[4], a11 = mat[5], a12 = mat[6],
-            a20 = mat[8], a21 = mat[9], a22 = mat[10],
-
-            b01 = a22 * a11 - a12 * a21,
-            b11 = -a22 * a10 + a12 * a20,
-            b21 = a21 * a10 - a11 * a20,
-
-            d = a00 * b01 + a01 * b11 + a02 * b21,
-            id;
-
-        if (!d) { return null; }
-        id = 1 / d;
-
-        if (!dest) { dest = mat3.create(); }
-
-        dest[0] = b01 * id;
-        dest[1] = (-a22 * a01 + a02 * a21) * id;
-        dest[2] = (a12 * a01 - a02 * a11) * id;
-        dest[3] = b11 * id;
-        dest[4] = (a22 * a00 - a02 * a20) * id;
-        dest[5] = (-a12 * a00 + a02 * a10) * id;
-        dest[6] = b21 * id;
-        dest[7] = (-a21 * a00 + a01 * a20) * id;
-        dest[8] = (a11 * a00 - a01 * a10) * id;
-
-        return dest;
-    };
+    out[0] = ay * bz - az * by;
+    out[1] = az * bx - ax * bz;
+    out[2] = ax * by - ay * bx;
+    return out;
+};
 
-    /**
-     * Performs a matrix multiplication
-     *
-     * @param {mat4} mat First operand
-     * @param {mat4} mat2 Second operand
-     * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
-     *
-     * @returns {mat4} dest if specified, mat otherwise
-     */
-    mat4.multiply = function (mat, mat2, dest) {
-        if (!dest) { dest = mat; }
-
-        // Cache the matrix values (makes for huge speed increases!)
-        var a00 = mat[ 0], a01 = mat[ 1], a02 = mat[ 2], a03 = mat[3];
-        var a10 = mat[ 4], a11 = mat[ 5], a12 = mat[ 6], a13 = mat[7];
-        var a20 = mat[ 8], a21 = mat[ 9], a22 = mat[10], a23 = mat[11];
-        var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
-
-        // Cache only the current line of the second matrix
-        var b0  = mat2[0], b1 = mat2[1], b2 = mat2[2], b3 = mat2[3];  
-        dest[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
-        dest[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
-        dest[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
-        dest[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
-
-        b0 = mat2[4];
-        b1 = mat2[5];
-        b2 = mat2[6];
-        b3 = mat2[7];
-        dest[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
-        dest[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
-        dest[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
-        dest[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
-
-        b0 = mat2[8];
-        b1 = mat2[9];
-        b2 = mat2[10];
-        b3 = mat2[11];
-        dest[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
-        dest[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
-        dest[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
-        dest[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
-
-        b0 = mat2[12];
-        b1 = mat2[13];
-        b2 = mat2[14];
-        b3 = mat2[15];
-        dest[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
-        dest[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
-        dest[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
-        dest[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
-
-        return dest;
-    };
-
-    /**
-     * Transforms a vec3 with the given matrix
-     * 4th vector component is implicitly '1'
-     *
-     * @param {mat4} mat mat4 to transform the vector with
-     * @param {vec3} vec vec3 to transform
-     * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec3} dest if specified, vec otherwise
-     */
-    mat4.multiplyVec3 = function (mat, vec, dest) {
-        if (!dest) { dest = vec; }
-
-        var x = vec[0], y = vec[1], z = vec[2];
-
-        dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
-        dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
-        dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
-
-        return dest;
-    };
-
-    /**
-     * Transforms a vec4 with the given matrix
-     *
-     * @param {mat4} mat mat4 to transform the vector with
-     * @param {vec4} vec vec4 to transform
-     * @param {vec4} [dest] vec4 receiving operation result. If not specified result is written to vec
-     *
-     * @returns {vec4} dest if specified, vec otherwise
-     */
-    mat4.multiplyVec4 = function (mat, vec, dest) {
-        if (!dest) { dest = vec; }
-
-        var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
-
-        dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12] * w;
-        dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13] * w;
-        dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14] * w;
-        dest[3] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15] * w;
-
-        return dest;
-    };
-
-    /**
-     * Translates a matrix by the given vector
-     *
-     * @param {mat4} mat mat4 to translate
-     * @param {vec3} vec vec3 specifying the translation
-     * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
-     *
-     * @returns {mat4} dest if specified, mat otherwise
-     */
-    mat4.translate = function (mat, vec, dest) {
-        var x = vec[0], y = vec[1], z = vec[2],
-            a00, a01, a02, a03,
-            a10, a11, a12, a13,
-            a20, a21, a22, a23;
-
-        if (!dest || mat === dest) {
-            mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
-            mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
-            mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
-            mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
-            return mat;
-        }
-
-        a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
-        a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
-        a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
-
-        dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03;
-        dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13;
-        dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23;
+/**
+ * Performs a linear interpolation between two vec3's
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the first operand
+ * @param {vec3} b the second operand
+ * @param {Number} t interpolation amount between the two inputs
+ * @returns {vec3} out
+ */
+vec3.lerp = function (out, a, b, t) {
+    var ax = a[0],
+        ay = a[1],
+        az = a[2];
+    out[0] = ax + t * (b[0] - ax);
+    out[1] = ay + t * (b[1] - ay);
+    out[2] = az + t * (b[2] - az);
+    return out;
+};
 
-        dest[12] = a00 * x + a10 * y + a20 * z + mat[12];
-        dest[13] = a01 * x + a11 * y + a21 * z + mat[13];
-        dest[14] = a02 * x + a12 * y + a22 * z + mat[14];
-        dest[15] = a03 * x + a13 * y + a23 * z + mat[15];
-        return dest;
-    };
+/**
+ * Transforms the vec3 with a mat4.
+ * 4th vector component is implicitly '1'
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the vector to transform
+ * @param {mat4} m matrix to transform with
+ * @returns {vec3} out
+ */
+vec3.transformMat4 = function(out, a, m) {
+    var x = a[0], y = a[1], z = a[2];
+    out[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
+    out[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
+    out[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
+    return out;
+};
 
-    /**
-     * Scales a matrix by the given vector
-     *
-     * @param {mat4} mat mat4 to scale
-     * @param {vec3} vec vec3 specifying the scale for each axis
-     * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
-     *
-     * @param {mat4} dest if specified, mat otherwise
-     */
-    mat4.scale = function (mat, vec, dest) {
-        var x = vec[0], y = vec[1], z = vec[2];
-
-        if (!dest || mat === dest) {
-            mat[0] *= x;
-            mat[1] *= x;
-            mat[2] *= x;
-            mat[3] *= x;
-            mat[4] *= y;
-            mat[5] *= y;
-            mat[6] *= y;
-            mat[7] *= y;
-            mat[8] *= z;
-            mat[9] *= z;
-            mat[10] *= z;
-            mat[11] *= z;
-            return mat;
-        }
+/**
+ * Transforms the vec3 with a quat
+ *
+ * @param {vec3} out the receiving vector
+ * @param {vec3} a the vector to transform
+ * @param {quat} q quaternion to transform with
+ * @returns {vec3} out
+ */
+vec3.transformQuat = function(out, a, q) {
+    var x = a[0], y = a[1], z = a[2],
+        qx = q[0], qy = q[1], qz = q[2], qw = q[3],
+
+        // calculate quat * vec
+        ix = qw * x + qy * z - qz * y,
+        iy = qw * y + qz * x - qx * z,
+        iz = qw * z + qx * y - qy * x,
+        iw = -qx * x - qy * y - qz * z;
+
+    // calculate result * inverse quat
+    out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
+    out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
+    out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
+    return out;
+};
 
-        dest[0] = mat[0] * x;
-        dest[1] = mat[1] * x;
-        dest[2] = mat[2] * x;
-        dest[3] = mat[3] * x;
-        dest[4] = mat[4] * y;
-        dest[5] = mat[5] * y;
-        dest[6] = mat[6] * y;
-        dest[7] = mat[7] * y;
-        dest[8] = mat[8] * z;
-        dest[9] = mat[9] * z;
-        dest[10] = mat[10] * z;
-        dest[11] = mat[11] * z;
-        dest[12] = mat[12];
-        dest[13] = mat[13];
-        dest[14] = mat[14];
-        dest[15] = mat[15];
-        return dest;
-    };
+/**
+ * Perform some operation over an array of vec3s.
+ *
+ * @param {Array} a the array of vectors to iterate over
+ * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
+ * @param {Number} offset Number of elements to skip at the beginning of the array
+ * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
+ * @param {Function} fn Function to call for each vector in the array
+ * @param {Object} [arg] additional argument to pass to fn
+ * @returns {Array} a
+ * @function
+ */
+vec3.forEach = (function() {
+    var vec = vec3.create();
 
-    /**
-     * Rotates a matrix by the given angle around the specified axis
-     * If rotating around a primary axis (X,Y,Z) one of the specialized rotation functions should be used instead for performance
-     *
-     * @param {mat4} mat mat4 to rotate
-     * @param {number} angle Angle (in radians) to rotate
-     * @param {vec3} axis vec3 representing the axis to rotate around
-     * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
-     *
-     * @returns {mat4} dest if specified, mat otherwise
-     */
-    mat4.rotate = function (mat, angle, axis, dest) {
-        var x = axis[0], y = axis[1], z = axis[2],
-            len = Math.sqrt(x * x + y * y + z * z),
-            s, c, t,
-            a00, a01, a02, a03,
-            a10, a11, a12, a13,
-            a20, a21, a22, a23,
-            b00, b01, b02,
-            b10, b11, b12,
-            b20, b21, b22;