2 * @fileoverview gl-matrix - High performance matrix and vector operations for WebGL
3 * @author Brandon Jones
4 * @author Colin MacKenzie IV
9 * Copyright (c) 2012 Brandon Jones, Colin MacKenzie IV
11 * This software is provided 'as-is', without any express or implied
12 * warranty. In no event will the authors be held liable for any damages
13 * arising from the use of this software.
15 * Permission is granted to anyone to use this software for any purpose,
16 * including commercial applications, and to alter it and redistribute it
17 * freely, subject to the following restrictions:
19 * 1. The origin of this software must not be misrepresented; you must not
20 * claim that you wrote the original software. If you use this software
21 * in a product, an acknowledgment in the product documentation would be
22 * appreciated but is not required.
24 * 2. Altered source versions must be plainly marked as such, and must not
25 * be misrepresented as being the original software.
27 * 3. This notice may not be removed or altered from any source
31 // Updated to use a modification of the "returnExportsGlobal" pattern from https://github.com/umdjs/umd
33 (function (root, factory) {
34 if (typeof exports === 'object') {
35 // Node. Does not work with strict CommonJS, but
36 // only CommonJS-like enviroments that support module.exports,
38 module.exports = factory(global);
39 } else if (typeof define === 'function' && define.amd) {
40 // AMD. Register as an anonymous module.
41 define([], function () {
45 // Specific initialization for TIZEN Web UI Framework
46 root.initGlMatrix = function ( targetRoot ) {
47 factory( targetRoot || root );
50 }(this, function (root) {
53 // Tweak to your liking
54 var FLOAT_EPSILON = 0.000001;
58 if (typeof(Float32Array) != 'undefined') {
59 var y = new Float32Array(1);
60 var i = new Int32Array(y.buffer);
63 * Fast way to calculate the inverse square root,
64 * see http://jsperf.com/inverse-square-root/5
66 * If typed arrays are not available, a slower
67 * implementation will be used.
69 * @param {Number} number the number
70 * @returns {Number} Inverse square root
72 glMath.invsqrt = function(number) {
73 var x2 = number * 0.5;
77 i[0] = 0x5f3759df - (i[0] >> 1);
81 return number2 * (threehalfs - (x2 * number2 * number2));
84 glMath.invsqrt = function(number) { return 1.0 / Math.sqrt(number); };
89 * @class System-specific optimal array type
92 var MatrixArray = null;
94 // explicitly sets and returns the type of array to use within glMatrix
95 function setMatrixArrayType(type) {
100 // auto-detects and returns the best type of array to use within glMatrix, falling
101 // back to Array if typed arrays are unsupported
102 function determineMatrixArrayType() {
103 MatrixArray = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
107 determineMatrixArrayType();
110 * @class 3 Dimensional Vector
116 * Creates a new instance of a vec3 using the default array type
117 * Any javascript array-like objects containing at least 3 numeric elements can serve as a vec3
119 * @param {vec3} [vec] vec3 containing values to initialize with
121 * @returns {vec3} New vec3
123 vec3.create = function (vec) {
124 var dest = new MatrixArray(3);
131 dest[0] = dest[1] = dest[2] = 0;
138 * Creates a new instance of a vec3, initializing it with the given arguments
140 * @param {number} x X value
141 * @param {number} y Y value
142 * @param {number} z Z value
144 * @returns {vec3} New vec3
146 vec3.createFrom = function (x, y, z) {
147 var dest = new MatrixArray(3);
157 * Copies the values of one vec3 to another
159 * @param {vec3} vec vec3 containing values to copy
160 * @param {vec3} dest vec3 receiving copied values
162 * @returns {vec3} dest
164 vec3.set = function (vec, dest) {
173 * Compares two vectors for equality within a certain margin of error
175 * @param {vec3} a First vector
176 * @param {vec3} b Second vector
178 * @returns {Boolean} True if a is equivalent to b
180 vec3.equal = function (a, b) {
182 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
183 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
184 Math.abs(a[2] - b[2]) < FLOAT_EPSILON
189 * Performs a vector addition
191 * @param {vec3} vec First operand
192 * @param {vec3} vec2 Second operand
193 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
195 * @returns {vec3} dest if specified, vec otherwise
197 vec3.add = function (vec, vec2, dest) {
198 if (!dest || vec === dest) {
205 dest[0] = vec[0] + vec2[0];
206 dest[1] = vec[1] + vec2[1];
207 dest[2] = vec[2] + vec2[2];
212 * Performs a vector subtraction
214 * @param {vec3} vec First operand
215 * @param {vec3} vec2 Second operand
216 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
218 * @returns {vec3} dest if specified, vec otherwise
220 vec3.subtract = function (vec, vec2, dest) {
221 if (!dest || vec === dest) {
228 dest[0] = vec[0] - vec2[0];
229 dest[1] = vec[1] - vec2[1];
230 dest[2] = vec[2] - vec2[2];
235 * Performs a vector multiplication
237 * @param {vec3} vec First operand
238 * @param {vec3} vec2 Second operand
239 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
241 * @returns {vec3} dest if specified, vec otherwise
243 vec3.multiply = function (vec, vec2, dest) {
244 if (!dest || vec === dest) {
251 dest[0] = vec[0] * vec2[0];
252 dest[1] = vec[1] * vec2[1];
253 dest[2] = vec[2] * vec2[2];
258 * Negates the components of a vec3
260 * @param {vec3} vec vec3 to negate
261 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
263 * @returns {vec3} dest if specified, vec otherwise
265 vec3.negate = function (vec, dest) {
266 if (!dest) { dest = vec; }
275 * Multiplies the components of a vec3 by a scalar value
277 * @param {vec3} vec vec3 to scale
278 * @param {number} val Value to scale by
279 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
281 * @returns {vec3} dest if specified, vec otherwise
283 vec3.scale = function (vec, val, dest) {
284 if (!dest || vec === dest) {
291 dest[0] = vec[0] * val;
292 dest[1] = vec[1] * val;
293 dest[2] = vec[2] * val;
298 * Generates a unit vector of the same direction as the provided vec3
299 * If vector length is 0, returns [0, 0, 0]
301 * @param {vec3} vec vec3 to normalize
302 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
304 * @returns {vec3} dest if specified, vec otherwise
306 vec3.normalize = function (vec, dest) {
307 if (!dest) { dest = vec; }
309 var x = vec[0], y = vec[1], z = vec[2],
310 len = Math.sqrt(x * x + y * y + z * z);
317 } else if (len === 1) {
332 * Generates the cross product of two vec3s
334 * @param {vec3} vec First operand
335 * @param {vec3} vec2 Second operand
336 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
338 * @returns {vec3} dest if specified, vec otherwise
340 vec3.cross = function (vec, vec2, dest) {
341 if (!dest) { dest = vec; }
343 var x = vec[0], y = vec[1], z = vec[2],
344 x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
346 dest[0] = y * z2 - z * y2;
347 dest[1] = z * x2 - x * z2;
348 dest[2] = x * y2 - y * x2;
353 * Caclulates the length of a vec3
355 * @param {vec3} vec vec3 to calculate length of
357 * @returns {number} Length of vec
359 vec3.length = function (vec) {
360 var x = vec[0], y = vec[1], z = vec[2];
361 return Math.sqrt(x * x + y * y + z * z);
365 * Caclulates the squared length of a vec3
367 * @param {vec3} vec vec3 to calculate squared length of
369 * @returns {number} Squared Length of vec
371 vec3.squaredLength = function (vec) {
372 var x = vec[0], y = vec[1], z = vec[2];
373 return x * x + y * y + z * z;
377 * Caclulates the dot product of two vec3s
379 * @param {vec3} vec First operand
380 * @param {vec3} vec2 Second operand
382 * @returns {number} Dot product of vec and vec2
384 vec3.dot = function (vec, vec2) {
385 return vec[0] * vec2[0] + vec[1] * vec2[1] + vec[2] * vec2[2];
389 * Generates a unit vector pointing from one vector to another
391 * @param {vec3} vec Origin vec3
392 * @param {vec3} vec2 vec3 to point to
393 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
395 * @returns {vec3} dest if specified, vec otherwise
397 vec3.direction = function (vec, vec2, dest) {
398 if (!dest) { dest = vec; }
400 var x = vec[0] - vec2[0],
401 y = vec[1] - vec2[1],
402 z = vec[2] - vec2[2],
403 len = Math.sqrt(x * x + y * y + z * z);
420 * Performs a linear interpolation between two vec3
422 * @param {vec3} vec First vector
423 * @param {vec3} vec2 Second vector
424 * @param {number} lerp Interpolation amount between the two inputs
425 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
427 * @returns {vec3} dest if specified, vec otherwise
429 vec3.lerp = function (vec, vec2, lerp, dest) {
430 if (!dest) { dest = vec; }
432 dest[0] = vec[0] + lerp * (vec2[0] - vec[0]);
433 dest[1] = vec[1] + lerp * (vec2[1] - vec[1]);
434 dest[2] = vec[2] + lerp * (vec2[2] - vec[2]);
440 * Calculates the euclidian distance between two vec3
443 * @param {vec3} vec First vector
444 * @param {vec3} vec2 Second vector
446 * @returns {number} Distance between vec and vec2
448 vec3.dist = function (vec, vec2) {
449 var x = vec2[0] - vec[0],
450 y = vec2[1] - vec[1],
451 z = vec2[2] - vec[2];
453 return Math.sqrt(x*x + y*y + z*z);
456 // Pre-allocated to prevent unecessary garbage collection
457 var unprojectMat = null;
458 var unprojectVec = new MatrixArray(4);
460 * Projects the specified vec3 from screen space into object space
461 * 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>
463 * @param {vec3} vec Screen-space vector to project
464 * @param {mat4} view View matrix
465 * @param {mat4} proj Projection matrix
466 * @param {vec4} viewport Viewport as given to gl.viewport [x, y, width, height]
467 * @param {vec3} [dest] vec3 receiving unprojected result. If not specified result is written to vec
469 * @returns {vec3} dest if specified, vec otherwise
471 vec3.unproject = function (vec, view, proj, viewport, dest) {
472 if (!dest) { dest = vec; }
475 unprojectMat = mat4.create();
478 var m = unprojectMat;
479 var v = unprojectVec;
481 v[0] = (vec[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
482 v[1] = (vec[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
483 v[2] = 2.0 * vec[2] - 1.0;
486 mat4.multiply(proj, view, m);
487 if(!mat4.inverse(m)) { return null; }
489 mat4.multiplyVec4(m, v);
490 if(v[3] === 0.0) { return null; }
492 dest[0] = v[0] / v[3];
493 dest[1] = v[1] / v[3];
494 dest[2] = v[2] / v[3];
499 var xUnitVec3 = vec3.createFrom(1,0,0);
500 var yUnitVec3 = vec3.createFrom(0,1,0);
501 var zUnitVec3 = vec3.createFrom(0,0,1);
503 var tmpvec3 = vec3.create();
505 * Generates a quaternion of rotation between two given normalized vectors
507 * @param {vec3} a Normalized source vector
508 * @param {vec3} b Normalized target vector
509 * @param {quat4} [dest] quat4 receiving operation result.
511 * @returns {quat4} dest if specified, a new quat4 otherwise
513 vec3.rotationTo = function (a, b, dest) {
514 if (!dest) { dest = quat4.create(); }
516 var d = vec3.dot(a, b);
519 quat4.set(identityQuat4, dest);
520 } else if (d < (0.000001 - 1.0)) {
521 vec3.cross(xUnitVec3, a, axis);
522 if (vec3.length(axis) < 0.000001)
523 vec3.cross(yUnitVec3, a, axis);
524 if (vec3.length(axis) < 0.000001)
525 vec3.cross(zUnitVec3, a, axis);
526 vec3.normalize(axis);
527 quat4.fromAngleAxis(Math.PI, axis, dest);
529 var s = Math.sqrt((1.0 + d) * 2.0);
531 vec3.cross(a, b, axis);
532 dest[0] = axis[0] * sInv;
533 dest[1] = axis[1] * sInv;
534 dest[2] = axis[2] * sInv;
536 quat4.normalize(dest);
538 if (dest[3] > 1.0) dest[3] = 1.0;
539 else if (dest[3] < -1.0) dest[3] = -1.0;
544 * Returns a string representation of a vector
546 * @param {vec3} vec Vector to represent as a string
548 * @returns {string} String representation of vec
550 vec3.str = function (vec) {
551 return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ']';
561 * Creates a new instance of a mat3 using the default array type
562 * Any javascript array-like object containing at least 9 numeric elements can serve as a mat3
564 * @param {mat3} [mat] mat3 containing values to initialize with
566 * @returns {mat3} New mat3
568 mat3.create = function (mat) {
569 var dest = new MatrixArray(9);
593 * Creates a new instance of a mat3, initializing it with the given arguments
595 * @param {number} m00
596 * @param {number} m01
597 * @param {number} m02
598 * @param {number} m10
599 * @param {number} m11
600 * @param {number} m12
601 * @param {number} m20
602 * @param {number} m21
603 * @param {number} m22
605 * @returns {mat3} New mat3
607 mat3.createFrom = function (m00, m01, m02, m10, m11, m12, m20, m21, m22) {
608 var dest = new MatrixArray(9);
624 * Calculates the determinant of a mat3
626 * @param {mat3} mat mat3 to calculate determinant of
628 * @returns {Number} determinant of mat
630 mat3.determinant = function (mat) {
631 var a00 = mat[0], a01 = mat[1], a02 = mat[2],
632 a10 = mat[3], a11 = mat[4], a12 = mat[5],
633 a20 = mat[6], a21 = mat[7], a22 = mat[8];
635 return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
639 * Calculates the inverse matrix of a mat3
641 * @param {mat3} mat mat3 to calculate inverse of
642 * @param {mat3} [dest] mat3 receiving inverse matrix. If not specified result is written to mat
644 * @param {mat3} dest is specified, mat otherwise, null if matrix cannot be inverted
646 mat3.inverse = function (mat, dest) {
647 var a00 = mat[0], a01 = mat[1], a02 = mat[2],
648 a10 = mat[3], a11 = mat[4], a12 = mat[5],
649 a20 = mat[6], a21 = mat[7], a22 = mat[8],
651 b01 = a22 * a11 - a12 * a21,
652 b11 = -a22 * a10 + a12 * a20,
653 b21 = a21 * a10 - a11 * a20,
655 d = a00 * b01 + a01 * b11 + a02 * b21,
658 if (!d) { return null; }
661 if (!dest) { dest = mat3.create(); }
664 dest[1] = (-a22 * a01 + a02 * a21) * id;
665 dest[2] = (a12 * a01 - a02 * a11) * id;
667 dest[4] = (a22 * a00 - a02 * a20) * id;
668 dest[5] = (-a12 * a00 + a02 * a10) * id;
670 dest[7] = (-a21 * a00 + a01 * a20) * id;
671 dest[8] = (a11 * a00 - a01 * a10) * id;
676 * Performs a matrix multiplication
678 * @param {mat3} mat First operand
679 * @param {mat3} mat2 Second operand
680 * @param {mat3} [dest] mat3 receiving operation result. If not specified result is written to mat
682 * @returns {mat3} dest if specified, mat otherwise
684 mat3.multiply = function (mat, mat2, dest) {
685 if (!dest) { dest = mat; }
688 // Cache the matrix values (makes for huge speed increases!)
689 var a00 = mat[0], a01 = mat[1], a02 = mat[2],
690 a10 = mat[3], a11 = mat[4], a12 = mat[5],
691 a20 = mat[6], a21 = mat[7], a22 = mat[8],
693 b00 = mat2[0], b01 = mat2[1], b02 = mat2[2],
694 b10 = mat2[3], b11 = mat2[4], b12 = mat2[5],
695 b20 = mat2[6], b21 = mat2[7], b22 = mat2[8];
697 dest[0] = b00 * a00 + b01 * a10 + b02 * a20;
698 dest[1] = b00 * a01 + b01 * a11 + b02 * a21;
699 dest[2] = b00 * a02 + b01 * a12 + b02 * a22;
701 dest[3] = b10 * a00 + b11 * a10 + b12 * a20;
702 dest[4] = b10 * a01 + b11 * a11 + b12 * a21;
703 dest[5] = b10 * a02 + b11 * a12 + b12 * a22;
705 dest[6] = b20 * a00 + b21 * a10 + b22 * a20;
706 dest[7] = b20 * a01 + b21 * a11 + b22 * a21;
707 dest[8] = b20 * a02 + b21 * a12 + b22 * a22;
713 * Transforms the vec2 according to the given mat3.
715 * @param {mat3} matrix mat3 to multiply against
716 * @param {vec2} vec the vector to multiply
717 * @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
719 * @returns {vec2} The multiplication result
721 mat3.multiplyVec2 = function(matrix, vec, dest) {
722 if (!dest) dest = vec;
723 var x = vec[0], y = vec[1];
724 dest[0] = x * matrix[0] + y * matrix[3] + matrix[6];
725 dest[1] = x * matrix[1] + y * matrix[4] + matrix[7];
730 * Transforms the vec3 according to the given mat3
732 * @param {mat3} matrix mat3 to multiply against
733 * @param {vec3} vec the vector to multiply
734 * @param {vec3} [dest] an optional receiving vector. If not given, vec is used.
736 * @returns {vec3} The multiplication result
738 mat3.multiplyVec3 = function(matrix, vec, dest) {
739 if (!dest) dest = vec;
740 var x = vec[0], y = vec[1], z = vec[2];
741 dest[0] = x * matrix[0] + y * matrix[3] + z * matrix[6];
742 dest[1] = x * matrix[1] + y * matrix[4] + z * matrix[7];
743 dest[2] = x * matrix[2] + y * matrix[5] + z * matrix[8];
749 * Copies the values of one mat3 to another
751 * @param {mat3} mat mat3 containing values to copy
752 * @param {mat3} dest mat3 receiving copied values
754 * @returns {mat3} dest
756 mat3.set = function (mat, dest) {
770 * Compares two matrices for equality within a certain margin of error
772 * @param {mat3} a First matrix
773 * @param {mat3} b Second matrix
775 * @returns {Boolean} True if a is equivalent to b
777 mat3.equal = function (a, b) {
779 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
780 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
781 Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
782 Math.abs(a[3] - b[3]) < FLOAT_EPSILON &&
783 Math.abs(a[4] - b[4]) < FLOAT_EPSILON &&
784 Math.abs(a[5] - b[5]) < FLOAT_EPSILON &&
785 Math.abs(a[6] - b[6]) < FLOAT_EPSILON &&
786 Math.abs(a[7] - b[7]) < FLOAT_EPSILON &&
787 Math.abs(a[8] - b[8]) < FLOAT_EPSILON
792 * Sets a mat3 to an identity matrix
794 * @param {mat3} dest mat3 to set
796 * @returns dest if specified, otherwise a new mat3
798 mat3.identity = function (dest) {
799 if (!dest) { dest = mat3.create(); }
813 * Transposes a mat3 (flips the values over the diagonal)
816 * @param {mat3} mat mat3 to transpose
817 * @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
819 * @returns {mat3} dest is specified, mat otherwise
821 mat3.transpose = function (mat, dest) {
822 // If we are transposing ourselves we can skip a few steps but have to cache some values
823 if (!dest || mat === dest) {
824 var a01 = mat[1], a02 = mat[2],
849 * Copies the elements of a mat3 into the upper 3x3 elements of a mat4
851 * @param {mat3} mat mat3 containing values to copy
852 * @param {mat4} [dest] mat4 receiving copied values
854 * @returns {mat4} dest if specified, a new mat4 otherwise
856 mat3.toMat4 = function (mat, dest) {
857 if (!dest) { dest = mat4.create(); }
883 * Returns a string representation of a mat3
885 * @param {mat3} mat mat3 to represent as a string
887 * @param {string} String representation of mat
889 mat3.str = function (mat) {
890 return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] +
891 ', ' + mat[3] + ', ' + mat[4] + ', ' + mat[5] +
892 ', ' + mat[6] + ', ' + mat[7] + ', ' + mat[8] + ']';
902 * Creates a new instance of a mat4 using the default array type
903 * Any javascript array-like object containing at least 16 numeric elements can serve as a mat4
905 * @param {mat4} [mat] mat4 containing values to initialize with
907 * @returns {mat4} New mat4
909 mat4.create = function (mat) {
910 var dest = new MatrixArray(16);
935 * Creates a new instance of a mat4, initializing it with the given arguments
937 * @param {number} m00
938 * @param {number} m01
939 * @param {number} m02
940 * @param {number} m03
941 * @param {number} m10
942 * @param {number} m11
943 * @param {number} m12
944 * @param {number} m13
945 * @param {number} m20
946 * @param {number} m21
947 * @param {number} m22
948 * @param {number} m23
949 * @param {number} m30
950 * @param {number} m31
951 * @param {number} m32
952 * @param {number} m33
954 * @returns {mat4} New mat4
956 mat4.createFrom = function (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
957 var dest = new MatrixArray(16);
980 * Copies the values of one mat4 to another
982 * @param {mat4} mat mat4 containing values to copy
983 * @param {mat4} dest mat4 receiving copied values
985 * @returns {mat4} dest
987 mat4.set = function (mat, dest) {
1008 * Compares two matrices for equality within a certain margin of error
1010 * @param {mat4} a First matrix
1011 * @param {mat4} b Second matrix
1013 * @returns {Boolean} True if a is equivalent to b
1015 mat4.equal = function (a, b) {
1017 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
1018 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
1019 Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
1020 Math.abs(a[3] - b[3]) < FLOAT_EPSILON &&
1021 Math.abs(a[4] - b[4]) < FLOAT_EPSILON &&
1022 Math.abs(a[5] - b[5]) < FLOAT_EPSILON &&
1023 Math.abs(a[6] - b[6]) < FLOAT_EPSILON &&
1024 Math.abs(a[7] - b[7]) < FLOAT_EPSILON &&
1025 Math.abs(a[8] - b[8]) < FLOAT_EPSILON &&
1026 Math.abs(a[9] - b[9]) < FLOAT_EPSILON &&
1027 Math.abs(a[10] - b[10]) < FLOAT_EPSILON &&
1028 Math.abs(a[11] - b[11]) < FLOAT_EPSILON &&
1029 Math.abs(a[12] - b[12]) < FLOAT_EPSILON &&
1030 Math.abs(a[13] - b[13]) < FLOAT_EPSILON &&
1031 Math.abs(a[14] - b[14]) < FLOAT_EPSILON &&
1032 Math.abs(a[15] - b[15]) < FLOAT_EPSILON
1037 * Sets a mat4 to an identity matrix
1039 * @param {mat4} dest mat4 to set
1041 * @returns {mat4} dest
1043 mat4.identity = function (dest) {
1044 if (!dest) { dest = mat4.create(); }
1065 * Transposes a mat4 (flips the values over the diagonal)
1067 * @param {mat4} mat mat4 to transpose
1068 * @param {mat4} [dest] mat4 receiving transposed values. If not specified result is written to mat
1070 * @param {mat4} dest is specified, mat otherwise
1072 mat4.transpose = function (mat, dest) {
1073 // If we are transposing ourselves we can skip a few steps but have to cache some values
1074 if (!dest || mat === dest) {
1075 var a01 = mat[1], a02 = mat[2], a03 = mat[3],
1076 a12 = mat[6], a13 = mat[7],
1114 * Calculates the determinant of a mat4
1116 * @param {mat4} mat mat4 to calculate determinant of
1118 * @returns {number} determinant of mat
1120 mat4.determinant = function (mat) {
1121 // Cache the matrix values (makes for huge speed increases!)
1122 var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
1123 a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
1124 a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
1125 a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
1127 return (a30 * a21 * a12 * a03 - a20 * a31 * a12 * a03 - a30 * a11 * a22 * a03 + a10 * a31 * a22 * a03 +
1128 a20 * a11 * a32 * a03 - a10 * a21 * a32 * a03 - a30 * a21 * a02 * a13 + a20 * a31 * a02 * a13 +
1129 a30 * a01 * a22 * a13 - a00 * a31 * a22 * a13 - a20 * a01 * a32 * a13 + a00 * a21 * a32 * a13 +
1130 a30 * a11 * a02 * a23 - a10 * a31 * a02 * a23 - a30 * a01 * a12 * a23 + a00 * a31 * a12 * a23 +
1131 a10 * a01 * a32 * a23 - a00 * a11 * a32 * a23 - a20 * a11 * a02 * a33 + a10 * a21 * a02 * a33 +
1132 a20 * a01 * a12 * a33 - a00 * a21 * a12 * a33 - a10 * a01 * a22 * a33 + a00 * a11 * a22 * a33);
1136 * Calculates the inverse matrix of a mat4
1138 * @param {mat4} mat mat4 to calculate inverse of
1139 * @param {mat4} [dest] mat4 receiving inverse matrix. If not specified result is written to mat
1141 * @param {mat4} dest is specified, mat otherwise, null if matrix cannot be inverted
1143 mat4.inverse = function (mat, dest) {
1144 if (!dest) { dest = mat; }
1146 // Cache the matrix values (makes for huge speed increases!)
1147 var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
1148 a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
1149 a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
1150 a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
1152 b00 = a00 * a11 - a01 * a10,
1153 b01 = a00 * a12 - a02 * a10,
1154 b02 = a00 * a13 - a03 * a10,
1155 b03 = a01 * a12 - a02 * a11,
1156 b04 = a01 * a13 - a03 * a11,
1157 b05 = a02 * a13 - a03 * a12,
1158 b06 = a20 * a31 - a21 * a30,
1159 b07 = a20 * a32 - a22 * a30,
1160 b08 = a20 * a33 - a23 * a30,
1161 b09 = a21 * a32 - a22 * a31,
1162 b10 = a21 * a33 - a23 * a31,
1163 b11 = a22 * a33 - a23 * a32,
1165 d = (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06),
1168 // Calculate the determinant
1169 if (!d) { return null; }
1172 dest[0] = (a11 * b11 - a12 * b10 + a13 * b09) * invDet;
1173 dest[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
1174 dest[2] = (a31 * b05 - a32 * b04 + a33 * b03) * invDet;
1175 dest[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
1176 dest[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
1177 dest[5] = (a00 * b11 - a02 * b08 + a03 * b07) * invDet;
1178 dest[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
1179 dest[7] = (a20 * b05 - a22 * b02 + a23 * b01) * invDet;
1180 dest[8] = (a10 * b10 - a11 * b08 + a13 * b06) * invDet;
1181 dest[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
1182 dest[10] = (a30 * b04 - a31 * b02 + a33 * b00) * invDet;
1183 dest[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
1184 dest[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
1185 dest[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet;
1186 dest[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
1187 dest[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet;
1193 * Copies the upper 3x3 elements of a mat4 into another mat4
1195 * @param {mat4} mat mat4 containing values to copy
1196 * @param {mat4} [dest] mat4 receiving copied values
1198 * @returns {mat4} dest is specified, a new mat4 otherwise
1200 mat4.toRotationMat = function (mat, dest) {
1201 if (!dest) { dest = mat4.create(); }
1224 * Copies the upper 3x3 elements of a mat4 into a mat3
1226 * @param {mat4} mat mat4 containing values to copy
1227 * @param {mat3} [dest] mat3 receiving copied values
1229 * @returns {mat3} dest is specified, a new mat3 otherwise
1231 mat4.toMat3 = function (mat, dest) {
1232 if (!dest) { dest = mat3.create(); }
1248 * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
1249 * The resulting matrix is useful for calculating transformed normals
1252 * @param {mat4} mat mat4 containing values to invert and copy
1253 * @param {mat3} [dest] mat3 receiving values
1255 * @returns {mat3} dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
1257 mat4.toInverseMat3 = function (mat, dest) {
1258 // Cache the matrix values (makes for huge speed increases!)
1259 var a00 = mat[0], a01 = mat[1], a02 = mat[2],
1260 a10 = mat[4], a11 = mat[5], a12 = mat[6],
1261 a20 = mat[8], a21 = mat[9], a22 = mat[10],
1263 b01 = a22 * a11 - a12 * a21,
1264 b11 = -a22 * a10 + a12 * a20,
1265 b21 = a21 * a10 - a11 * a20,
1267 d = a00 * b01 + a01 * b11 + a02 * b21,
1270 if (!d) { return null; }
1273 if (!dest) { dest = mat3.create(); }
1276 dest[1] = (-a22 * a01 + a02 * a21) * id;
1277 dest[2] = (a12 * a01 - a02 * a11) * id;
1279 dest[4] = (a22 * a00 - a02 * a20) * id;
1280 dest[5] = (-a12 * a00 + a02 * a10) * id;
1282 dest[7] = (-a21 * a00 + a01 * a20) * id;
1283 dest[8] = (a11 * a00 - a01 * a10) * id;
1289 * Performs a matrix multiplication
1291 * @param {mat4} mat First operand
1292 * @param {mat4} mat2 Second operand
1293 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1295 * @returns {mat4} dest if specified, mat otherwise
1297 mat4.multiply = function (mat, mat2, dest) {
1298 if (!dest) { dest = mat; }
1300 // Cache the matrix values (makes for huge speed increases!)
1301 var a00 = mat[ 0], a01 = mat[ 1], a02 = mat[ 2], a03 = mat[3];
1302 var a10 = mat[ 4], a11 = mat[ 5], a12 = mat[ 6], a13 = mat[7];
1303 var a20 = mat[ 8], a21 = mat[ 9], a22 = mat[10], a23 = mat[11];
1304 var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
1306 // Cache only the current line of the second matrix
1307 var b0 = mat2[0], b1 = mat2[1], b2 = mat2[2], b3 = mat2[3];
1308 dest[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
1309 dest[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
1310 dest[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
1311 dest[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
1317 dest[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
1318 dest[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
1319 dest[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
1320 dest[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
1326 dest[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
1327 dest[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
1328 dest[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
1329 dest[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
1335 dest[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
1336 dest[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
1337 dest[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
1338 dest[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
1344 * Transforms a vec3 with the given matrix
1345 * 4th vector component is implicitly '1'
1347 * @param {mat4} mat mat4 to transform the vector with
1348 * @param {vec3} vec vec3 to transform
1349 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
1351 * @returns {vec3} dest if specified, vec otherwise
1353 mat4.multiplyVec3 = function (mat, vec, dest) {
1354 if (!dest) { dest = vec; }
1356 var x = vec[0], y = vec[1], z = vec[2];
1358 dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
1359 dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
1360 dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
1366 * Transforms a vec4 with the given matrix
1368 * @param {mat4} mat mat4 to transform the vector with
1369 * @param {vec4} vec vec4 to transform
1370 * @param {vec4} [dest] vec4 receiving operation result. If not specified result is written to vec
1372 * @returns {vec4} dest if specified, vec otherwise
1374 mat4.multiplyVec4 = function (mat, vec, dest) {
1375 if (!dest) { dest = vec; }
1377 var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
1379 dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12] * w;
1380 dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13] * w;
1381 dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14] * w;
1382 dest[3] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15] * w;
1388 * Translates a matrix by the given vector
1390 * @param {mat4} mat mat4 to translate
1391 * @param {vec3} vec vec3 specifying the translation
1392 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1394 * @returns {mat4} dest if specified, mat otherwise
1396 mat4.translate = function (mat, vec, dest) {
1397 var x = vec[0], y = vec[1], z = vec[2],
1402 if (!dest || mat === dest) {
1403 mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
1404 mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
1405 mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
1406 mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
1410 a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
1411 a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
1412 a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
1414 dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03;
1415 dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13;
1416 dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23;
1418 dest[12] = a00 * x + a10 * y + a20 * z + mat[12];
1419 dest[13] = a01 * x + a11 * y + a21 * z + mat[13];
1420 dest[14] = a02 * x + a12 * y + a22 * z + mat[14];
1421 dest[15] = a03 * x + a13 * y + a23 * z + mat[15];
1426 * Scales a matrix by the given vector
1428 * @param {mat4} mat mat4 to scale
1429 * @param {vec3} vec vec3 specifying the scale for each axis
1430 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1432 * @param {mat4} dest if specified, mat otherwise
1434 mat4.scale = function (mat, vec, dest) {
1435 var x = vec[0], y = vec[1], z = vec[2];
1437 if (!dest || mat === dest) {
1453 dest[0] = mat[0] * x;
1454 dest[1] = mat[1] * x;
1455 dest[2] = mat[2] * x;
1456 dest[3] = mat[3] * x;
1457 dest[4] = mat[4] * y;
1458 dest[5] = mat[5] * y;
1459 dest[6] = mat[6] * y;
1460 dest[7] = mat[7] * y;
1461 dest[8] = mat[8] * z;
1462 dest[9] = mat[9] * z;
1463 dest[10] = mat[10] * z;
1464 dest[11] = mat[11] * z;
1473 * Rotates a matrix by the given angle around the specified axis
1474 * If rotating around a primary axis (X,Y,Z) one of the specialized rotation functions should be used instead for performance
1476 * @param {mat4} mat mat4 to rotate
1477 * @param {number} angle Angle (in radians) to rotate
1478 * @param {vec3} axis vec3 representing the axis to rotate around
1479 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1481 * @returns {mat4} dest if specified, mat otherwise
1483 mat4.rotate = function (mat, angle, axis, dest) {
1484 var x = axis[0], y = axis[1], z = axis[2],
1485 len = Math.sqrt(x * x + y * y + z * z),
1494 if (!len) { return null; }
1502 s = Math.sin(angle);
1503 c = Math.cos(angle);
1506 a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
1507 a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
1508 a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
1510 // Construct the elements of the rotation matrix
1511 b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
1512 b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
1513 b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
1517 } else if (mat !== dest) { // If the source and destination differ, copy the unchanged last row
1524 // Perform rotation-specific matrix multiplication
1525 dest[0] = a00 * b00 + a10 * b01 + a20 * b02;
1526 dest[1] = a01 * b00 + a11 * b01 + a21 * b02;
1527 dest[2] = a02 * b00 + a12 * b01 + a22 * b02;
1528 dest[3] = a03 * b00 + a13 * b01 + a23 * b02;
1530 dest[4] = a00 * b10 + a10 * b11 + a20 * b12;
1531 dest[5] = a01 * b10 + a11 * b11 + a21 * b12;
1532 dest[6] = a02 * b10 + a12 * b11 + a22 * b12;
1533 dest[7] = a03 * b10 + a13 * b11 + a23 * b12;
1535 dest[8] = a00 * b20 + a10 * b21 + a20 * b22;
1536 dest[9] = a01 * b20 + a11 * b21 + a21 * b22;
1537 dest[10] = a02 * b20 + a12 * b21 + a22 * b22;
1538 dest[11] = a03 * b20 + a13 * b21 + a23 * b22;
1543 * Rotates a matrix by the given angle around the X axis
1545 * @param {mat4} mat mat4 to rotate
1546 * @param {number} angle Angle (in radians) to rotate
1547 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1549 * @returns {mat4} dest if specified, mat otherwise
1551 mat4.rotateX = function (mat, angle, dest) {
1552 var s = Math.sin(angle),
1553 c = Math.cos(angle),
1565 } else if (mat !== dest) { // If the source and destination differ, copy the unchanged rows
1577 // Perform axis-specific matrix multiplication
1578 dest[4] = a10 * c + a20 * s;
1579 dest[5] = a11 * c + a21 * s;
1580 dest[6] = a12 * c + a22 * s;
1581 dest[7] = a13 * c + a23 * s;
1583 dest[8] = a10 * -s + a20 * c;
1584 dest[9] = a11 * -s + a21 * c;
1585 dest[10] = a12 * -s + a22 * c;
1586 dest[11] = a13 * -s + a23 * c;
1591 * Rotates a matrix by the given angle around the Y axis
1593 * @param {mat4} mat mat4 to rotate
1594 * @param {number} angle Angle (in radians) to rotate
1595 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1597 * @returns {mat4} dest if specified, mat otherwise
1599 mat4.rotateY = function (mat, angle, dest) {
1600 var s = Math.sin(angle),
1601 c = Math.cos(angle),
1613 } else if (mat !== dest) { // If the source and destination differ, copy the unchanged rows
1625 // Perform axis-specific matrix multiplication
1626 dest[0] = a00 * c + a20 * -s;
1627 dest[1] = a01 * c + a21 * -s;
1628 dest[2] = a02 * c + a22 * -s;
1629 dest[3] = a03 * c + a23 * -s;
1631 dest[8] = a00 * s + a20 * c;
1632 dest[9] = a01 * s + a21 * c;
1633 dest[10] = a02 * s + a22 * c;
1634 dest[11] = a03 * s + a23 * c;
1639 * Rotates a matrix by the given angle around the Z axis
1641 * @param {mat4} mat mat4 to rotate
1642 * @param {number} angle Angle (in radians) to rotate
1643 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
1645 * @returns {mat4} dest if specified, mat otherwise
1647 mat4.rotateZ = function (mat, angle, dest) {
1648 var s = Math.sin(angle),
1649 c = Math.cos(angle),
1661 } else if (mat !== dest) { // If the source and destination differ, copy the unchanged last row
1673 // Perform axis-specific matrix multiplication
1674 dest[0] = a00 * c + a10 * s;
1675 dest[1] = a01 * c + a11 * s;
1676 dest[2] = a02 * c + a12 * s;
1677 dest[3] = a03 * c + a13 * s;
1679 dest[4] = a00 * -s + a10 * c;
1680 dest[5] = a01 * -s + a11 * c;
1681 dest[6] = a02 * -s + a12 * c;
1682 dest[7] = a03 * -s + a13 * c;
1688 * Generates a frustum matrix with the given bounds
1690 * @param {number} left Left bound of the frustum
1691 * @param {number} right Right bound of the frustum
1692 * @param {number} bottom Bottom bound of the frustum
1693 * @param {number} top Top bound of the frustum
1694 * @param {number} near Near bound of the frustum
1695 * @param {number} far Far bound of the frustum
1696 * @param {mat4} [dest] mat4 frustum matrix will be written into
1698 * @returns {mat4} dest if specified, a new mat4 otherwise
1700 mat4.frustum = function (left, right, bottom, top, near, far, dest) {
1701 if (!dest) { dest = mat4.create(); }
1702 var rl = (right - left),
1703 tb = (top - bottom),
1705 dest[0] = (near * 2) / rl;
1710 dest[5] = (near * 2) / tb;
1713 dest[8] = (right + left) / rl;
1714 dest[9] = (top + bottom) / tb;
1715 dest[10] = -(far + near) / fn;
1719 dest[14] = -(far * near * 2) / fn;
1725 * Generates a perspective projection matrix with the given bounds
1727 * @param {number} fovy Vertical field of view
1728 * @param {number} aspect Aspect ratio. typically viewport width/height
1729 * @param {number} near Near bound of the frustum
1730 * @param {number} far Far bound of the frustum
1731 * @param {mat4} [dest] mat4 frustum matrix will be written into
1733 * @returns {mat4} dest if specified, a new mat4 otherwise
1735 mat4.perspective = function (fovy, aspect, near, far, dest) {
1736 var top = near * Math.tan(fovy * Math.PI / 360.0),
1737 right = top * aspect;
1738 return mat4.frustum(-right, right, -top, top, near, far, dest);
1742 * Generates a orthogonal projection matrix with the given bounds
1744 * @param {number} left Left bound of the frustum
1745 * @param {number} right Right bound of the frustum
1746 * @param {number} bottom Bottom bound of the frustum
1747 * @param {number} top Top bound of the frustum
1748 * @param {number} near Near bound of the frustum
1749 * @param {number} far Far bound of the frustum
1750 * @param {mat4} [dest] mat4 frustum matrix will be written into
1752 * @returns {mat4} dest if specified, a new mat4 otherwise
1754 mat4.ortho = function (left, right, bottom, top, near, far, dest) {
1755 if (!dest) { dest = mat4.create(); }
1756 var rl = (right - left),
1757 tb = (top - bottom),
1771 dest[12] = -(left + right) / rl;
1772 dest[13] = -(top + bottom) / tb;
1773 dest[14] = -(far + near) / fn;
1779 * Generates a look-at matrix with the given eye position, focal point, and up axis
1781 * @param {vec3} eye Position of the viewer
1782 * @param {vec3} center Point the viewer is looking at
1783 * @param {vec3} up vec3 pointing "up"
1784 * @param {mat4} [dest] mat4 frustum matrix will be written into
1786 * @returns {mat4} dest if specified, a new mat4 otherwise
1788 mat4.lookAt = function (eye, center, up, dest) {
1789 if (!dest) { dest = mat4.create(); }
1791 var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
1798 centerx = center[0],
1799 centery = center[1],
1800 centerz = center[2];
1802 if (eyex === centerx && eyey === centery && eyez === centerz) {
1803 return mat4.identity(dest);
1806 //vec3.direction(eye, center, z);
1807 z0 = eyex - centerx;
1808 z1 = eyey - centery;
1809 z2 = eyez - centerz;
1811 // normalize (no check needed for 0 because of early return)
1812 len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
1817 //vec3.normalize(vec3.cross(up, z, x));
1818 x0 = upy * z2 - upz * z1;
1819 x1 = upz * z0 - upx * z2;
1820 x2 = upx * z1 - upy * z0;
1821 len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
1833 //vec3.normalize(vec3.cross(z, x, y));
1834 y0 = z1 * x2 - z2 * x1;
1835 y1 = z2 * x0 - z0 * x2;
1836 y2 = z0 * x1 - z1 * x0;
1838 len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
1862 dest[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
1863 dest[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
1864 dest[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
1871 * Creates a matrix from a quaternion rotation and vector translation
1872 * This is equivalent to (but much faster than):
1874 * mat4.identity(dest);
1875 * mat4.translate(dest, vec);
1876 * var quatMat = mat4.create();
1877 * quat4.toMat4(quat, quatMat);
1878 * mat4.multiply(dest, quatMat);
1880 * @param {quat4} quat Rotation quaternion
1881 * @param {vec3} vec Translation vector
1882 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to a new mat4
1884 * @returns {mat4} dest if specified, a new mat4 otherwise
1886 mat4.fromRotationTranslation = function (quat, vec, dest) {
1887 if (!dest) { dest = mat4.create(); }
1890 var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
1905 dest[0] = 1 - (yy + zz);
1910 dest[5] = 1 - (xx + zz);
1915 dest[10] = 1 - (xx + yy);
1926 * Returns a string representation of a mat4
1928 * @param {mat4} mat mat4 to represent as a string
1930 * @returns {string} String representation of mat
1932 mat4.str = function (mat) {
1933 return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] +
1934 ', ' + mat[4] + ', ' + mat[5] + ', ' + mat[6] + ', ' + mat[7] +
1935 ', ' + mat[8] + ', ' + mat[9] + ', ' + mat[10] + ', ' + mat[11] +
1936 ', ' + mat[12] + ', ' + mat[13] + ', ' + mat[14] + ', ' + mat[15] + ']';
1946 * Creates a new instance of a quat4 using the default array type
1947 * Any javascript array containing at least 4 numeric elements can serve as a quat4
1949 * @param {quat4} [quat] quat4 containing values to initialize with
1951 * @returns {quat4} New quat4
1953 quat4.create = function (quat) {
1954 var dest = new MatrixArray(4);
1962 dest[0] = dest[1] = dest[2] = dest[3] = 0;
1969 * Creates a new instance of a quat4, initializing it with the given arguments
1971 * @param {number} x X value
1972 * @param {number} y Y value
1973 * @param {number} z Z value
1974 * @param {number} w W value
1976 * @returns {quat4} New quat4
1978 quat4.createFrom = function (x, y, z, w) {
1979 var dest = new MatrixArray(4);
1990 * Copies the values of one quat4 to another
1992 * @param {quat4} quat quat4 containing values to copy
1993 * @param {quat4} dest quat4 receiving copied values
1995 * @returns {quat4} dest
1997 quat4.set = function (quat, dest) {
2007 * Compares two quaternions for equality within a certain margin of error
2009 * @param {quat4} a First vector
2010 * @param {quat4} b Second vector
2012 * @returns {Boolean} True if a is equivalent to b
2014 quat4.equal = function (a, b) {
2016 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
2017 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
2018 Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
2019 Math.abs(a[3] - b[3]) < FLOAT_EPSILON
2024 * Creates a new identity Quat4
2026 * @param {quat4} [dest] quat4 receiving copied values
2028 * @returns {quat4} dest is specified, new quat4 otherwise
2030 quat4.identity = function (dest) {
2031 if (!dest) { dest = quat4.create(); }
2039 var identityQuat4 = quat4.identity();
2042 * Calculates the W component of a quat4 from the X, Y, and Z components.
2043 * Assumes that quaternion is 1 unit in length.
2044 * Any existing W component will be ignored.
2046 * @param {quat4} quat quat4 to calculate W component of
2047 * @param {quat4} [dest] quat4 receiving calculated values. If not specified result is written to quat
2049 * @returns {quat4} dest if specified, quat otherwise
2051 quat4.calculateW = function (quat, dest) {
2052 var x = quat[0], y = quat[1], z = quat[2];
2054 if (!dest || quat === dest) {
2055 quat[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
2061 dest[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
2066 * Calculates the dot product of two quaternions
2068 * @param {quat4} quat First operand
2069 * @param {quat4} quat2 Second operand
2071 * @return {number} Dot product of quat and quat2
2073 quat4.dot = function(quat, quat2){
2074 return quat[0]*quat2[0] + quat[1]*quat2[1] + quat[2]*quat2[2] + quat[3]*quat2[3];
2078 * Calculates the inverse of a quat4
2080 * @param {quat4} quat quat4 to calculate inverse of
2081 * @param {quat4} [dest] quat4 receiving inverse values. If not specified result is written to quat
2083 * @returns {quat4} dest if specified, quat otherwise
2085 quat4.inverse = function(quat, dest) {
2086 var q0 = quat[0], q1 = quat[1], q2 = quat[2], q3 = quat[3],
2087 dot = q0*q0 + q1*q1 + q2*q2 + q3*q3,
2088 invDot = dot ? 1.0/dot : 0;
2090 // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
2092 if(!dest || quat === dest) {
2099 dest[0] = -quat[0]*invDot;
2100 dest[1] = -quat[1]*invDot;
2101 dest[2] = -quat[2]*invDot;
2102 dest[3] = quat[3]*invDot;
2108 * Calculates the conjugate of a quat4
2109 * If the quaternion is normalized, this function is faster than quat4.inverse and produces the same result.
2111 * @param {quat4} quat quat4 to calculate conjugate of
2112 * @param {quat4} [dest] quat4 receiving conjugate values. If not specified result is written to quat
2114 * @returns {quat4} dest if specified, quat otherwise
2116 quat4.conjugate = function (quat, dest) {
2117 if (!dest || quat === dest) {
2131 * Calculates the length of a quat4
2134 * @param {quat4} quat quat4 to calculate length of
2136 * @returns Length of quat
2138 quat4.length = function (quat) {
2139 var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
2140 return Math.sqrt(x * x + y * y + z * z + w * w);
2144 * Generates a unit quaternion of the same direction as the provided quat4
2145 * If quaternion length is 0, returns [0, 0, 0, 0]
2147 * @param {quat4} quat quat4 to normalize
2148 * @param {quat4} [dest] quat4 receiving operation result. If not specified result is written to quat
2150 * @returns {quat4} dest if specified, quat otherwise
2152 quat4.normalize = function (quat, dest) {
2153 if (!dest) { dest = quat; }
2155 var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
2156 len = Math.sqrt(x * x + y * y + z * z + w * w);
2174 * Performs quaternion addition
2176 * @param {quat4} quat First operand
2177 * @param {quat4} quat2 Second operand
2178 * @param {quat4} [dest] quat4 receiving operation result. If not specified result is written to quat
2180 * @returns {quat4} dest if specified, quat otherwise
2182 quat4.add = function (quat, quat2, dest) {
2183 if(!dest || quat === dest) {
2184 quat[0] += quat2[0];
2185 quat[1] += quat2[1];
2186 quat[2] += quat2[2];
2187 quat[3] += quat2[3];
2190 dest[0] = quat[0]+quat2[0];
2191 dest[1] = quat[1]+quat2[1];
2192 dest[2] = quat[2]+quat2[2];
2193 dest[3] = quat[3]+quat2[3];
2198 * Performs a quaternion multiplication
2200 * @param {quat4} quat First operand
2201 * @param {quat4} quat2 Second operand
2202 * @param {quat4} [dest] quat4 receiving operation result. If not specified result is written to quat
2204 * @returns {quat4} dest if specified, quat otherwise
2206 quat4.multiply = function (quat, quat2, dest) {
2207 if (!dest) { dest = quat; }
2209 var qax = quat[0], qay = quat[1], qaz = quat[2], qaw = quat[3],
2210 qbx = quat2[0], qby = quat2[1], qbz = quat2[2], qbw = quat2[3];
2212 dest[0] = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
2213 dest[1] = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
2214 dest[2] = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
2215 dest[3] = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
2221 * Transforms a vec3 with the given quaternion
2223 * @param {quat4} quat quat4 to transform the vector with
2224 * @param {vec3} vec vec3 to transform
2225 * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
2227 * @returns dest if specified, vec otherwise
2229 quat4.multiplyVec3 = function (quat, vec, dest) {
2230 if (!dest) { dest = vec; }
2232 var x = vec[0], y = vec[1], z = vec[2],
2233 qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3],
2235 // calculate quat * vec
2236 ix = qw * x + qy * z - qz * y,
2237 iy = qw * y + qz * x - qx * z,
2238 iz = qw * z + qx * y - qy * x,
2239 iw = -qx * x - qy * y - qz * z;
2241 // calculate result * inverse quat
2242 dest[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
2243 dest[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
2244 dest[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
2250 * Multiplies the components of a quaternion by a scalar value
2252 * @param {quat4} quat to scale
2253 * @param {number} val Value to scale by
2254 * @param {quat4} [dest] quat4 receiving operation result. If not specified result is written to quat
2256 * @returns {quat4} dest if specified, quat otherwise
2258 quat4.scale = function (quat, val, dest) {
2259 if(!dest || quat === dest) {
2266 dest[0] = quat[0]*val;
2267 dest[1] = quat[1]*val;
2268 dest[2] = quat[2]*val;
2269 dest[3] = quat[3]*val;
2274 * Calculates a 3x3 matrix from the given quat4
2276 * @param {quat4} quat quat4 to create matrix from
2277 * @param {mat3} [dest] mat3 receiving operation result
2279 * @returns {mat3} dest if specified, a new mat3 otherwise
2281 quat4.toMat3 = function (quat, dest) {
2282 if (!dest) { dest = mat3.create(); }
2284 var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
2299 dest[0] = 1 - (yy + zz);
2304 dest[4] = 1 - (xx + zz);
2309 dest[8] = 1 - (xx + yy);
2315 * Calculates a 4x4 matrix from the given quat4
2317 * @param {quat4} quat quat4 to create matrix from
2318 * @param {mat4} [dest] mat4 receiving operation result
2320 * @returns {mat4} dest if specified, a new mat4 otherwise
2322 quat4.toMat4 = function (quat, dest) {
2323 if (!dest) { dest = mat4.create(); }
2325 var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
2340 dest[0] = 1 - (yy + zz);
2346 dest[5] = 1 - (xx + zz);
2352 dest[10] = 1 - (xx + yy);
2364 * Performs a spherical linear interpolation between two quat4
2366 * @param {quat4} quat First quaternion
2367 * @param {quat4} quat2 Second quaternion
2368 * @param {number} slerp Interpolation amount between the two inputs
2369 * @param {quat4} [dest] quat4 receiving operation result. If not specified result is written to quat
2371 * @returns {quat4} dest if specified, quat otherwise
2373 quat4.slerp = function (quat, quat2, slerp, dest) {
2374 if (!dest) { dest = quat; }
2376 var cosHalfTheta = quat[0] * quat2[0] + quat[1] * quat2[1] + quat[2] * quat2[2] + quat[3] * quat2[3],
2382 if (Math.abs(cosHalfTheta) >= 1.0) {
2383 if (dest !== quat) {
2392 halfTheta = Math.acos(cosHalfTheta);
2393 sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
2395 if (Math.abs(sinHalfTheta) < 0.001) {
2396 dest[0] = (quat[0] * 0.5 + quat2[0] * 0.5);
2397 dest[1] = (quat[1] * 0.5 + quat2[1] * 0.5);
2398 dest[2] = (quat[2] * 0.5 + quat2[2] * 0.5);
2399 dest[3] = (quat[3] * 0.5 + quat2[3] * 0.5);
2403 ratioA = Math.sin((1 - slerp) * halfTheta) / sinHalfTheta;
2404 ratioB = Math.sin(slerp * halfTheta) / sinHalfTheta;
2406 dest[0] = (quat[0] * ratioA + quat2[0] * ratioB);
2407 dest[1] = (quat[1] * ratioA + quat2[1] * ratioB);
2408 dest[2] = (quat[2] * ratioA + quat2[2] * ratioB);
2409 dest[3] = (quat[3] * ratioA + quat2[3] * ratioB);
2415 * Creates a quaternion from the given 3x3 rotation matrix.
2416 * If dest is omitted, a new quaternion will be created.
2418 * @param {mat3} mat the rotation matrix
2419 * @param {quat4} [dest] an optional receiving quaternion
2421 * @returns {quat4} the quaternion constructed from the rotation matrix
2424 quat4.fromRotationMatrix = function(mat, dest) {
2425 if (!dest) dest = quat4.create();
2427 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
2428 // article "Quaternion Calculus and Fast Animation".
2430 var fTrace = mat[0] + mat[4] + mat[8];
2433 if ( fTrace > 0.0 ) {
2434 // |w| > 1/2, may as well choose w > 1/2
2435 fRoot = Math.sqrt(fTrace + 1.0); // 2w
2436 dest[3] = 0.5 * fRoot;
2437 fRoot = 0.5/fRoot; // 1/(4w)
2438 dest[0] = (mat[7]-mat[5])*fRoot;
2439 dest[1] = (mat[2]-mat[6])*fRoot;
2440 dest[2] = (mat[3]-mat[1])*fRoot;
2443 var s_iNext = quat4.fromRotationMatrix.s_iNext = quat4.fromRotationMatrix.s_iNext || [1,2,0];
2445 if ( mat[4] > mat[0] )
2447 if ( mat[8] > mat[i*3+i] )
2452 fRoot = Math.sqrt(mat[i*3+i]-mat[j*3+j]-mat[k*3+k] + 1.0);
2453 dest[i] = 0.5 * fRoot;
2454 fRoot = 0.5 / fRoot;
2455 dest[3] = (mat[k*3+j] - mat[j*3+k]) * fRoot;
2456 dest[j] = (mat[j*3+i] + mat[i*3+j]) * fRoot;
2457 dest[k] = (mat[k*3+i] + mat[i*3+k]) * fRoot;
2464 * Alias. See the description for quat4.fromRotationMatrix().
2466 mat3.toQuat4 = quat4.fromRotationMatrix;
2469 var mat = mat3.create();
2472 * Creates a quaternion from the 3 given vectors. They must be perpendicular
2473 * to one another and represent the X, Y and Z axes.
2475 * If dest is omitted, a new quat4 will be created.
2477 * Example: The default OpenGL orientation has a view vector [0, 0, -1],
2478 * right vector [1, 0, 0], and up vector [0, 1, 0]. A quaternion representing
2479 * this orientation could be constructed with:
2481 * quat = quat4.fromAxes([0, 0, -1], [1, 0, 0], [0, 1, 0], quat4.create());
2483 * @param {vec3} view the view vector, or direction the object is pointing in
2484 * @param {vec3} right the right vector, or direction to the "right" of the object
2485 * @param {vec3} up the up vector, or direction towards the object's "up"
2486 * @param {quat4} [dest] an optional receiving quat4
2488 * @returns {quat4} dest
2490 quat4.fromAxes = function(view, right, up, dest) {
2503 return quat4.fromRotationMatrix(mat, dest);
2508 * Sets a quat4 to the Identity and returns it.
2510 * @param {quat4} [dest] quat4 to set. If omitted, a
2511 * new quat4 will be created.
2513 * @returns {quat4} dest
2515 quat4.identity = function(dest) {
2516 if (!dest) dest = quat4.create();
2525 * Sets a quat4 from the given angle and rotation axis,
2526 * then returns it. If dest is not given, a new quat4 is created.
2528 * @param {Number} angle the angle in radians
2529 * @param {vec3} axis the axis around which to rotate
2530 * @param {quat4} [dest] the optional quat4 to store the result
2532 * @returns {quat4} dest
2534 quat4.fromAngleAxis = function(angle, axis, dest) {
2535 // The quaternion representing the rotation is
2536 // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
2537 if (!dest) dest = quat4.create();
2539 var half = angle * 0.5;
2540 var s = Math.sin(half);
2541 dest[3] = Math.cos(half);
2542 dest[0] = s * axis[0];
2543 dest[1] = s * axis[1];
2544 dest[2] = s * axis[2];
2550 * Stores the angle and axis in a vec4, where the XYZ components represent
2551 * the axis and the W (4th) component is the angle in radians.
2553 * If dest is not given, src will be modified in place and returned, after
2554 * which it should not be considered not a quaternion (just an axis and angle).
2556 * @param {quat4} quat the quaternion whose angle and axis to store
2557 * @param {vec4} [dest] the optional vec4 to receive the data
2559 * @returns {vec4} dest
2561 quat4.toAngleAxis = function(src, dest) {
2562 if (!dest) dest = src;
2563 // The quaternion representing the rotation is
2564 // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
2566 var sqrlen = src[0]*src[0]+src[1]*src[1]+src[2]*src[2];
2569 dest[3] = 2 * Math.acos(src[3]);
2570 var invlen = glMath.invsqrt(sqrlen);
2571 dest[0] = src[0]*invlen;
2572 dest[1] = src[1]*invlen;
2573 dest[2] = src[2]*invlen;
2575 // angle is 0 (mod 2*pi), so any axis will do
2586 * Returns a string representation of a quaternion
2588 * @param {quat4} quat quat4 to represent as a string
2590 * @returns {string} String representation of quat
2592 quat4.str = function (quat) {
2593 return '[' + quat[0] + ', ' + quat[1] + ', ' + quat[2] + ', ' + quat[3] + ']';
2597 * @class 2 Dimensional Vector
2603 * Creates a new vec2, initializing it from vec if vec
2606 * @param {vec2} [vec] the vector's initial contents
2607 * @returns {vec2} a new 2D vector
2609 vec2.create = function(vec) {
2610 var dest = new MatrixArray(2);
2623 * Creates a new instance of a vec2, initializing it with the given arguments
2625 * @param {number} x X value
2626 * @param {number} y Y value
2628 * @returns {vec2} New vec2
2630 vec2.createFrom = function (x, y) {
2631 var dest = new MatrixArray(2);
2640 * Adds the vec2's together. If dest is given, the result
2641 * is stored there. Otherwise, the result is stored in vecB.
2643 * @param {vec2} vecA the first operand
2644 * @param {vec2} vecB the second operand
2645 * @param {vec2} [dest] the optional receiving vector
2646 * @returns {vec2} dest
2648 vec2.add = function(vecA, vecB, dest) {
2649 if (!dest) dest = vecB;
2650 dest[0] = vecA[0] + vecB[0];
2651 dest[1] = vecA[1] + vecB[1];
2656 * Subtracts vecB from vecA. If dest is given, the result
2657 * is stored there. Otherwise, the result is stored in vecB.
2659 * @param {vec2} vecA the first operand
2660 * @param {vec2} vecB the second operand
2661 * @param {vec2} [dest] the optional receiving vector
2662 * @returns {vec2} dest
2664 vec2.subtract = function(vecA, vecB, dest) {
2665 if (!dest) dest = vecB;
2666 dest[0] = vecA[0] - vecB[0];
2667 dest[1] = vecA[1] - vecB[1];
2672 * Multiplies vecA with vecB. If dest is given, the result
2673 * is stored there. Otherwise, the result is stored in vecB.
2675 * @param {vec2} vecA the first operand
2676 * @param {vec2} vecB the second operand
2677 * @param {vec2} [dest] the optional receiving vector
2678 * @returns {vec2} dest
2680 vec2.multiply = function(vecA, vecB, dest) {
2681 if (!dest) dest = vecB;
2682 dest[0] = vecA[0] * vecB[0];
2683 dest[1] = vecA[1] * vecB[1];
2688 * Divides vecA by vecB. If dest is given, the result
2689 * is stored there. Otherwise, the result is stored in vecB.
2691 * @param {vec2} vecA the first operand
2692 * @param {vec2} vecB the second operand
2693 * @param {vec2} [dest] the optional receiving vector
2694 * @returns {vec2} dest
2696 vec2.divide = function(vecA, vecB, dest) {
2697 if (!dest) dest = vecB;
2698 dest[0] = vecA[0] / vecB[0];
2699 dest[1] = vecA[1] / vecB[1];
2704 * Scales vecA by some scalar number. If dest is given, the result
2705 * is stored there. Otherwise, the result is stored in vecA.
2707 * This is the same as multiplying each component of vecA
2708 * by the given scalar.
2710 * @param {vec2} vecA the vector to be scaled
2711 * @param {Number} scalar the amount to scale the vector by
2712 * @param {vec2} [dest] the optional receiving vector
2713 * @returns {vec2} dest
2715 vec2.scale = function(vecA, scalar, dest) {
2716 if (!dest) dest = vecA;
2717 dest[0] = vecA[0] * scalar;
2718 dest[1] = vecA[1] * scalar;
2723 * Calculates the euclidian distance between two vec2
2726 * @param {vec2} vecA First vector
2727 * @param {vec2} vecB Second vector
2729 * @returns {number} Distance between vecA and vecB
2731 vec2.dist = function (vecA, vecB) {
2732 var x = vecB[0] - vecA[0],
2733 y = vecB[1] - vecA[1];
2734 return Math.sqrt(x*x + y*y);
2738 * Copies the values of one vec2 to another
2740 * @param {vec2} vec vec2 containing values to copy
2741 * @param {vec2} dest vec2 receiving copied values
2743 * @returns {vec2} dest
2745 vec2.set = function (vec, dest) {
2752 * Compares two vectors for equality within a certain margin of error
2754 * @param {vec2} a First vector
2755 * @param {vec2} b Second vector
2757 * @returns {Boolean} True if a is equivalent to b
2759 vec2.equal = function (a, b) {
2761 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
2762 Math.abs(a[1] - b[1]) < FLOAT_EPSILON
2767 * Negates the components of a vec2
2769 * @param {vec2} vec vec2 to negate
2770 * @param {vec2} [dest] vec2 receiving operation result. If not specified result is written to vec
2772 * @returns {vec2} dest if specified, vec otherwise
2774 vec2.negate = function (vec, dest) {
2775 if (!dest) { dest = vec; }
2784 * @param {vec2} vec vec2 to normalize
2785 * @param {vec2} [dest] vec2 receiving operation result. If not specified result is written to vec
2787 * @returns {vec2} dest if specified, vec otherwise
2789 vec2.normalize = function (vec, dest) {
2790 if (!dest) { dest = vec; }
2791 var mag = vec[0] * vec[0] + vec[1] * vec[1];
2793 mag = Math.sqrt(mag);
2794 dest[0] = vec[0] / mag;
2795 dest[1] = vec[1] / mag;
2797 dest[0] = dest[1] = 0;
2803 * Computes the cross product of two vec2's. Note that the cross product must by definition
2804 * produce a 3D vector. If a dest vector is given, it will contain the resultant 3D vector.
2805 * Otherwise, a scalar number will be returned, representing the vector's Z coordinate, since
2806 * its X and Y must always equal 0.
2809 * var crossResult = vec3.create();
2810 * vec2.cross([1, 2], [3, 4], crossResult);
2813 * vec2.cross([1, 2], [3, 4]);
2816 * See http://stackoverflow.com/questions/243945/calculating-a-2d-vectors-cross-product
2817 * for some interesting facts.
2819 * @param {vec2} vecA left operand
2820 * @param {vec2} vecB right operand
2821 * @param {vec2} [dest] optional vec2 receiving result. If not specified a scalar is returned
2824 vec2.cross = function (vecA, vecB, dest) {
2825 var z = vecA[0] * vecB[1] - vecA[1] * vecB[0];
2826 if (!dest) return z;
2827 dest[0] = dest[1] = 0;
2833 * Caclulates the length of a vec2
2835 * @param {vec2} vec vec2 to calculate length of
2837 * @returns {Number} Length of vec
2839 vec2.length = function (vec) {
2840 var x = vec[0], y = vec[1];
2841 return Math.sqrt(x * x + y * y);
2845 * Caclulates the squared length of a vec2
2847 * @param {vec2} vec vec2 to calculate squared length of
2849 * @returns {Number} Squared Length of vec
2851 vec2.squaredLength = function (vec) {
2852 var x = vec[0], y = vec[1];
2853 return x * x + y * y;
2857 * Caclulates the dot product of two vec2s
2859 * @param {vec2} vecA First operand
2860 * @param {vec2} vecB Second operand
2862 * @returns {Number} Dot product of vecA and vecB
2864 vec2.dot = function (vecA, vecB) {
2865 return vecA[0] * vecB[0] + vecA[1] * vecB[1];
2869 * Generates a 2D unit vector pointing from one vector to another
2871 * @param {vec2} vecA Origin vec2
2872 * @param {vec2} vecB vec2 to point to
2873 * @param {vec2} [dest] vec2 receiving operation result. If not specified result is written to vecA
2875 * @returns {vec2} dest if specified, vecA otherwise
2877 vec2.direction = function (vecA, vecB, dest) {
2878 if (!dest) { dest = vecA; }
2880 var x = vecA[0] - vecB[0],
2881 y = vecA[1] - vecB[1],
2882 len = x * x + y * y;
2891 len = 1 / Math.sqrt(len);
2898 * Performs a linear interpolation between two vec2
2900 * @param {vec2} vecA First vector
2901 * @param {vec2} vecB Second vector
2902 * @param {Number} lerp Interpolation amount between the two inputs
2903 * @param {vec2} [dest] vec2 receiving operation result. If not specified result is written to vecA
2905 * @returns {vec2} dest if specified, vecA otherwise
2907 vec2.lerp = function (vecA, vecB, lerp, dest) {
2908 if (!dest) { dest = vecA; }
2909 dest[0] = vecA[0] + lerp * (vecB[0] - vecA[0]);
2910 dest[1] = vecA[1] + lerp * (vecB[1] - vecA[1]);
2915 * Returns a string representation of a vector
2917 * @param {vec2} vec Vector to represent as a string
2919 * @returns {String} String representation of vec
2921 vec2.str = function (vec) {
2922 return '[' + vec[0] + ', ' + vec[1] + ']';
2932 * Creates a new 2x2 matrix. If src is given, the new matrix
2933 * is initialized to those values.
2935 * @param {mat2} [src] the seed values for the new matrix, if any
2936 * @returns {mat2} a new matrix
2938 mat2.create = function(src) {
2939 var dest = new MatrixArray(4);
2947 dest[0] = dest[1] = dest[2] = dest[3] = 0;
2953 * Creates a new instance of a mat2, initializing it with the given arguments
2955 * @param {number} m00
2956 * @param {number} m01
2957 * @param {number} m10
2958 * @param {number} m11
2960 * @returns {mat2} New mat2
2962 mat2.createFrom = function (m00, m01, m10, m11) {
2963 var dest = new MatrixArray(4);
2974 * Copies the values of one mat2 to another
2976 * @param {mat2} mat mat2 containing values to copy
2977 * @param {mat2} dest mat2 receiving copied values
2979 * @returns {mat2} dest
2981 mat2.set = function (mat, dest) {
2990 * Compares two matrices for equality within a certain margin of error
2992 * @param {mat2} a First matrix
2993 * @param {mat2} b Second matrix
2995 * @returns {Boolean} True if a is equivalent to b
2997 mat2.equal = function (a, b) {
2999 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
3000 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
3001 Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
3002 Math.abs(a[3] - b[3]) < FLOAT_EPSILON
3007 * Sets a mat2 to an identity matrix
3009 * @param {mat2} [dest] mat2 to set. If omitted a new one will be created.
3011 * @returns {mat2} dest
3013 mat2.identity = function (dest) {
3014 if (!dest) { dest = mat2.create(); }
3023 * Transposes a mat2 (flips the values over the diagonal)
3025 * @param {mat2} mat mat2 to transpose
3026 * @param {mat2} [dest] mat2 receiving transposed values. If not specified result is written to mat
3028 * @param {mat2} dest if specified, mat otherwise
3030 mat2.transpose = function (mat, dest) {
3031 // If we are transposing ourselves we can skip a few steps but have to cache some values
3032 if (!dest || mat === dest) {
3047 * Calculates the determinant of a mat2
3049 * @param {mat2} mat mat2 to calculate determinant of
3051 * @returns {Number} determinant of mat
3053 mat2.determinant = function (mat) {
3054 return mat[0] * mat[3] - mat[2] * mat[1];
3058 * Calculates the inverse matrix of a mat2
3060 * @param {mat2} mat mat2 to calculate inverse of
3061 * @param {mat2} [dest] mat2 receiving inverse matrix. If not specified result is written to mat
3063 * @param {mat2} dest is specified, mat otherwise, null if matrix cannot be inverted
3065 mat2.inverse = function (mat, dest) {
3066 if (!dest) { dest = mat; }
3067 var a0 = mat[0], a1 = mat[1], a2 = mat[2], a3 = mat[3];
3068 var det = a0 * a3 - a2 * a1;
3069 if (!det) return null;
3073 dest[1] = -a1 * det;
3074 dest[2] = -a2 * det;
3080 * Performs a matrix multiplication
3082 * @param {mat2} matA First operand
3083 * @param {mat2} matB Second operand
3084 * @param {mat2} [dest] mat2 receiving operation result. If not specified result is written to matA
3086 * @returns {mat2} dest if specified, matA otherwise
3088 mat2.multiply = function (matA, matB, dest) {
3089 if (!dest) { dest = matA; }
3094 dest[0] = a11 * matB[0] + a12 * matB[2];
3095 dest[1] = a11 * matB[1] + a12 * matB[3];
3096 dest[2] = a21 * matB[0] + a22 * matB[2];
3097 dest[3] = a21 * matB[1] + a22 * matB[3];
3102 * Rotates a 2x2 matrix by an angle
3104 * @param {mat2} mat The matrix to rotate
3105 * @param {Number} angle The angle in radians
3106 * @param {mat2} [dest] Optional mat2 receiving the result. If omitted mat will be used.
3108 * @returns {mat2} dest if specified, mat otherwise
3110 mat2.rotate = function (mat, angle, dest) {
3111 if (!dest) { dest = mat; }
3116 s = Math.sin(angle),
3117 c = Math.cos(angle);
3118 dest[0] = a11 * c + a12 * s;
3119 dest[1] = a11 * -s + a12 * c;
3120 dest[2] = a21 * c + a22 * s;
3121 dest[3] = a21 * -s + a22 * c;
3126 * Multiplies the vec2 by the given 2x2 matrix
3128 * @param {mat2} matrix the 2x2 matrix to multiply against
3129 * @param {vec2} vec the vector to multiply
3130 * @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
3132 * @returns {vec2} The multiplication result
3134 mat2.multiplyVec2 = function(matrix, vec, dest) {
3135 if (!dest) dest = vec;
3136 var x = vec[0], y = vec[1];
3137 dest[0] = x * matrix[0] + y * matrix[1];
3138 dest[1] = x * matrix[2] + y * matrix[3];
3143 * Scales the mat2 by the dimensions in the given vec2
3145 * @param {mat2} matrix the 2x2 matrix to scale
3146 * @param {vec2} vec the vector containing the dimensions to scale by
3147 * @param {vec2} [dest] an optional receiving mat2. If not given, matrix is used.
3149 * @returns {mat2} dest if specified, matrix otherwise
3151 mat2.scale = function(matrix, vec, dest) {
3152 if (!dest) { dest = matrix; }
3153 var a11 = matrix[0],
3159 dest[0] = a11 * b11;
3160 dest[1] = a12 * b22;
3161 dest[2] = a21 * b11;
3162 dest[3] = a22 * b22;
3167 * Returns a string representation of a mat2
3169 * @param {mat2} mat mat2 to represent as a string
3171 * @param {String} String representation of mat
3173 mat2.str = function (mat) {
3174 return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] + ']';
3178 * @class 4 Dimensional Vector
3184 * Creates a new vec4, initializing it from vec if vec
3187 * @param {vec4} [vec] the vector's initial contents
3188 * @returns {vec4} a new 2D vector
3190 vec4.create = function(vec) {
3191 var dest = new MatrixArray(4);
3208 * Creates a new instance of a vec4, initializing it with the given arguments
3210 * @param {number} x X value
3211 * @param {number} y Y value
3212 * @param {number} z Z value
3213 * @param {number} w W value
3215 * @returns {vec4} New vec4
3217 vec4.createFrom = function (x, y, z, w) {
3218 var dest = new MatrixArray(4);
3229 * Adds the vec4's together. If dest is given, the result
3230 * is stored there. Otherwise, the result is stored in vecB.
3232 * @param {vec4} vecA the first operand
3233 * @param {vec4} vecB the second operand
3234 * @param {vec4} [dest] the optional receiving vector
3235 * @returns {vec4} dest
3237 vec4.add = function(vecA, vecB, dest) {
3238 if (!dest) dest = vecB;
3239 dest[0] = vecA[0] + vecB[0];
3240 dest[1] = vecA[1] + vecB[1];
3241 dest[2] = vecA[2] + vecB[2];
3242 dest[3] = vecA[3] + vecB[3];
3247 * Subtracts vecB from vecA. If dest is given, the result
3248 * is stored there. Otherwise, the result is stored in vecB.
3250 * @param {vec4} vecA the first operand
3251 * @param {vec4} vecB the second operand
3252 * @param {vec4} [dest] the optional receiving vector
3253 * @returns {vec4} dest
3255 vec4.subtract = function(vecA, vecB, dest) {
3256 if (!dest) dest = vecB;
3257 dest[0] = vecA[0] - vecB[0];
3258 dest[1] = vecA[1] - vecB[1];
3259 dest[2] = vecA[2] - vecB[2];
3260 dest[3] = vecA[3] - vecB[3];
3265 * Multiplies vecA with vecB. If dest is given, the result
3266 * is stored there. Otherwise, the result is stored in vecB.
3268 * @param {vec4} vecA the first operand
3269 * @param {vec4} vecB the second operand
3270 * @param {vec4} [dest] the optional receiving vector
3271 * @returns {vec4} dest
3273 vec4.multiply = function(vecA, vecB, dest) {
3274 if (!dest) dest = vecB;
3275 dest[0] = vecA[0] * vecB[0];
3276 dest[1] = vecA[1] * vecB[1];
3277 dest[2] = vecA[2] * vecB[2];
3278 dest[3] = vecA[3] * vecB[3];
3283 * Divides vecA by vecB. If dest is given, the result
3284 * is stored there. Otherwise, the result is stored in vecB.
3286 * @param {vec4} vecA the first operand
3287 * @param {vec4} vecB the second operand
3288 * @param {vec4} [dest] the optional receiving vector
3289 * @returns {vec4} dest
3291 vec4.divide = function(vecA, vecB, dest) {
3292 if (!dest) dest = vecB;
3293 dest[0] = vecA[0] / vecB[0];
3294 dest[1] = vecA[1] / vecB[1];
3295 dest[2] = vecA[2] / vecB[2];
3296 dest[3] = vecA[3] / vecB[3];
3301 * Scales vecA by some scalar number. If dest is given, the result
3302 * is stored there. Otherwise, the result is stored in vecA.
3304 * This is the same as multiplying each component of vecA
3305 * by the given scalar.
3307 * @param {vec4} vecA the vector to be scaled
3308 * @param {Number} scalar the amount to scale the vector by
3309 * @param {vec4} [dest] the optional receiving vector
3310 * @returns {vec4} dest
3312 vec4.scale = function(vecA, scalar, dest) {
3313 if (!dest) dest = vecA;
3314 dest[0] = vecA[0] * scalar;
3315 dest[1] = vecA[1] * scalar;
3316 dest[2] = vecA[2] * scalar;
3317 dest[3] = vecA[3] * scalar;
3322 * Copies the values of one vec4 to another
3324 * @param {vec4} vec vec4 containing values to copy
3325 * @param {vec4} dest vec4 receiving copied values
3327 * @returns {vec4} dest
3329 vec4.set = function (vec, dest) {
3338 * Compares two vectors for equality within a certain margin of error
3340 * @param {vec4} a First vector
3341 * @param {vec4} b Second vector
3343 * @returns {Boolean} True if a is equivalent to b
3345 vec4.equal = function (a, b) {
3347 Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
3348 Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
3349 Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
3350 Math.abs(a[3] - b[3]) < FLOAT_EPSILON
3355 * Negates the components of a vec4
3357 * @param {vec4} vec vec4 to negate
3358 * @param {vec4} [dest] vec4 receiving operation result. If not specified result is written to vec
3360 * @returns {vec4} dest if specified, vec otherwise
3362 vec4.negate = function (vec, dest) {
3363 if (!dest) { dest = vec; }
3372 * Caclulates the length of a vec2
3374 * @param {vec2} vec vec2 to calculate length of
3376 * @returns {Number} Length of vec
3378 vec4.length = function (vec) {
3379 var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
3380 return Math.sqrt(x * x + y * y + z * z + w * w);
3384 * Caclulates the squared length of a vec4
3386 * @param {vec4} vec vec4 to calculate squared length of
3388 * @returns {Number} Squared Length of vec
3390 vec4.squaredLength = function (vec) {
3391 var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
3392 return x * x + y * y + z * z + w * w;
3396 * Performs a linear interpolation between two vec4
3398 * @param {vec4} vecA First vector
3399 * @param {vec4} vecB Second vector
3400 * @param {Number} lerp Interpolation amount between the two inputs
3401 * @param {vec4} [dest] vec4 receiving operation result. If not specified result is written to vecA
3403 * @returns {vec4} dest if specified, vecA otherwise
3405 vec4.lerp = function (vecA, vecB, lerp, dest) {
3406 if (!dest) { dest = vecA; }
3407 dest[0] = vecA[0] + lerp * (vecB[0] - vecA[0]);
3408 dest[1] = vecA[1] + lerp * (vecB[1] - vecA[1]);
3409 dest[2] = vecA[2] + lerp * (vecB[2] - vecA[2]);
3410 dest[3] = vecA[3] + lerp * (vecB[3] - vecA[3]);
3415 * Returns a string representation of a vector
3417 * @param {vec4} vec Vector to represent as a string
3419 * @returns {String} String representation of vec
3421 vec4.str = function (vec) {
3422 return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ', ' + vec[3] + ']';
3430 root.glMatrixArrayType = MatrixArray;
3431 root.MatrixArray = MatrixArray;
3432 root.setMatrixArrayType = setMatrixArrayType;
3433 root.determineMatrixArrayType = determineMatrixArrayType;
3434 root.glMath = glMath;
3445 glMatrixArrayType: MatrixArray,
3446 MatrixArray: MatrixArray,
3447 setMatrixArrayType: setMatrixArrayType,
3448 determineMatrixArrayType: determineMatrixArrayType,