2 * @fileoverview gl-matrix - High performance matrix and vector operations
3 * @author Brandon Jones
4 * @author Colin MacKenzie IV
8 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
35 if (typeof(exports
) === 'undefined') {
36 if(typeof define
== 'function' && typeof define
.amd
== 'object' && define
.amd
) {
42 // gl-matrix lives in a browser, define its namespaces in global
43 shim
.exports
= typeof(window
) !== 'undefined' ? window
: _global
;
47 // gl-matrix lives in commonjs, define its namespaces in exports
48 shim
.exports
= exports
;
52 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
54 Redistribution and use in source and binary forms, with or without modification,
55 are permitted provided that the following conditions are met:
57 * Redistributions of source code must retain the above copyright notice, this
58 list of conditions and the following disclaimer.
59 * Redistributions in binary form must reproduce the above copyright notice,
60 this list of conditions and the following disclaimer in the documentation
61 and/or other materials provided with the distribution.
63 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
64 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
67 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
70 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
76 var GLMAT_EPSILON
= 0.000001;
79 if(!GLMAT_ARRAY_TYPE
) {
80 var GLMAT_ARRAY_TYPE
= (typeof Float32Array
!== 'undefined') ? Float32Array
: Array
;
84 var GLMAT_RANDOM
= Math
.random
;
88 * @class Common utilities
94 * Sets the type of array used when creating new vectors and matrices
96 * @param {Type} type Array type, such as Float32Array or Array
98 glMatrix
.setMatrixArrayType = function(type
) {
99 GLMAT_ARRAY_TYPE
= type
;
102 if(typeof(exports
) !== 'undefined') {
103 exports
.glMatrix
= glMatrix
;
106 var degree
= Math
.PI
/ 180;
109 * Convert Degree To Radian
111 * @param {Number} Angle in Degrees
113 glMatrix
.toRadian = function(a
){
117 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
119 Redistribution and use in source and binary forms, with or without modification,
120 are permitted provided that the following conditions are met:
122 * Redistributions of source code must retain the above copyright notice, this
123 list of conditions and the following disclaimer.
124 * Redistributions in binary form must reproduce the above copyright notice,
125 this list of conditions and the following disclaimer in the documentation
126 and/or other materials provided with the distribution.
128 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
129 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
130 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
131 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
132 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
133 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
134 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
135 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
137 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
140 * @class 2 Dimensional Vector
147 * Creates a new, empty vec2
149 * @returns {vec2} a new 2D vector
151 vec2
.create = function() {
152 var out
= new GLMAT_ARRAY_TYPE(2);
159 * Creates a new vec2 initialized with values from an existing vector
161 * @param {vec2} a vector to clone
162 * @returns {vec2} a new 2D vector
164 vec2
.clone = function(a
) {
165 var out
= new GLMAT_ARRAY_TYPE(2);
172 * Creates a new vec2 initialized with the given values
174 * @param {Number} x X component
175 * @param {Number} y Y component
176 * @returns {vec2} a new 2D vector
178 vec2
.fromValues = function(x
, y
) {
179 var out
= new GLMAT_ARRAY_TYPE(2);
186 * Copy the values from one vec2 to another
188 * @param {vec2} out the receiving vector
189 * @param {vec2} a the source vector
190 * @returns {vec2} out
192 vec2
.copy = function(out
, a
) {
199 * Set the components of a vec2 to the given values
201 * @param {vec2} out the receiving vector
202 * @param {Number} x X component
203 * @param {Number} y Y component
204 * @returns {vec2} out
206 vec2
.set = function(out
, x
, y
) {
215 * @param {vec2} out the receiving vector
216 * @param {vec2} a the first operand
217 * @param {vec2} b the second operand
218 * @returns {vec2} out
220 vec2
.add = function(out
, a
, b
) {
221 out
[0] = a
[0] + b
[0];
222 out
[1] = a
[1] + b
[1];
227 * Subtracts vector b from vector a
229 * @param {vec2} out the receiving vector
230 * @param {vec2} a the first operand
231 * @param {vec2} b the second operand
232 * @returns {vec2} out
234 vec2
.subtract = function(out
, a
, b
) {
235 out
[0] = a
[0] - b
[0];
236 out
[1] = a
[1] - b
[1];
241 * Alias for {@link vec2.subtract}
244 vec2
.sub
= vec2
.subtract
;
247 * Multiplies two vec2's
249 * @param {vec2} out the receiving vector
250 * @param {vec2} a the first operand
251 * @param {vec2} b the second operand
252 * @returns {vec2} out
254 vec2
.multiply = function(out
, a
, b
) {
255 out
[0] = a
[0] * b
[0];
256 out
[1] = a
[1] * b
[1];
261 * Alias for {@link vec2.multiply}
264 vec2
.mul
= vec2
.multiply
;
269 * @param {vec2} out the receiving vector
270 * @param {vec2} a the first operand
271 * @param {vec2} b the second operand
272 * @returns {vec2} out
274 vec2
.divide = function(out
, a
, b
) {
275 out
[0] = a
[0] / b
[0];
276 out
[1] = a
[1] / b
[1];
281 * Alias for {@link vec2.divide}
284 vec2
.div
= vec2
.divide
;
287 * Returns the minimum of two vec2's
289 * @param {vec2} out the receiving vector
290 * @param {vec2} a the first operand
291 * @param {vec2} b the second operand
292 * @returns {vec2} out
294 vec2
.min = function(out
, a
, b
) {
295 out
[0] = Math
.min(a
[0], b
[0]);
296 out
[1] = Math
.min(a
[1], b
[1]);
301 * Returns the maximum of two vec2's
303 * @param {vec2} out the receiving vector
304 * @param {vec2} a the first operand
305 * @param {vec2} b the second operand
306 * @returns {vec2} out
308 vec2
.max = function(out
, a
, b
) {
309 out
[0] = Math
.max(a
[0], b
[0]);
310 out
[1] = Math
.max(a
[1], b
[1]);
315 * Scales a vec2 by a scalar number
317 * @param {vec2} out the receiving vector
318 * @param {vec2} a the vector to scale
319 * @param {Number} b amount to scale the vector by
320 * @returns {vec2} out
322 vec2
.scale = function(out
, a
, b
) {
329 * Adds two vec2's after scaling the second operand by a scalar value
331 * @param {vec2} out the receiving vector
332 * @param {vec2} a the first operand
333 * @param {vec2} b the second operand
334 * @param {Number} scale the amount to scale b by before adding
335 * @returns {vec2} out
337 vec2
.scaleAndAdd = function(out
, a
, b
, scale
) {
338 out
[0] = a
[0] + (b
[0] * scale
);
339 out
[1] = a
[1] + (b
[1] * scale
);
344 * Calculates the euclidian distance between two vec2's
346 * @param {vec2} a the first operand
347 * @param {vec2} b the second operand
348 * @returns {Number} distance between a and b
350 vec2
.distance = function(a
, b
) {
353 return Math
.sqrt(x
*x
+ y
*y
);
357 * Alias for {@link vec2.distance}
360 vec2
.dist
= vec2
.distance
;
363 * Calculates the squared euclidian distance between two vec2's
365 * @param {vec2} a the first operand
366 * @param {vec2} b the second operand
367 * @returns {Number} squared distance between a and b
369 vec2
.squaredDistance = function(a
, b
) {
376 * Alias for {@link vec2.squaredDistance}
379 vec2
.sqrDist
= vec2
.squaredDistance
;
382 * Calculates the length of a vec2
384 * @param {vec2} a vector to calculate length of
385 * @returns {Number} length of a
387 vec2
.length = function (a
) {
390 return Math
.sqrt(x
*x
+ y
*y
);
394 * Alias for {@link vec2.length}
397 vec2
.len
= vec2
.length
;
400 * Calculates the squared length of a vec2
402 * @param {vec2} a vector to calculate squared length of
403 * @returns {Number} squared length of a
405 vec2
.squaredLength = function (a
) {
412 * Alias for {@link vec2.squaredLength}
415 vec2
.sqrLen
= vec2
.squaredLength
;
418 * Negates the components of a vec2
420 * @param {vec2} out the receiving vector
421 * @param {vec2} a vector to negate
422 * @returns {vec2} out
424 vec2
.negate = function(out
, a
) {
431 * Returns the inverse of the components of a vec2
433 * @param {vec2} out the receiving vector
434 * @param {vec2} a vector to invert
435 * @returns {vec2} out
437 vec2
.inverse = function(out
, a
) {
446 * @param {vec2} out the receiving vector
447 * @param {vec2} a vector to normalize
448 * @returns {vec2} out
450 vec2
.normalize = function(out
, a
) {
455 //TODO: evaluate use of glm_invsqrt here?
456 len
= 1 / Math
.sqrt(len
);
464 * Calculates the dot product of two vec2's
466 * @param {vec2} a the first operand
467 * @param {vec2} b the second operand
468 * @returns {Number} dot product of a and b
470 vec2
.dot = function (a
, b
) {
471 return a
[0] * b
[0] + a
[1] * b
[1];
475 * Computes the cross product of two vec2's
476 * Note that the cross product must by definition produce a 3D vector
478 * @param {vec3} out the receiving vector
479 * @param {vec2} a the first operand
480 * @param {vec2} b the second operand
481 * @returns {vec3} out
483 vec2
.cross = function(out
, a
, b
) {
484 var z
= a
[0] * b
[1] - a
[1] * b
[0];
491 * Performs a linear interpolation between two vec2's
493 * @param {vec2} out the receiving vector
494 * @param {vec2} a the first operand
495 * @param {vec2} b the second operand
496 * @param {Number} t interpolation amount between the two inputs
497 * @returns {vec2} out
499 vec2
.lerp = function (out
, a
, b
, t
) {
502 out
[0] = ax
+ t
* (b
[0] - ax
);
503 out
[1] = ay
+ t
* (b
[1] - ay
);
508 * Generates a random vector with the given scale
510 * @param {vec2} out the receiving vector
511 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
512 * @returns {vec2} out
514 vec2
.random = function (out
, scale
) {
515 scale
= scale
|| 1.0;
516 var r
= GLMAT_RANDOM() * 2.0 * Math
.PI
;
517 out
[0] = Math
.cos(r
) * scale
;
518 out
[1] = Math
.sin(r
) * scale
;
523 * Transforms the vec2 with a mat2
525 * @param {vec2} out the receiving vector
526 * @param {vec2} a the vector to transform
527 * @param {mat2} m matrix to transform with
528 * @returns {vec2} out
530 vec2
.transformMat2 = function(out
, a
, m
) {
533 out
[0] = m
[0] * x
+ m
[2] * y
;
534 out
[1] = m
[1] * x
+ m
[3] * y
;
539 * Transforms the vec2 with a mat2d
541 * @param {vec2} out the receiving vector
542 * @param {vec2} a the vector to transform
543 * @param {mat2d} m matrix to transform with
544 * @returns {vec2} out
546 vec2
.transformMat2d = function(out
, a
, m
) {
549 out
[0] = m
[0] * x
+ m
[2] * y
+ m
[4];
550 out
[1] = m
[1] * x
+ m
[3] * y
+ m
[5];
555 * Transforms the vec2 with a mat3
556 * 3rd vector component is implicitly '1'
558 * @param {vec2} out the receiving vector
559 * @param {vec2} a the vector to transform
560 * @param {mat3} m matrix to transform with
561 * @returns {vec2} out
563 vec2
.transformMat3 = function(out
, a
, m
) {
566 out
[0] = m
[0] * x
+ m
[3] * y
+ m
[6];
567 out
[1] = m
[1] * x
+ m
[4] * y
+ m
[7];
572 * Transforms the vec2 with a mat4
573 * 3rd vector component is implicitly '0'
574 * 4th vector component is implicitly '1'
576 * @param {vec2} out the receiving vector
577 * @param {vec2} a the vector to transform
578 * @param {mat4} m matrix to transform with
579 * @returns {vec2} out
581 vec2
.transformMat4 = function(out
, a
, m
) {
584 out
[0] = m
[0] * x
+ m
[4] * y
+ m
[12];
585 out
[1] = m
[1] * x
+ m
[5] * y
+ m
[13];
590 * Perform some operation over an array of vec2s.
592 * @param {Array} a the array of vectors to iterate over
593 * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
594 * @param {Number} offset Number of elements to skip at the beginning of the array
595 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
596 * @param {Function} fn Function to call for each vector in the array
597 * @param {Object} [arg] additional argument to pass to fn
601 vec2
.forEach
= (function() {
602 var vec
= vec2
.create();
604 return function(a
, stride
, offset
, count
, fn
, arg
) {
615 l
= Math
.min((count
* stride
) + offset
, a
.length
);
620 for(i
= offset
; i
< l
; i
+= stride
) {
621 vec
[0] = a
[i
]; vec
[1] = a
[i
+1];
623 a
[i
] = vec
[0]; a
[i
+1] = vec
[1];
631 * Returns a string representation of a vector
633 * @param {vec2} vec vector to represent as a string
634 * @returns {String} string representation of the vector
636 vec2
.str = function (a
) {
637 return 'vec2(' + a
[0] + ', ' + a
[1] + ')';
640 if(typeof(exports
) !== 'undefined') {
644 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
646 Redistribution and use in source and binary forms, with or without modification,
647 are permitted provided that the following conditions are met:
649 * Redistributions of source code must retain the above copyright notice, this
650 list of conditions and the following disclaimer.
651 * Redistributions in binary form must reproduce the above copyright notice,
652 this list of conditions and the following disclaimer in the documentation
653 and/or other materials provided with the distribution.
655 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
656 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
657 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
658 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
659 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
660 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
661 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
662 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
663 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
664 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
667 * @class 3 Dimensional Vector
674 * Creates a new, empty vec3
676 * @returns {vec3} a new 3D vector
678 vec3
.create = function() {
679 var out
= new GLMAT_ARRAY_TYPE(3);
687 * Creates a new vec3 initialized with values from an existing vector
689 * @param {vec3} a vector to clone
690 * @returns {vec3} a new 3D vector
692 vec3
.clone = function(a
) {
693 var out
= new GLMAT_ARRAY_TYPE(3);
701 * Creates a new vec3 initialized with the given values
703 * @param {Number} x X component
704 * @param {Number} y Y component
705 * @param {Number} z Z component
706 * @returns {vec3} a new 3D vector
708 vec3
.fromValues = function(x
, y
, z
) {
709 var out
= new GLMAT_ARRAY_TYPE(3);
717 * Copy the values from one vec3 to another
719 * @param {vec3} out the receiving vector
720 * @param {vec3} a the source vector
721 * @returns {vec3} out
723 vec3
.copy = function(out
, a
) {
731 * Set the components of a vec3 to the given values
733 * @param {vec3} out the receiving vector
734 * @param {Number} x X component
735 * @param {Number} y Y component
736 * @param {Number} z Z component
737 * @returns {vec3} out
739 vec3
.set = function(out
, x
, y
, z
) {
749 * @param {vec3} out the receiving vector
750 * @param {vec3} a the first operand
751 * @param {vec3} b the second operand
752 * @returns {vec3} out
754 vec3
.add = function(out
, a
, b
) {
755 out
[0] = a
[0] + b
[0];
756 out
[1] = a
[1] + b
[1];
757 out
[2] = a
[2] + b
[2];
762 * Subtracts vector b from vector a
764 * @param {vec3} out the receiving vector
765 * @param {vec3} a the first operand
766 * @param {vec3} b the second operand
767 * @returns {vec3} out
769 vec3
.subtract = function(out
, a
, b
) {
770 out
[0] = a
[0] - b
[0];
771 out
[1] = a
[1] - b
[1];
772 out
[2] = a
[2] - b
[2];
777 * Alias for {@link vec3.subtract}
780 vec3
.sub
= vec3
.subtract
;
783 * Multiplies two vec3's
785 * @param {vec3} out the receiving vector
786 * @param {vec3} a the first operand
787 * @param {vec3} b the second operand
788 * @returns {vec3} out
790 vec3
.multiply = function(out
, a
, b
) {
791 out
[0] = a
[0] * b
[0];
792 out
[1] = a
[1] * b
[1];
793 out
[2] = a
[2] * b
[2];
798 * Alias for {@link vec3.multiply}
801 vec3
.mul
= vec3
.multiply
;
806 * @param {vec3} out the receiving vector
807 * @param {vec3} a the first operand
808 * @param {vec3} b the second operand
809 * @returns {vec3} out
811 vec3
.divide = function(out
, a
, b
) {
812 out
[0] = a
[0] / b
[0];
813 out
[1] = a
[1] / b
[1];
814 out
[2] = a
[2] / b
[2];
819 * Alias for {@link vec3.divide}
822 vec3
.div
= vec3
.divide
;
825 * Returns the minimum of two vec3's
827 * @param {vec3} out the receiving vector
828 * @param {vec3} a the first operand
829 * @param {vec3} b the second operand
830 * @returns {vec3} out
832 vec3
.min = function(out
, a
, b
) {
833 out
[0] = Math
.min(a
[0], b
[0]);
834 out
[1] = Math
.min(a
[1], b
[1]);
835 out
[2] = Math
.min(a
[2], b
[2]);
840 * Returns the maximum of two vec3's
842 * @param {vec3} out the receiving vector
843 * @param {vec3} a the first operand
844 * @param {vec3} b the second operand
845 * @returns {vec3} out
847 vec3
.max = function(out
, a
, b
) {
848 out
[0] = Math
.max(a
[0], b
[0]);
849 out
[1] = Math
.max(a
[1], b
[1]);
850 out
[2] = Math
.max(a
[2], b
[2]);
855 * Scales a vec3 by a scalar number
857 * @param {vec3} out the receiving vector
858 * @param {vec3} a the vector to scale
859 * @param {Number} b amount to scale the vector by
860 * @returns {vec3} out
862 vec3
.scale = function(out
, a
, b
) {
870 * Adds two vec3's after scaling the second operand by a scalar value
872 * @param {vec3} out the receiving vector
873 * @param {vec3} a the first operand
874 * @param {vec3} b the second operand
875 * @param {Number} scale the amount to scale b by before adding
876 * @returns {vec3} out
878 vec3
.scaleAndAdd = function(out
, a
, b
, scale
) {
879 out
[0] = a
[0] + (b
[0] * scale
);
880 out
[1] = a
[1] + (b
[1] * scale
);
881 out
[2] = a
[2] + (b
[2] * scale
);
886 * Calculates the euclidian distance between two vec3's
888 * @param {vec3} a the first operand
889 * @param {vec3} b the second operand
890 * @returns {Number} distance between a and b
892 vec3
.distance = function(a
, b
) {
896 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
);
900 * Alias for {@link vec3.distance}
903 vec3
.dist
= vec3
.distance
;
906 * Calculates the squared euclidian distance between two vec3's
908 * @param {vec3} a the first operand
909 * @param {vec3} b the second operand
910 * @returns {Number} squared distance between a and b
912 vec3
.squaredDistance = function(a
, b
) {
916 return x
*x
+ y
*y
+ z
*z
;
920 * Alias for {@link vec3.squaredDistance}
923 vec3
.sqrDist
= vec3
.squaredDistance
;
926 * Calculates the length of a vec3
928 * @param {vec3} a vector to calculate length of
929 * @returns {Number} length of a
931 vec3
.length = function (a
) {
935 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
);
939 * Alias for {@link vec3.length}
942 vec3
.len
= vec3
.length
;
945 * Calculates the squared length of a vec3
947 * @param {vec3} a vector to calculate squared length of
948 * @returns {Number} squared length of a
950 vec3
.squaredLength = function (a
) {
954 return x
*x
+ y
*y
+ z
*z
;
958 * Alias for {@link vec3.squaredLength}
961 vec3
.sqrLen
= vec3
.squaredLength
;
964 * Negates the components of a vec3
966 * @param {vec3} out the receiving vector
967 * @param {vec3} a vector to negate
968 * @returns {vec3} out
970 vec3
.negate = function(out
, a
) {
978 * Returns the inverse of the components of a vec3
980 * @param {vec3} out the receiving vector
981 * @param {vec3} a vector to invert
982 * @returns {vec3} out
984 vec3
.inverse = function(out
, a
) {
994 * @param {vec3} out the receiving vector
995 * @param {vec3} a vector to normalize
996 * @returns {vec3} out
998 vec3
.normalize = function(out
, a
) {
1002 var len
= x
*x
+ y
*y
+ z
*z
;
1004 //TODO: evaluate use of glm_invsqrt here?
1005 len
= 1 / Math
.sqrt(len
);
1006 out
[0] = a
[0] * len
;
1007 out
[1] = a
[1] * len
;
1008 out
[2] = a
[2] * len
;
1014 * Calculates the dot product of two vec3's
1016 * @param {vec3} a the first operand
1017 * @param {vec3} b the second operand
1018 * @returns {Number} dot product of a and b
1020 vec3
.dot = function (a
, b
) {
1021 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
1025 * Computes the cross product of two vec3's
1027 * @param {vec3} out the receiving vector
1028 * @param {vec3} a the first operand
1029 * @param {vec3} b the second operand
1030 * @returns {vec3} out
1032 vec3
.cross = function(out
, a
, b
) {
1033 var ax
= a
[0], ay
= a
[1], az
= a
[2],
1034 bx
= b
[0], by
= b
[1], bz
= b
[2];
1036 out
[0] = ay
* bz
- az
* by
;
1037 out
[1] = az
* bx
- ax
* bz
;
1038 out
[2] = ax
* by
- ay
* bx
;
1043 * Performs a linear interpolation between two vec3's
1045 * @param {vec3} out the receiving vector
1046 * @param {vec3} a the first operand
1047 * @param {vec3} b the second operand
1048 * @param {Number} t interpolation amount between the two inputs
1049 * @returns {vec3} out
1051 vec3
.lerp = function (out
, a
, b
, t
) {
1055 out
[0] = ax
+ t
* (b
[0] - ax
);
1056 out
[1] = ay
+ t
* (b
[1] - ay
);
1057 out
[2] = az
+ t
* (b
[2] - az
);
1062 * Generates a random vector with the given scale
1064 * @param {vec3} out the receiving vector
1065 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1066 * @returns {vec3} out
1068 vec3
.random = function (out
, scale
) {
1069 scale
= scale
|| 1.0;
1071 var r
= GLMAT_RANDOM() * 2.0 * Math
.PI
;
1072 var z
= (GLMAT_RANDOM() * 2.0) - 1.0;
1073 var zScale
= Math
.sqrt(1.0-z
*z
) * scale
;
1075 out
[0] = Math
.cos(r
) * zScale
;
1076 out
[1] = Math
.sin(r
) * zScale
;
1082 * Transforms the vec3 with a mat4.
1083 * 4th vector component is implicitly '1'
1085 * @param {vec3} out the receiving vector
1086 * @param {vec3} a the vector to transform
1087 * @param {mat4} m matrix to transform with
1088 * @returns {vec3} out
1090 vec3
.transformMat4 = function(out
, a
, m
) {
1091 var x
= a
[0], y
= a
[1], z
= a
[2],
1092 w
= m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1094 out
[0] = (m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12]) / w
;
1095 out
[1] = (m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13]) / w
;
1096 out
[2] = (m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14]) / w
;
1101 * Transforms the vec3 with a mat3.
1103 * @param {vec3} out the receiving vector
1104 * @param {vec3} a the vector to transform
1105 * @param {mat4} m the 3x3 matrix to transform with
1106 * @returns {vec3} out
1108 vec3
.transformMat3 = function(out
, a
, m
) {
1109 var x
= a
[0], y
= a
[1], z
= a
[2];
1110 out
[0] = x
* m
[0] + y
* m
[3] + z
* m
[6];
1111 out
[1] = x
* m
[1] + y
* m
[4] + z
* m
[7];
1112 out
[2] = x
* m
[2] + y
* m
[5] + z
* m
[8];
1117 * Transforms the vec3 with a quat
1119 * @param {vec3} out the receiving vector
1120 * @param {vec3} a the vector to transform
1121 * @param {quat} q quaternion to transform with
1122 * @returns {vec3} out
1124 vec3
.transformQuat = function(out
, a
, q
) {
1125 // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
1127 var x
= a
[0], y
= a
[1], z
= a
[2],
1128 qx
= q
[0], qy
= q
[1], qz
= q
[2], qw
= q
[3],
1130 // calculate quat * vec
1131 ix
= qw
* x
+ qy
* z
- qz
* y
,
1132 iy
= qw
* y
+ qz
* x
- qx
* z
,
1133 iz
= qw
* z
+ qx
* y
- qy
* x
,
1134 iw
= -qx
* x
- qy
* y
- qz
* z
;
1136 // calculate result * inverse quat
1137 out
[0] = ix
* qw
+ iw
* -qx
+ iy
* -qz
- iz
* -qy
;
1138 out
[1] = iy
* qw
+ iw
* -qy
+ iz
* -qx
- ix
* -qz
;
1139 out
[2] = iz
* qw
+ iw
* -qz
+ ix
* -qy
- iy
* -qx
;
1144 * Rotate a 3D vector around the x-axis
1145 * @param {vec3} out The receiving vec3
1146 * @param {vec3} a The vec3 point to rotate
1147 * @param {vec3} b The origin of the rotation
1148 * @param {Number} c The angle of rotation
1149 * @returns {vec3} out
1151 vec3
.rotateX = function(out
, a
, b
, c
){
1153 //Translate point to the origin
1160 r
[1] = p
[1]*Math
.cos(c
) - p
[2]*Math
.sin(c
);
1161 r
[2] = p
[1]*Math
.sin(c
) + p
[2]*Math
.cos(c
);
1163 //translate to correct position
1164 out
[0] = r
[0] + b
[0];
1165 out
[1] = r
[1] + b
[1];
1166 out
[2] = r
[2] + b
[2];
1172 * Rotate a 3D vector around the y-axis
1173 * @param {vec3} out The receiving vec3
1174 * @param {vec3} a The vec3 point to rotate
1175 * @param {vec3} b The origin of the rotation
1176 * @param {Number} c The angle of rotation
1177 * @returns {vec3} out
1179 vec3
.rotateY = function(out
, a
, b
, c
){
1181 //Translate point to the origin
1187 r
[0] = p
[2]*Math
.sin(c
) + p
[0]*Math
.cos(c
);
1189 r
[2] = p
[2]*Math
.cos(c
) - p
[0]*Math
.sin(c
);
1191 //translate to correct position
1192 out
[0] = r
[0] + b
[0];
1193 out
[1] = r
[1] + b
[1];
1194 out
[2] = r
[2] + b
[2];
1200 * Rotate a 3D vector around the z-axis
1201 * @param {vec3} out The receiving vec3
1202 * @param {vec3} a The vec3 point to rotate
1203 * @param {vec3} b The origin of the rotation
1204 * @param {Number} c The angle of rotation
1205 * @returns {vec3} out
1207 vec3
.rotateZ = function(out
, a
, b
, c
){
1209 //Translate point to the origin
1215 r
[0] = p
[0]*Math
.cos(c
) - p
[1]*Math
.sin(c
);
1216 r
[1] = p
[0]*Math
.sin(c
) + p
[1]*Math
.cos(c
);
1219 //translate to correct position
1220 out
[0] = r
[0] + b
[0];
1221 out
[1] = r
[1] + b
[1];
1222 out
[2] = r
[2] + b
[2];
1228 * Perform some operation over an array of vec3s.
1230 * @param {Array} a the array of vectors to iterate over
1231 * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
1232 * @param {Number} offset Number of elements to skip at the beginning of the array
1233 * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
1234 * @param {Function} fn Function to call for each vector in the array
1235 * @param {Object} [arg] additional argument to pass to fn
1236 * @returns {Array} a
1239 vec3
.forEach
= (function() {
1240 var vec
= vec3
.create();
1242 return function(a
, stride
, offset
, count
, fn
, arg
) {
1253 l
= Math
.min((count
* stride
) + offset
, a
.length
);
1258 for(i
= offset
; i
< l
; i
+= stride
) {
1259 vec
[0] = a
[i
]; vec
[1] = a
[i
+1]; vec
[2] = a
[i
+2];
1261 a
[i
] = vec
[0]; a
[i
+1] = vec
[1]; a
[i
+2] = vec
[2];
1269 * Get the angle between two 3D vectors
1270 * @param {vec3} a The first operand
1271 * @param {vec3} b The second operand
1272 * @returns {Number} The angle in radians
1274 vec3
.angle = function(a
, b
) {
1276 var tempA
= vec3
.fromValues(a
[0], a
[1], a
[2]);
1277 var tempB
= vec3
.fromValues(b
[0], b
[1], b
[2]);
1279 vec3
.normalize(tempA
, tempA
);
1280 vec3
.normalize(tempB
, tempB
);
1282 var cosine
= vec3
.dot(tempA
, tempB
);
1287 return Math
.acos(cosine
);
1292 * Returns a string representation of a vector
1294 * @param {vec3} vec vector to represent as a string
1295 * @returns {String} string representation of the vector
1297 vec3
.str = function (a
) {
1298 return 'vec3(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ')';
1301 if(typeof(exports
) !== 'undefined') {
1302 exports
.vec3
= vec3
;
1305 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1307 Redistribution and use in source and binary forms, with or without modification,
1308 are permitted provided that the following conditions are met:
1310 * Redistributions of source code must retain the above copyright notice, this
1311 list of conditions and the following disclaimer.
1312 * Redistributions in binary form must reproduce the above copyright notice,
1313 this list of conditions and the following disclaimer in the documentation
1314 and/or other materials provided with the distribution.
1316 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1317 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1318 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1319 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1320 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1321 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1322 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1323 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1324 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1325 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1328 * @class 4 Dimensional Vector
1335 * Creates a new, empty vec4
1337 * @returns {vec4} a new 4D vector
1339 vec4
.create = function() {
1340 var out
= new GLMAT_ARRAY_TYPE(4);
1349 * Creates a new vec4 initialized with values from an existing vector
1351 * @param {vec4} a vector to clone
1352 * @returns {vec4} a new 4D vector
1354 vec4
.clone = function(a
) {
1355 var out
= new GLMAT_ARRAY_TYPE(4);
1364 * Creates a new vec4 initialized with the given values
1366 * @param {Number} x X component
1367 * @param {Number} y Y component
1368 * @param {Number} z Z component
1369 * @param {Number} w W component
1370 * @returns {vec4} a new 4D vector
1372 vec4
.fromValues = function(x
, y
, z
, w
) {
1373 var out
= new GLMAT_ARRAY_TYPE(4);
1382 * Copy the values from one vec4 to another
1384 * @param {vec4} out the receiving vector
1385 * @param {vec4} a the source vector
1386 * @returns {vec4} out
1388 vec4
.copy = function(out
, a
) {
1397 * Set the components of a vec4 to the given values
1399 * @param {vec4} out the receiving vector
1400 * @param {Number} x X component
1401 * @param {Number} y Y component
1402 * @param {Number} z Z component
1403 * @param {Number} w W component
1404 * @returns {vec4} out
1406 vec4
.set = function(out
, x
, y
, z
, w
) {
1417 * @param {vec4} out the receiving vector
1418 * @param {vec4} a the first operand
1419 * @param {vec4} b the second operand
1420 * @returns {vec4} out
1422 vec4
.add = function(out
, a
, b
) {
1423 out
[0] = a
[0] + b
[0];
1424 out
[1] = a
[1] + b
[1];
1425 out
[2] = a
[2] + b
[2];
1426 out
[3] = a
[3] + b
[3];
1431 * Subtracts vector b from vector a
1433 * @param {vec4} out the receiving vector
1434 * @param {vec4} a the first operand
1435 * @param {vec4} b the second operand
1436 * @returns {vec4} out
1438 vec4
.subtract = function(out
, a
, b
) {
1439 out
[0] = a
[0] - b
[0];
1440 out
[1] = a
[1] - b
[1];
1441 out
[2] = a
[2] - b
[2];
1442 out
[3] = a
[3] - b
[3];
1447 * Alias for {@link vec4.subtract}
1450 vec4
.sub
= vec4
.subtract
;
1453 * Multiplies two vec4's
1455 * @param {vec4} out the receiving vector
1456 * @param {vec4} a the first operand
1457 * @param {vec4} b the second operand
1458 * @returns {vec4} out
1460 vec4
.multiply = function(out
, a
, b
) {
1461 out
[0] = a
[0] * b
[0];
1462 out
[1] = a
[1] * b
[1];
1463 out
[2] = a
[2] * b
[2];
1464 out
[3] = a
[3] * b
[3];
1469 * Alias for {@link vec4.multiply}
1472 vec4
.mul
= vec4
.multiply
;
1475 * Divides two vec4's
1477 * @param {vec4} out the receiving vector
1478 * @param {vec4} a the first operand
1479 * @param {vec4} b the second operand
1480 * @returns {vec4} out
1482 vec4
.divide = function(out
, a
, b
) {
1483 out
[0] = a
[0] / b
[0];
1484 out
[1] = a
[1] / b
[1];
1485 out
[2] = a
[2] / b
[2];
1486 out
[3] = a
[3] / b
[3];
1491 * Alias for {@link vec4.divide}
1494 vec4
.div
= vec4
.divide
;
1497 * Returns the minimum of two vec4's
1499 * @param {vec4} out the receiving vector
1500 * @param {vec4} a the first operand
1501 * @param {vec4} b the second operand
1502 * @returns {vec4} out
1504 vec4
.min = function(out
, a
, b
) {
1505 out
[0] = Math
.min(a
[0], b
[0]);
1506 out
[1] = Math
.min(a
[1], b
[1]);
1507 out
[2] = Math
.min(a
[2], b
[2]);
1508 out
[3] = Math
.min(a
[3], b
[3]);
1513 * Returns the maximum of two vec4's
1515 * @param {vec4} out the receiving vector
1516 * @param {vec4} a the first operand
1517 * @param {vec4} b the second operand
1518 * @returns {vec4} out
1520 vec4
.max = function(out
, a
, b
) {
1521 out
[0] = Math
.max(a
[0], b
[0]);
1522 out
[1] = Math
.max(a
[1], b
[1]);
1523 out
[2] = Math
.max(a
[2], b
[2]);
1524 out
[3] = Math
.max(a
[3], b
[3]);
1529 * Scales a vec4 by a scalar number
1531 * @param {vec4} out the receiving vector
1532 * @param {vec4} a the vector to scale
1533 * @param {Number} b amount to scale the vector by
1534 * @returns {vec4} out
1536 vec4
.scale = function(out
, a
, b
) {
1545 * Adds two vec4's after scaling the second operand by a scalar value
1547 * @param {vec4} out the receiving vector
1548 * @param {vec4} a the first operand
1549 * @param {vec4} b the second operand
1550 * @param {Number} scale the amount to scale b by before adding
1551 * @returns {vec4} out
1553 vec4
.scaleAndAdd = function(out
, a
, b
, scale
) {
1554 out
[0] = a
[0] + (b
[0] * scale
);
1555 out
[1] = a
[1] + (b
[1] * scale
);
1556 out
[2] = a
[2] + (b
[2] * scale
);
1557 out
[3] = a
[3] + (b
[3] * scale
);
1562 * Calculates the euclidian distance between two vec4's
1564 * @param {vec4} a the first operand
1565 * @param {vec4} b the second operand
1566 * @returns {Number} distance between a and b
1568 vec4
.distance = function(a
, b
) {
1569 var x
= b
[0] - a
[0],
1573 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
+ w
*w
);
1577 * Alias for {@link vec4.distance}
1580 vec4
.dist
= vec4
.distance
;
1583 * Calculates the squared euclidian distance between two vec4's
1585 * @param {vec4} a the first operand
1586 * @param {vec4} b the second operand
1587 * @returns {Number} squared distance between a and b
1589 vec4
.squaredDistance = function(a
, b
) {
1590 var x
= b
[0] - a
[0],
1594 return x
*x
+ y
*y
+ z
*z
+ w
*w
;
1598 * Alias for {@link vec4.squaredDistance}
1601 vec4
.sqrDist
= vec4
.squaredDistance
;
1604 * Calculates the length of a vec4
1606 * @param {vec4} a vector to calculate length of
1607 * @returns {Number} length of a
1609 vec4
.length = function (a
) {
1614 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
+ w
*w
);
1618 * Alias for {@link vec4.length}
1621 vec4
.len
= vec4
.length
;
1624 * Calculates the squared length of a vec4
1626 * @param {vec4} a vector to calculate squared length of
1627 * @returns {Number} squared length of a
1629 vec4
.squaredLength = function (a
) {
1634 return x
*x
+ y
*y
+ z
*z
+ w
*w
;
1638 * Alias for {@link vec4.squaredLength}
1641 vec4
.sqrLen
= vec4
.squaredLength
;
1644 * Negates the components of a vec4
1646 * @param {vec4} out the receiving vector
1647 * @param {vec4} a vector to negate
1648 * @returns {vec4} out
1650 vec4
.negate = function(out
, a
) {
1659 * Returns the inverse of the components of a vec4
1661 * @param {vec4} out the receiving vector
1662 * @param {vec4} a vector to invert
1663 * @returns {vec4} out
1665 vec4
.inverse = function(out
, a
) {
1666 out
[0] = 1.0 / a
[0];
1667 out
[1] = 1.0 / a
[1];
1668 out
[2] = 1.0 / a
[2];
1669 out
[3] = 1.0 / a
[3];
1676 * @param {vec4} out the receiving vector
1677 * @param {vec4} a vector to normalize
1678 * @returns {vec4} out
1680 vec4
.normalize = function(out
, a
) {
1685 var len
= x
*x
+ y
*y
+ z
*z
+ w
*w
;
1687 len
= 1 / Math
.sqrt(len
);
1688 out
[0] = a
[0] * len
;
1689 out
[1] = a
[1] * len
;
1690 out
[2] = a
[2] * len
;
1691 out
[3] = a
[3] * len
;
1697 * Calculates the dot product of two vec4's
1699 * @param {vec4} a the first operand
1700 * @param {vec4} b the second operand
1701 * @returns {Number} dot product of a and b
1703 vec4
.dot = function (a
, b
) {
1704 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
1708 * Performs a linear interpolation between two vec4's
1710 * @param {vec4} out the receiving vector
1711 * @param {vec4} a the first operand
1712 * @param {vec4} b the second operand
1713 * @param {Number} t interpolation amount between the two inputs
1714 * @returns {vec4} out
1716 vec4
.lerp = function (out
, a
, b
, t
) {
1721 out
[0] = ax
+ t
* (b
[0] - ax
);
1722 out
[1] = ay
+ t
* (b
[1] - ay
);
1723 out
[2] = az
+ t
* (b
[2] - az
);
1724 out
[3] = aw
+ t
* (b
[3] - aw
);
1729 * Generates a random vector with the given scale
1731 * @param {vec4} out the receiving vector
1732 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1733 * @returns {vec4} out
1735 vec4
.random = function (out
, scale
) {
1736 scale
= scale
|| 1.0;
1738 //TODO: This is a pretty awful way of doing this. Find something better.
1739 out
[0] = GLMAT_RANDOM();
1740 out
[1] = GLMAT_RANDOM();
1741 out
[2] = GLMAT_RANDOM();
1742 out
[3] = GLMAT_RANDOM();
1743 vec4
.normalize(out
, out
);
1744 vec4
.scale(out
, out
, scale
);
1749 * Transforms the vec4 with a mat4.
1751 * @param {vec4} out the receiving vector
1752 * @param {vec4} a the vector to transform
1753 * @param {mat4} m matrix to transform with
1754 * @returns {vec4} out
1756 vec4
.transformMat4 = function(out
, a
, m
) {
1757 var x
= a
[0], y
= a
[1], z
= a
[2], w
= a
[3];
1758 out
[0] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12] * w
;
1759 out
[1] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13] * w
;
1760 out
[2] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14] * w
;
1761 out
[3] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15] * w
;
1766 * Transforms the vec4 with a quat
1768 * @param {vec4} out the receiving vector
1769 * @param {vec4} a the vector to transform
1770 * @param {quat} q quaternion to transform with
1771 * @returns {vec4} out
1773 vec4
.transformQuat = function(out
, a
, q
) {
1774 var x
= a
[0], y
= a
[1], z
= a
[2],
1775 qx
= q
[0], qy
= q
[1], qz
= q
[2], qw
= q
[3],
1777 // calculate quat * vec
1778 ix
= qw
* x
+ qy
* z
- qz
* y
,
1779 iy
= qw
* y
+ qz
* x
- qx
* z
,
1780 iz
= qw
* z
+ qx
* y
- qy
* x
,
1781 iw
= -qx
* x
- qy
* y
- qz
* z
;
1783 // calculate result * inverse quat
1784 out
[0] = ix
* qw
+ iw
* -qx
+ iy
* -qz
- iz
* -qy
;
1785 out
[1] = iy
* qw
+ iw
* -qy
+ iz
* -qx
- ix
* -qz
;
1786 out
[2] = iz
* qw
+ iw
* -qz
+ ix
* -qy
- iy
* -qx
;
1791 * Perform some operation over an array of vec4s.
1793 * @param {Array} a the array of vectors to iterate over
1794 * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
1795 * @param {Number} offset Number of elements to skip at the beginning of the array
1796 * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
1797 * @param {Function} fn Function to call for each vector in the array
1798 * @param {Object} [arg] additional argument to pass to fn
1799 * @returns {Array} a
1802 vec4
.forEach
= (function() {
1803 var vec
= vec4
.create();
1805 return function(a
, stride
, offset
, count
, fn
, arg
) {
1816 l
= Math
.min((count
* stride
) + offset
, a
.length
);
1821 for(i
= offset
; i
< l
; i
+= stride
) {
1822 vec
[0] = a
[i
]; vec
[1] = a
[i
+1]; vec
[2] = a
[i
+2]; vec
[3] = a
[i
+3];
1824 a
[i
] = vec
[0]; a
[i
+1] = vec
[1]; a
[i
+2] = vec
[2]; a
[i
+3] = vec
[3];
1832 * Returns a string representation of a vector
1834 * @param {vec4} vec vector to represent as a string
1835 * @returns {String} string representation of the vector
1837 vec4
.str = function (a
) {
1838 return 'vec4(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
1841 if(typeof(exports
) !== 'undefined') {
1842 exports
.vec4
= vec4
;
1845 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1847 Redistribution and use in source and binary forms, with or without modification,
1848 are permitted provided that the following conditions are met:
1850 * Redistributions of source code must retain the above copyright notice, this
1851 list of conditions and the following disclaimer.
1852 * Redistributions in binary form must reproduce the above copyright notice,
1853 this list of conditions and the following disclaimer in the documentation
1854 and/or other materials provided with the distribution.
1856 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1857 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1858 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1859 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1860 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1861 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1862 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1863 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1864 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1865 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1875 * Creates a new identity mat2
1877 * @returns {mat2} a new 2x2 matrix
1879 mat2
.create = function() {
1880 var out
= new GLMAT_ARRAY_TYPE(4);
1889 * Creates a new mat2 initialized with values from an existing matrix
1891 * @param {mat2} a matrix to clone
1892 * @returns {mat2} a new 2x2 matrix
1894 mat2
.clone = function(a
) {
1895 var out
= new GLMAT_ARRAY_TYPE(4);
1904 * Copy the values from one mat2 to another
1906 * @param {mat2} out the receiving matrix
1907 * @param {mat2} a the source matrix
1908 * @returns {mat2} out
1910 mat2
.copy = function(out
, a
) {
1919 * Set a mat2 to the identity matrix
1921 * @param {mat2} out the receiving matrix
1922 * @returns {mat2} out
1924 mat2
.identity = function(out
) {
1933 * Transpose the values of a mat2
1935 * @param {mat2} out the receiving matrix
1936 * @param {mat2} a the source matrix
1937 * @returns {mat2} out
1939 mat2
.transpose = function(out
, a
) {
1940 // If we are transposing ourselves we can skip a few steps but have to cache some values
1958 * @param {mat2} out the receiving matrix
1959 * @param {mat2} a the source matrix
1960 * @returns {mat2} out
1962 mat2
.invert = function(out
, a
) {
1963 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
1965 // Calculate the determinant
1966 det
= a0
* a3
- a2
* a1
;
1982 * Calculates the adjugate of a mat2
1984 * @param {mat2} out the receiving matrix
1985 * @param {mat2} a the source matrix
1986 * @returns {mat2} out
1988 mat2
.adjoint = function(out
, a
) {
1989 // Caching this value is nessecary if out == a
2000 * Calculates the determinant of a mat2
2002 * @param {mat2} a the source matrix
2003 * @returns {Number} determinant of a
2005 mat2
.determinant = function (a
) {
2006 return a
[0] * a
[3] - a
[2] * a
[1];
2010 * Multiplies two mat2's
2012 * @param {mat2} out the receiving matrix
2013 * @param {mat2} a the first operand
2014 * @param {mat2} b the second operand
2015 * @returns {mat2} out
2017 mat2
.multiply = function (out
, a
, b
) {
2018 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3];
2019 var b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3];
2020 out
[0] = a0
* b0
+ a2
* b1
;
2021 out
[1] = a1
* b0
+ a3
* b1
;
2022 out
[2] = a0
* b2
+ a2
* b3
;
2023 out
[3] = a1
* b2
+ a3
* b3
;
2028 * Alias for {@link mat2.multiply}
2031 mat2
.mul
= mat2
.multiply
;
2034 * Rotates a mat2 by the given angle
2036 * @param {mat2} out the receiving matrix
2037 * @param {mat2} a the matrix to rotate
2038 * @param {Number} rad the angle to rotate the matrix by
2039 * @returns {mat2} out
2041 mat2
.rotate = function (out
, a
, rad
) {
2042 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
2045 out
[0] = a0
* c
+ a2
* s
;
2046 out
[1] = a1
* c
+ a3
* s
;
2047 out
[2] = a0
* -s
+ a2
* c
;
2048 out
[3] = a1
* -s
+ a3
* c
;
2053 * Scales the mat2 by the dimensions in the given vec2
2055 * @param {mat2} out the receiving matrix
2056 * @param {mat2} a the matrix to rotate
2057 * @param {vec2} v the vec2 to scale the matrix by
2058 * @returns {mat2} out
2060 mat2
.scale = function(out
, a
, v
) {
2061 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
2062 v0
= v
[0], v1
= v
[1];
2071 * Returns a string representation of a mat2
2073 * @param {mat2} mat matrix to represent as a string
2074 * @returns {String} string representation of the matrix
2076 mat2
.str = function (a
) {
2077 return 'mat2(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
2081 * Returns Frobenius norm of a mat2
2083 * @param {mat2} a the matrix to calculate Frobenius norm of
2084 * @returns {Number} Frobenius norm
2086 mat2
.frob = function (a
) {
2087 return(Math
.sqrt(Math
.pow(a
[0], 2) + Math
.pow(a
[1], 2) + Math
.pow(a
[2], 2) + Math
.pow(a
[3], 2)))
2091 * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
2092 * @param {mat2} L the lower triangular matrix
2093 * @param {mat2} D the diagonal matrix
2094 * @param {mat2} U the upper triangular matrix
2095 * @param {mat2} a the input matrix to factorize
2098 mat2
.LDU = function (L
, D
, U
, a
) {
2102 U
[3] = a
[3] - L
[2] * U
[1];
2106 if(typeof(exports
) !== 'undefined') {
2107 exports
.mat2
= mat2
;
2110 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2112 Redistribution and use in source and binary forms, with or without modification,
2113 are permitted provided that the following conditions are met:
2115 * Redistributions of source code must retain the above copyright notice, this
2116 list of conditions and the following disclaimer.
2117 * Redistributions in binary form must reproduce the above copyright notice,
2118 this list of conditions and the following disclaimer in the documentation
2119 and/or other materials provided with the distribution.
2121 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2122 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2123 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2124 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2125 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2126 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2127 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2128 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2129 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2130 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2137 * A mat2d contains six elements defined as:
2142 * This is a short form for the 3x3 matrix:
2148 * The last row is ignored so the array is shorter and operations are faster.
2154 * Creates a new identity mat2d
2156 * @returns {mat2d} a new 2x3 matrix
2158 mat2d
.create = function() {
2159 var out
= new GLMAT_ARRAY_TYPE(6);
2170 * Creates a new mat2d initialized with values from an existing matrix
2172 * @param {mat2d} a matrix to clone
2173 * @returns {mat2d} a new 2x3 matrix
2175 mat2d
.clone = function(a
) {
2176 var out
= new GLMAT_ARRAY_TYPE(6);
2187 * Copy the values from one mat2d to another
2189 * @param {mat2d} out the receiving matrix
2190 * @param {mat2d} a the source matrix
2191 * @returns {mat2d} out
2193 mat2d
.copy = function(out
, a
) {
2204 * Set a mat2d to the identity matrix
2206 * @param {mat2d} out the receiving matrix
2207 * @returns {mat2d} out
2209 mat2d
.identity = function(out
) {
2222 * @param {mat2d} out the receiving matrix
2223 * @param {mat2d} a the source matrix
2224 * @returns {mat2d} out
2226 mat2d
.invert = function(out
, a
) {
2227 var aa
= a
[0], ab
= a
[1], ac
= a
[2], ad
= a
[3],
2228 atx
= a
[4], aty
= a
[5];
2230 var det
= aa
* ad
- ab
* ac
;
2240 out
[4] = (ac
* aty
- ad
* atx
) * det
;
2241 out
[5] = (ab
* atx
- aa
* aty
) * det
;
2246 * Calculates the determinant of a mat2d
2248 * @param {mat2d} a the source matrix
2249 * @returns {Number} determinant of a
2251 mat2d
.determinant = function (a
) {
2252 return a
[0] * a
[3] - a
[1] * a
[2];
2256 * Multiplies two mat2d's
2258 * @param {mat2d} out the receiving matrix
2259 * @param {mat2d} a the first operand
2260 * @param {mat2d} b the second operand
2261 * @returns {mat2d} out
2263 mat2d
.multiply = function (out
, a
, b
) {
2264 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2265 b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3], b4
= b
[4], b5
= b
[5];
2266 out
[0] = a0
* b0
+ a2
* b1
;
2267 out
[1] = a1
* b0
+ a3
* b1
;
2268 out
[2] = a0
* b2
+ a2
* b3
;
2269 out
[3] = a1
* b2
+ a3
* b3
;
2270 out
[4] = a0
* b4
+ a2
* b5
+ a4
;
2271 out
[5] = a1
* b4
+ a3
* b5
+ a5
;
2276 * Alias for {@link mat2d.multiply}
2279 mat2d
.mul
= mat2d
.multiply
;
2283 * Rotates a mat2d by the given angle
2285 * @param {mat2d} out the receiving matrix
2286 * @param {mat2d} a the matrix to rotate
2287 * @param {Number} rad the angle to rotate the matrix by
2288 * @returns {mat2d} out
2290 mat2d
.rotate = function (out
, a
, rad
) {
2291 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2294 out
[0] = a0
* c
+ a2
* s
;
2295 out
[1] = a1
* c
+ a3
* s
;
2296 out
[2] = a0
* -s
+ a2
* c
;
2297 out
[3] = a1
* -s
+ a3
* c
;
2304 * Scales the mat2d by the dimensions in the given vec2
2306 * @param {mat2d} out the receiving matrix
2307 * @param {mat2d} a the matrix to translate
2308 * @param {vec2} v the vec2 to scale the matrix by
2309 * @returns {mat2d} out
2311 mat2d
.scale = function(out
, a
, v
) {
2312 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2313 v0
= v
[0], v1
= v
[1];
2324 * Translates the mat2d by the dimensions in the given vec2
2326 * @param {mat2d} out the receiving matrix
2327 * @param {mat2d} a the matrix to translate
2328 * @param {vec2} v the vec2 to translate the matrix by
2329 * @returns {mat2d} out
2331 mat2d
.translate = function(out
, a
, v
) {
2332 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2333 v0
= v
[0], v1
= v
[1];
2338 out
[4] = a0
* v0
+ a2
* v1
+ a4
;
2339 out
[5] = a1
* v0
+ a3
* v1
+ a5
;
2344 * Returns a string representation of a mat2d
2346 * @param {mat2d} a matrix to represent as a string
2347 * @returns {String} string representation of the matrix
2349 mat2d
.str = function (a
) {
2350 return 'mat2d(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' +
2351 a
[3] + ', ' + a
[4] + ', ' + a
[5] + ')';
2355 * Returns Frobenius norm of a mat2d
2357 * @param {mat2d} a the matrix to calculate Frobenius norm of
2358 * @returns {Number} Frobenius norm
2360 mat2d
.frob = function (a
) {
2361 return(Math
.sqrt(Math
.pow(a
[0], 2) + Math
.pow(a
[1], 2) + Math
.pow(a
[2], 2) + Math
.pow(a
[3], 2) + Math
.pow(a
[4], 2) + Math
.pow(a
[5], 2) + 1))
2364 if(typeof(exports
) !== 'undefined') {
2365 exports
.mat2d
= mat2d
;
2368 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2370 Redistribution and use in source and binary forms, with or without modification,
2371 are permitted provided that the following conditions are met:
2373 * Redistributions of source code must retain the above copyright notice, this
2374 list of conditions and the following disclaimer.
2375 * Redistributions in binary form must reproduce the above copyright notice,
2376 this list of conditions and the following disclaimer in the documentation
2377 and/or other materials provided with the distribution.
2379 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2380 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2381 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2382 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2383 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2384 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2385 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2386 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2387 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2388 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2398 * Creates a new identity mat3
2400 * @returns {mat3} a new 3x3 matrix
2402 mat3
.create = function() {
2403 var out
= new GLMAT_ARRAY_TYPE(9);
2417 * Copies the upper-left 3x3 values into the given mat3.
2419 * @param {mat3} out the receiving 3x3 matrix
2420 * @param {mat4} a the source 4x4 matrix
2421 * @returns {mat3} out
2423 mat3
.fromMat4 = function(out
, a
) {
2437 * Creates a new mat3 initialized with values from an existing matrix
2439 * @param {mat3} a matrix to clone
2440 * @returns {mat3} a new 3x3 matrix
2442 mat3
.clone = function(a
) {
2443 var out
= new GLMAT_ARRAY_TYPE(9);
2457 * Copy the values from one mat3 to another
2459 * @param {mat3} out the receiving matrix
2460 * @param {mat3} a the source matrix
2461 * @returns {mat3} out
2463 mat3
.copy = function(out
, a
) {
2477 * Set a mat3 to the identity matrix
2479 * @param {mat3} out the receiving matrix
2480 * @returns {mat3} out
2482 mat3
.identity = function(out
) {
2496 * Transpose the values of a mat3
2498 * @param {mat3} out the receiving matrix
2499 * @param {mat3} a the source matrix
2500 * @returns {mat3} out
2502 mat3
.transpose = function(out
, a
) {
2503 // If we are transposing ourselves we can skip a few steps but have to cache some values
2505 var a01
= a
[1], a02
= a
[2], a12
= a
[5];
2530 * @param {mat3} out the receiving matrix
2531 * @param {mat3} a the source matrix
2532 * @returns {mat3} out
2534 mat3
.invert = function(out
, a
) {
2535 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2536 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2537 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2539 b01
= a22
* a11
- a12
* a21
,
2540 b11
= -a22
* a10
+ a12
* a20
,
2541 b21
= a21
* a10
- a11
* a20
,
2543 // Calculate the determinant
2544 det
= a00
* b01
+ a01
* b11
+ a02
* b21
;
2552 out
[1] = (-a22
* a01
+ a02
* a21
) * det
;
2553 out
[2] = (a12
* a01
- a02
* a11
) * det
;
2555 out
[4] = (a22
* a00
- a02
* a20
) * det
;
2556 out
[5] = (-a12
* a00
+ a02
* a10
) * det
;
2558 out
[7] = (-a21
* a00
+ a01
* a20
) * det
;
2559 out
[8] = (a11
* a00
- a01
* a10
) * det
;
2564 * Calculates the adjugate of a mat3
2566 * @param {mat3} out the receiving matrix
2567 * @param {mat3} a the source matrix
2568 * @returns {mat3} out
2570 mat3
.adjoint = function(out
, a
) {
2571 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2572 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2573 a20
= a
[6], a21
= a
[7], a22
= a
[8];
2575 out
[0] = (a11
* a22
- a12
* a21
);
2576 out
[1] = (a02
* a21
- a01
* a22
);
2577 out
[2] = (a01
* a12
- a02
* a11
);
2578 out
[3] = (a12
* a20
- a10
* a22
);
2579 out
[4] = (a00
* a22
- a02
* a20
);
2580 out
[5] = (a02
* a10
- a00
* a12
);
2581 out
[6] = (a10
* a21
- a11
* a20
);
2582 out
[7] = (a01
* a20
- a00
* a21
);
2583 out
[8] = (a00
* a11
- a01
* a10
);
2588 * Calculates the determinant of a mat3
2590 * @param {mat3} a the source matrix
2591 * @returns {Number} determinant of a
2593 mat3
.determinant = function (a
) {
2594 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2595 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2596 a20
= a
[6], a21
= a
[7], a22
= a
[8];
2598 return a00
* (a22
* a11
- a12
* a21
) + a01
* (-a22
* a10
+ a12
* a20
) + a02
* (a21
* a10
- a11
* a20
);
2602 * Multiplies two mat3's
2604 * @param {mat3} out the receiving matrix
2605 * @param {mat3} a the first operand
2606 * @param {mat3} b the second operand
2607 * @returns {mat3} out
2609 mat3
.multiply = function (out
, a
, b
) {
2610 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2611 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2612 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2614 b00
= b
[0], b01
= b
[1], b02
= b
[2],
2615 b10
= b
[3], b11
= b
[4], b12
= b
[5],
2616 b20
= b
[6], b21
= b
[7], b22
= b
[8];
2618 out
[0] = b00
* a00
+ b01
* a10
+ b02
* a20
;
2619 out
[1] = b00
* a01
+ b01
* a11
+ b02
* a21
;
2620 out
[2] = b00
* a02
+ b01
* a12
+ b02
* a22
;
2622 out
[3] = b10
* a00
+ b11
* a10
+ b12
* a20
;
2623 out
[4] = b10
* a01
+ b11
* a11
+ b12
* a21
;
2624 out
[5] = b10
* a02
+ b11
* a12
+ b12
* a22
;
2626 out
[6] = b20
* a00
+ b21
* a10
+ b22
* a20
;
2627 out
[7] = b20
* a01
+ b21
* a11
+ b22
* a21
;
2628 out
[8] = b20
* a02
+ b21
* a12
+ b22
* a22
;
2633 * Alias for {@link mat3.multiply}
2636 mat3
.mul
= mat3
.multiply
;
2639 * Translate a mat3 by the given vector
2641 * @param {mat3} out the receiving matrix
2642 * @param {mat3} a the matrix to translate
2643 * @param {vec2} v vector to translate by
2644 * @returns {mat3} out
2646 mat3
.translate = function(out
, a
, v
) {
2647 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2648 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2649 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2660 out
[6] = x
* a00
+ y
* a10
+ a20
;
2661 out
[7] = x
* a01
+ y
* a11
+ a21
;
2662 out
[8] = x
* a02
+ y
* a12
+ a22
;
2667 * Rotates a mat3 by the given angle
2669 * @param {mat3} out the receiving matrix
2670 * @param {mat3} a the matrix to rotate
2671 * @param {Number} rad the angle to rotate the matrix by
2672 * @returns {mat3} out
2674 mat3
.rotate = function (out
, a
, rad
) {
2675 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2676 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2677 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2682 out
[0] = c
* a00
+ s
* a10
;
2683 out
[1] = c
* a01
+ s
* a11
;
2684 out
[2] = c
* a02
+ s
* a12
;
2686 out
[3] = c
* a10
- s
* a00
;
2687 out
[4] = c
* a11
- s
* a01
;
2688 out
[5] = c
* a12
- s
* a02
;
2697 * Scales the mat3 by the dimensions in the given vec2
2699 * @param {mat3} out the receiving matrix
2700 * @param {mat3} a the matrix to rotate
2701 * @param {vec2} v the vec2 to scale the matrix by
2702 * @returns {mat3} out
2704 mat3
.scale = function(out
, a
, v
) {
2705 var x
= v
[0], y
= v
[1];
2722 * Copies the values from a mat2d into a mat3
2724 * @param {mat3} out the receiving matrix
2725 * @param {mat2d} a the matrix to copy
2726 * @returns {mat3} out
2728 mat3
.fromMat2d = function(out
, a
) {
2744 * Calculates a 3x3 matrix from the given quaternion
2746 * @param {mat3} out mat3 receiving operation result
2747 * @param {quat} q Quaternion to create matrix from
2749 * @returns {mat3} out
2751 mat3
.fromQuat = function (out
, q
) {
2752 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
2767 out
[0] = 1 - yy
- zz
;
2772 out
[4] = 1 - xx
- zz
;
2777 out
[8] = 1 - xx
- yy
;
2783 * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
2785 * @param {mat3} out mat3 receiving operation result
2786 * @param {mat4} a Mat4 to derive the normal matrix from
2788 * @returns {mat3} out
2790 mat3
.normalFromMat4 = function (out
, a
) {
2791 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
2792 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
2793 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
2794 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
2796 b00
= a00
* a11
- a01
* a10
,
2797 b01
= a00
* a12
- a02
* a10
,
2798 b02
= a00
* a13
- a03
* a10
,
2799 b03
= a01
* a12
- a02
* a11
,
2800 b04
= a01
* a13
- a03
* a11
,
2801 b05
= a02
* a13
- a03
* a12
,
2802 b06
= a20
* a31
- a21
* a30
,
2803 b07
= a20
* a32
- a22
* a30
,
2804 b08
= a20
* a33
- a23
* a30
,
2805 b09
= a21
* a32
- a22
* a31
,
2806 b10
= a21
* a33
- a23
* a31
,
2807 b11
= a22
* a33
- a23
* a32
,
2809 // Calculate the determinant
2810 det
= b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
2817 out
[0] = (a11
* b11
- a12
* b10
+ a13
* b09
) * det
;
2818 out
[1] = (a12
* b08
- a10
* b11
- a13
* b07
) * det
;
2819 out
[2] = (a10
* b10
- a11
* b08
+ a13
* b06
) * det
;
2821 out
[3] = (a02
* b10
- a01
* b11
- a03
* b09
) * det
;
2822 out
[4] = (a00
* b11
- a02
* b08
+ a03
* b07
) * det
;
2823 out
[5] = (a01
* b08
- a00
* b10
- a03
* b06
) * det
;
2825 out
[6] = (a31
* b05
- a32
* b04
+ a33
* b03
) * det
;
2826 out
[7] = (a32
* b02
- a30
* b05
- a33
* b01
) * det
;
2827 out
[8] = (a30
* b04
- a31
* b02
+ a33
* b00
) * det
;
2833 * Returns a string representation of a mat3
2835 * @param {mat3} mat matrix to represent as a string
2836 * @returns {String} string representation of the matrix
2838 mat3
.str = function (a
) {
2839 return 'mat3(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' +
2840 a
[3] + ', ' + a
[4] + ', ' + a
[5] + ', ' +
2841 a
[6] + ', ' + a
[7] + ', ' + a
[8] + ')';
2845 * Returns Frobenius norm of a mat3
2847 * @param {mat3} a the matrix to calculate Frobenius norm of
2848 * @returns {Number} Frobenius norm
2850 mat3
.frob = function (a
) {
2851 return(Math
.sqrt(Math
.pow(a
[0], 2) + Math
.pow(a
[1], 2) + Math
.pow(a
[2], 2) + Math
.pow(a
[3], 2) + Math
.pow(a
[4], 2) + Math
.pow(a
[5], 2) + Math
.pow(a
[6], 2) + Math
.pow(a
[7], 2) + Math
.pow(a
[8], 2)))
2855 if(typeof(exports
) !== 'undefined') {
2856 exports
.mat3
= mat3
;
2859 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2861 Redistribution and use in source and binary forms, with or without modification,
2862 are permitted provided that the following conditions are met:
2864 * Redistributions of source code must retain the above copyright notice, this
2865 list of conditions and the following disclaimer.
2866 * Redistributions in binary form must reproduce the above copyright notice,
2867 this list of conditions and the following disclaimer in the documentation
2868 and/or other materials provided with the distribution.
2870 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2871 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2872 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2873 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2874 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2875 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2876 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2877 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2878 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2879 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2889 * Creates a new identity mat4
2891 * @returns {mat4} a new 4x4 matrix
2893 mat4
.create = function() {
2894 var out
= new GLMAT_ARRAY_TYPE(16);
2915 * Creates a new mat4 initialized with values from an existing matrix
2917 * @param {mat4} a matrix to clone
2918 * @returns {mat4} a new 4x4 matrix
2920 mat4
.clone = function(a
) {
2921 var out
= new GLMAT_ARRAY_TYPE(16);
2942 * Copy the values from one mat4 to another
2944 * @param {mat4} out the receiving matrix
2945 * @param {mat4} a the source matrix
2946 * @returns {mat4} out
2948 mat4
.copy = function(out
, a
) {
2969 * Set a mat4 to the identity matrix
2971 * @param {mat4} out the receiving matrix
2972 * @returns {mat4} out
2974 mat4
.identity = function(out
) {
2995 * Transpose the values of a mat4
2997 * @param {mat4} out the receiving matrix
2998 * @param {mat4} a the source matrix
2999 * @returns {mat4} out
3001 mat4
.transpose = function(out
, a
) {
3002 // If we are transposing ourselves we can skip a few steps but have to cache some values
3004 var a01
= a
[1], a02
= a
[2], a03
= a
[3],
3005 a12
= a
[6], a13
= a
[7],
3045 * @param {mat4} out the receiving matrix
3046 * @param {mat4} a the source matrix
3047 * @returns {mat4} out
3049 mat4
.invert = function(out
, a
) {
3050 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3051 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3052 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3053 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
3055 b00
= a00
* a11
- a01
* a10
,
3056 b01
= a00
* a12
- a02
* a10
,
3057 b02
= a00
* a13
- a03
* a10
,
3058 b03
= a01
* a12
- a02
* a11
,
3059 b04
= a01
* a13
- a03
* a11
,
3060 b05
= a02
* a13
- a03
* a12
,
3061 b06
= a20
* a31
- a21
* a30
,
3062 b07
= a20
* a32
- a22
* a30
,
3063 b08
= a20
* a33
- a23
* a30
,
3064 b09
= a21
* a32
- a22
* a31
,
3065 b10
= a21
* a33
- a23
* a31
,
3066 b11
= a22
* a33
- a23
* a32
,
3068 // Calculate the determinant
3069 det
= b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
3076 out
[0] = (a11
* b11
- a12
* b10
+ a13
* b09
) * det
;
3077 out
[1] = (a02
* b10
- a01
* b11
- a03
* b09
) * det
;
3078 out
[2] = (a31
* b05
- a32
* b04
+ a33
* b03
) * det
;
3079 out
[3] = (a22
* b04
- a21
* b05
- a23
* b03
) * det
;
3080 out
[4] = (a12
* b08
- a10
* b11
- a13
* b07
) * det
;
3081 out
[5] = (a00
* b11
- a02
* b08
+ a03
* b07
) * det
;
3082 out
[6] = (a32
* b02
- a30
* b05
- a33
* b01
) * det
;
3083 out
[7] = (a20
* b05
- a22
* b02
+ a23
* b01
) * det
;
3084 out
[8] = (a10
* b10
- a11
* b08
+ a13
* b06
) * det
;
3085 out
[9] = (a01
* b08
- a00
* b10
- a03
* b06
) * det
;
3086 out
[10] = (a30
* b04
- a31
* b02
+ a33
* b00
) * det
;
3087 out
[11] = (a21
* b02
- a20
* b04
- a23
* b00
) * det
;
3088 out
[12] = (a11
* b07
- a10
* b09
- a12
* b06
) * det
;
3089 out
[13] = (a00
* b09
- a01
* b07
+ a02
* b06
) * det
;
3090 out
[14] = (a31
* b01
- a30
* b03
- a32
* b00
) * det
;
3091 out
[15] = (a20
* b03
- a21
* b01
+ a22
* b00
) * det
;
3097 * Calculates the adjugate of a mat4
3099 * @param {mat4} out the receiving matrix
3100 * @param {mat4} a the source matrix
3101 * @returns {mat4} out
3103 mat4
.adjoint = function(out
, a
) {
3104 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3105 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3106 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3107 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15];
3109 out
[0] = (a11
* (a22
* a33
- a23
* a32
) - a21
* (a12
* a33
- a13
* a32
) + a31
* (a12
* a23
- a13
* a22
));
3110 out
[1] = -(a01
* (a22
* a33
- a23
* a32
) - a21
* (a02
* a33
- a03
* a32
) + a31
* (a02
* a23
- a03
* a22
));
3111 out
[2] = (a01
* (a12
* a33
- a13
* a32
) - a11
* (a02
* a33
- a03
* a32
) + a31
* (a02
* a13
- a03
* a12
));
3112 out
[3] = -(a01
* (a12
* a23
- a13
* a22
) - a11
* (a02
* a23
- a03
* a22
) + a21
* (a02
* a13
- a03
* a12
));
3113 out
[4] = -(a10
* (a22
* a33
- a23
* a32
) - a20
* (a12
* a33
- a13
* a32
) + a30
* (a12
* a23
- a13
* a22
));
3114 out
[5] = (a00
* (a22
* a33
- a23
* a32
) - a20
* (a02
* a33
- a03
* a32
) + a30
* (a02
* a23
- a03
* a22
));
3115 out
[6] = -(a00
* (a12
* a33
- a13
* a32
) - a10
* (a02
* a33
- a03
* a32
) + a30
* (a02
* a13
- a03
* a12
));
3116 out
[7] = (a00
* (a12
* a23
- a13
* a22
) - a10
* (a02
* a23
- a03
* a22
) + a20
* (a02
* a13
- a03
* a12
));
3117 out
[8] = (a10
* (a21
* a33
- a23
* a31
) - a20
* (a11
* a33
- a13
* a31
) + a30
* (a11
* a23
- a13
* a21
));
3118 out
[9] = -(a00
* (a21
* a33
- a23
* a31
) - a20
* (a01
* a33
- a03
* a31
) + a30
* (a01
* a23
- a03
* a21
));
3119 out
[10] = (a00
* (a11
* a33
- a13
* a31
) - a10
* (a01
* a33
- a03
* a31
) + a30
* (a01
* a13
- a03
* a11
));
3120 out
[11] = -(a00
* (a11
* a23
- a13
* a21
) - a10
* (a01
* a23
- a03
* a21
) + a20
* (a01
* a13
- a03
* a11
));
3121 out
[12] = -(a10
* (a21
* a32
- a22
* a31
) - a20
* (a11
* a32
- a12
* a31
) + a30
* (a11
* a22
- a12
* a21
));
3122 out
[13] = (a00
* (a21
* a32
- a22
* a31
) - a20
* (a01
* a32
- a02
* a31
) + a30
* (a01
* a22
- a02
* a21
));
3123 out
[14] = -(a00
* (a11
* a32
- a12
* a31
) - a10
* (a01
* a32
- a02
* a31
) + a30
* (a01
* a12
- a02
* a11
));
3124 out
[15] = (a00
* (a11
* a22
- a12
* a21
) - a10
* (a01
* a22
- a02
* a21
) + a20
* (a01
* a12
- a02
* a11
));
3129 * Calculates the determinant of a mat4
3131 * @param {mat4} a the source matrix
3132 * @returns {Number} determinant of a
3134 mat4
.determinant = function (a
) {
3135 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3136 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3137 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3138 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
3140 b00
= a00
* a11
- a01
* a10
,
3141 b01
= a00
* a12
- a02
* a10
,
3142 b02
= a00
* a13
- a03
* a10
,
3143 b03
= a01
* a12
- a02
* a11
,
3144 b04
= a01
* a13
- a03
* a11
,
3145 b05
= a02
* a13
- a03
* a12
,
3146 b06
= a20
* a31
- a21
* a30
,
3147 b07
= a20
* a32
- a22
* a30
,
3148 b08
= a20
* a33
- a23
* a30
,
3149 b09
= a21
* a32
- a22
* a31
,
3150 b10
= a21
* a33
- a23
* a31
,
3151 b11
= a22
* a33
- a23
* a32
;
3153 // Calculate the determinant
3154 return b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
3158 * Multiplies two mat4's
3160 * @param {mat4} out the receiving matrix
3161 * @param {mat4} a the first operand
3162 * @param {mat4} b the second operand
3163 * @returns {mat4} out
3165 mat4
.multiply = function (out
, a
, b
) {
3166 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3167 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3168 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3169 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15];
3171 // Cache only the current line of the second matrix
3172 var b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3];
3173 out
[0] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3174 out
[1] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3175 out
[2] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3176 out
[3] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3178 b0
= b
[4]; b1
= b
[5]; b2
= b
[6]; b3
= b
[7];
3179 out
[4] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3180 out
[5] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3181 out
[6] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3182 out
[7] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3184 b0
= b
[8]; b1
= b
[9]; b2
= b
[10]; b3
= b
[11];
3185 out
[8] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3186 out
[9] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3187 out
[10] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3188 out
[11] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3190 b0
= b
[12]; b1
= b
[13]; b2
= b
[14]; b3
= b
[15];
3191 out
[12] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3192 out
[13] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3193 out
[14] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3194 out
[15] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3199 * Alias for {@link mat4.multiply}
3202 mat4
.mul
= mat4
.multiply
;
3205 * Translate a mat4 by the given vector
3207 * @param {mat4} out the receiving matrix
3208 * @param {mat4} a the matrix to translate
3209 * @param {vec3} v vector to translate by
3210 * @returns {mat4} out
3212 mat4
.translate = function (out
, a
, v
) {
3213 var x
= v
[0], y
= v
[1], z
= v
[2],
3219 out
[12] = a
[0] * x
+ a
[4] * y
+ a
[8] * z
+ a
[12];
3220 out
[13] = a
[1] * x
+ a
[5] * y
+ a
[9] * z
+ a
[13];
3221 out
[14] = a
[2] * x
+ a
[6] * y
+ a
[10] * z
+ a
[14];
3222 out
[15] = a
[3] * x
+ a
[7] * y
+ a
[11] * z
+ a
[15];
3224 a00
= a
[0]; a01
= a
[1]; a02
= a
[2]; a03
= a
[3];
3225 a10
= a
[4]; a11
= a
[5]; a12
= a
[6]; a13
= a
[7];
3226 a20
= a
[8]; a21
= a
[9]; a22
= a
[10]; a23
= a
[11];
3228 out
[0] = a00
; out
[1] = a01
; out
[2] = a02
; out
[3] = a03
;
3229 out
[4] = a10
; out
[5] = a11
; out
[6] = a12
; out
[7] = a13
;
3230 out
[8] = a20
; out
[9] = a21
; out
[10] = a22
; out
[11] = a23
;
3232 out
[12] = a00
* x
+ a10
* y
+ a20
* z
+ a
[12];
3233 out
[13] = a01
* x
+ a11
* y
+ a21
* z
+ a
[13];
3234 out
[14] = a02
* x
+ a12
* y
+ a22
* z
+ a
[14];
3235 out
[15] = a03
* x
+ a13
* y
+ a23
* z
+ a
[15];
3242 * Scales the mat4 by the dimensions in the given vec3
3244 * @param {mat4} out the receiving matrix
3245 * @param {mat4} a the matrix to scale
3246 * @param {vec3} v the vec3 to scale the matrix by
3247 * @returns {mat4} out
3249 mat4
.scale = function(out
, a
, v
) {
3250 var x
= v
[0], y
= v
[1], z
= v
[2];
3262 out
[10] = a
[10] * z
;
3263 out
[11] = a
[11] * z
;
3272 * Rotates a mat4 by the given angle
3274 * @param {mat4} out the receiving matrix
3275 * @param {mat4} a the matrix to rotate
3276 * @param {Number} rad the angle to rotate the matrix by
3277 * @param {vec3} axis the axis to rotate around
3278 * @returns {mat4} out
3280 mat4
.rotate = function (out
, a
, rad
, axis
) {
3281 var x
= axis
[0], y
= axis
[1], z
= axis
[2],
3282 len
= Math
.sqrt(x
* x
+ y
* y
+ z
* z
),
3291 if (Math
.abs(len
) < GLMAT_EPSILON
) { return null; }
3302 a00
= a
[0]; a01
= a
[1]; a02
= a
[2]; a03
= a
[3];
3303 a10
= a
[4]; a11
= a
[5]; a12
= a
[6]; a13
= a
[7];
3304 a20
= a
[8]; a21
= a
[9]; a22
= a
[10]; a23
= a
[11];
3306 // Construct the elements of the rotation matrix
3307 b00
= x
* x
* t
+ c
; b01
= y
* x
* t
+ z
* s
; b02
= z
* x
* t
- y
* s
;
3308 b10
= x
* y
* t
- z
* s
; b11
= y
* y
* t
+ c
; b12
= z
* y
* t
+ x
* s
;
3309 b20
= x
* z
* t
+ y
* s
; b21
= y
* z
* t
- x
* s
; b22
= z
* z
* t
+ c
;
3311 // Perform rotation-specific matrix multiplication
3312 out
[0] = a00
* b00
+ a10
* b01
+ a20
* b02
;
3313 out
[1] = a01
* b00
+ a11
* b01
+ a21
* b02
;
3314 out
[2] = a02
* b00
+ a12
* b01
+ a22
* b02
;
3315 out
[3] = a03
* b00
+ a13
* b01
+ a23
* b02
;
3316 out
[4] = a00
* b10
+ a10
* b11
+ a20
* b12
;
3317 out
[5] = a01
* b10
+ a11
* b11
+ a21
* b12
;
3318 out
[6] = a02
* b10
+ a12
* b11
+ a22
* b12
;
3319 out
[7] = a03
* b10
+ a13
* b11
+ a23
* b12
;
3320 out
[8] = a00
* b20
+ a10
* b21
+ a20
* b22
;
3321 out
[9] = a01
* b20
+ a11
* b21
+ a21
* b22
;
3322 out
[10] = a02
* b20
+ a12
* b21
+ a22
* b22
;
3323 out
[11] = a03
* b20
+ a13
* b21
+ a23
* b22
;
3325 if (a
!== out
) { // If the source and destination differ, copy the unchanged last row
3335 * Rotates a matrix by the given angle around the X axis
3337 * @param {mat4} out the receiving matrix
3338 * @param {mat4} a the matrix to rotate
3339 * @param {Number} rad the angle to rotate the matrix by
3340 * @returns {mat4} out
3342 mat4
.rotateX = function (out
, a
, rad
) {
3343 var s
= Math
.sin(rad
),
3354 if (a
!== out
) { // If the source and destination differ, copy the unchanged rows
3365 // Perform axis-specific matrix multiplication
3366 out
[4] = a10
* c
+ a20
* s
;
3367 out
[5] = a11
* c
+ a21
* s
;
3368 out
[6] = a12
* c
+ a22
* s
;
3369 out
[7] = a13
* c
+ a23
* s
;
3370 out
[8] = a20
* c
- a10
* s
;
3371 out
[9] = a21
* c
- a11
* s
;
3372 out
[10] = a22
* c
- a12
* s
;
3373 out
[11] = a23
* c
- a13
* s
;
3378 * Rotates a matrix by the given angle around the Y axis
3380 * @param {mat4} out the receiving matrix
3381 * @param {mat4} a the matrix to rotate
3382 * @param {Number} rad the angle to rotate the matrix by
3383 * @returns {mat4} out
3385 mat4
.rotateY = function (out
, a
, rad
) {
3386 var s
= Math
.sin(rad
),
3397 if (a
!== out
) { // If the source and destination differ, copy the unchanged rows
3408 // Perform axis-specific matrix multiplication
3409 out
[0] = a00
* c
- a20
* s
;
3410 out
[1] = a01
* c
- a21
* s
;
3411 out
[2] = a02
* c
- a22
* s
;
3412 out
[3] = a03
* c
- a23
* s
;
3413 out
[8] = a00
* s
+ a20
* c
;
3414 out
[9] = a01
* s
+ a21
* c
;
3415 out
[10] = a02
* s
+ a22
* c
;
3416 out
[11] = a03
* s
+ a23
* c
;
3421 * Rotates a matrix by the given angle around the Z axis
3423 * @param {mat4} out the receiving matrix
3424 * @param {mat4} a the matrix to rotate
3425 * @param {Number} rad the angle to rotate the matrix by
3426 * @returns {mat4} out
3428 mat4
.rotateZ = function (out
, a
, rad
) {
3429 var s
= Math
.sin(rad
),
3440 if (a
!== out
) { // If the source and destination differ, copy the unchanged last row
3451 // Perform axis-specific matrix multiplication
3452 out
[0] = a00
* c
+ a10
* s
;
3453 out
[1] = a01
* c
+ a11
* s
;
3454 out
[2] = a02
* c
+ a12
* s
;
3455 out
[3] = a03
* c
+ a13
* s
;
3456 out
[4] = a10
* c
- a00
* s
;
3457 out
[5] = a11
* c
- a01
* s
;
3458 out
[6] = a12
* c
- a02
* s
;
3459 out
[7] = a13
* c
- a03
* s
;
3464 * Creates a matrix from a quaternion rotation and vector translation
3465 * This is equivalent to (but much faster than):
3467 * mat4.identity(dest);
3468 * mat4.translate(dest, vec);
3469 * var quatMat = mat4.create();
3470 * quat4.toMat4(quat, quatMat);
3471 * mat4.multiply(dest, quatMat);
3473 * @param {mat4} out mat4 receiving operation result
3474 * @param {quat4} q Rotation quaternion
3475 * @param {vec3} v Translation vector
3476 * @returns {mat4} out
3478 mat4
.fromRotationTranslation = function (out
, q
, v
) {
3480 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
3495 out
[0] = 1 - (yy
+ zz
);
3500 out
[5] = 1 - (xx
+ zz
);
3505 out
[10] = 1 - (xx
+ yy
);
3515 mat4
.fromQuat = function (out
, q
) {
3516 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
3531 out
[0] = 1 - yy
- zz
;
3537 out
[5] = 1 - xx
- zz
;
3543 out
[10] = 1 - xx
- yy
;
3555 * Generates a frustum matrix with the given bounds
3557 * @param {mat4} out mat4 frustum matrix will be written into
3558 * @param {Number} left Left bound of the frustum
3559 * @param {Number} right Right bound of the frustum
3560 * @param {Number} bottom Bottom bound of the frustum
3561 * @param {Number} top Top bound of the frustum
3562 * @param {Number} near Near bound of the frustum
3563 * @param {Number} far Far bound of the frustum
3564 * @returns {mat4} out
3566 mat4
.frustum = function (out
, left
, right
, bottom
, top
, near
, far
) {
3567 var rl
= 1 / (right
- left
),
3568 tb
= 1 / (top
- bottom
),
3569 nf
= 1 / (near
- far
);
3570 out
[0] = (near
* 2) * rl
;
3575 out
[5] = (near
* 2) * tb
;
3578 out
[8] = (right
+ left
) * rl
;
3579 out
[9] = (top
+ bottom
) * tb
;
3580 out
[10] = (far
+ near
) * nf
;
3584 out
[14] = (far
* near
* 2) * nf
;
3590 * Generates a perspective projection matrix with the given bounds
3592 * @param {mat4} out mat4 frustum matrix will be written into
3593 * @param {number} fovy Vertical field of view in radians
3594 * @param {number} aspect Aspect ratio. typically viewport width/height
3595 * @param {number} near Near bound of the frustum
3596 * @param {number} far Far bound of the frustum
3597 * @returns {mat4} out
3599 mat4
.perspective = function (out
, fovy
, aspect
, near
, far
) {
3600 var f
= 1.0 / Math
.tan(fovy
/ 2),
3601 nf
= 1 / (near
- far
);
3602 out
[0] = f
/ aspect
;
3612 out
[10] = (far
+ near
) * nf
;
3616 out
[14] = (2 * far
* near
) * nf
;
3622 * Generates a orthogonal projection matrix with the given bounds
3624 * @param {mat4} out mat4 frustum matrix will be written into
3625 * @param {number} left Left bound of the frustum
3626 * @param {number} right Right bound of the frustum
3627 * @param {number} bottom Bottom bound of the frustum
3628 * @param {number} top Top bound of the frustum
3629 * @param {number} near Near bound of the frustum
3630 * @param {number} far Far bound of the frustum
3631 * @returns {mat4} out
3633 mat4
.ortho = function (out
, left
, right
, bottom
, top
, near
, far
) {
3634 var lr
= 1 / (left
- right
),
3635 bt
= 1 / (bottom
- top
),
3636 nf
= 1 / (near
- far
);
3649 out
[12] = (left
+ right
) * lr
;
3650 out
[13] = (top
+ bottom
) * bt
;
3651 out
[14] = (far
+ near
) * nf
;
3657 * Generates a look-at matrix with the given eye position, focal point, and up axis
3659 * @param {mat4} out mat4 frustum matrix will be written into
3660 * @param {vec3} eye Position of the viewer
3661 * @param {vec3} center Point the viewer is looking at
3662 * @param {vec3} up vec3 pointing up
3663 * @returns {mat4} out
3665 mat4
.lookAt = function (out
, eye
, center
, up
) {
3666 var x0
, x1
, x2
, y0
, y1
, y2
, z0
, z1
, z2
, len
,
3673 centerx
= center
[0],
3674 centery
= center
[1],
3675 centerz
= center
[2];
3677 if (Math
.abs(eyex
- centerx
) < GLMAT_EPSILON
&&
3678 Math
.abs(eyey
- centery
) < GLMAT_EPSILON
&&
3679 Math
.abs(eyez
- centerz
) < GLMAT_EPSILON
) {
3680 return mat4
.identity(out
);
3683 z0
= eyex
- centerx
;
3684 z1
= eyey
- centery
;
3685 z2
= eyez
- centerz
;
3687 len
= 1 / Math
.sqrt(z0
* z0
+ z1
* z1
+ z2
* z2
);
3692 x0
= upy
* z2
- upz
* z1
;
3693 x1
= upz
* z0
- upx
* z2
;
3694 x2
= upx
* z1
- upy
* z0
;
3695 len
= Math
.sqrt(x0
* x0
+ x1
* x1
+ x2
* x2
);
3707 y0
= z1
* x2
- z2
* x1
;
3708 y1
= z2
* x0
- z0
* x2
;
3709 y2
= z0
* x1
- z1
* x0
;
3711 len
= Math
.sqrt(y0
* y0
+ y1
* y1
+ y2
* y2
);
3735 out
[12] = -(x0
* eyex
+ x1
* eyey
+ x2
* eyez
);
3736 out
[13] = -(y0
* eyex
+ y1
* eyey
+ y2
* eyez
);
3737 out
[14] = -(z0
* eyex
+ z1
* eyey
+ z2
* eyez
);
3744 * Returns a string representation of a mat4
3746 * @param {mat4} mat matrix to represent as a string
3747 * @returns {String} string representation of the matrix
3749 mat4
.str = function (a
) {
3750 return 'mat4(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ', ' +
3751 a
[4] + ', ' + a
[5] + ', ' + a
[6] + ', ' + a
[7] + ', ' +
3752 a
[8] + ', ' + a
[9] + ', ' + a
[10] + ', ' + a
[11] + ', ' +
3753 a
[12] + ', ' + a
[13] + ', ' + a
[14] + ', ' + a
[15] + ')';
3757 * Returns Frobenius norm of a mat4
3759 * @param {mat4} a the matrix to calculate Frobenius norm of
3760 * @returns {Number} Frobenius norm
3762 mat4
.frob = function (a
) {
3763 return(Math
.sqrt(Math
.pow(a
[0], 2) + Math
.pow(a
[1], 2) + Math
.pow(a
[2], 2) + Math
.pow(a
[3], 2) + Math
.pow(a
[4], 2) + Math
.pow(a
[5], 2) + Math
.pow(a
[6], 2) + Math
.pow(a
[7], 2) + Math
.pow(a
[8], 2) + Math
.pow(a
[9], 2) + Math
.pow(a
[10], 2) + Math
.pow(a
[11], 2) + Math
.pow(a
[12], 2) + Math
.pow(a
[13], 2) + Math
.pow(a
[14], 2) + Math
.pow(a
[15], 2) ))
3767 if(typeof(exports
) !== 'undefined') {
3768 exports
.mat4
= mat4
;
3771 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3773 Redistribution and use in source and binary forms, with or without modification,
3774 are permitted provided that the following conditions are met:
3776 * Redistributions of source code must retain the above copyright notice, this
3777 list of conditions and the following disclaimer.
3778 * Redistributions in binary form must reproduce the above copyright notice,
3779 this list of conditions and the following disclaimer in the documentation
3780 and/or other materials provided with the distribution.
3782 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
3783 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3784 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3785 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
3786 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3787 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3788 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
3789 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3790 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3791 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
3801 * Creates a new identity quat
3803 * @returns {quat} a new quaternion
3805 quat
.create = function() {
3806 var out
= new GLMAT_ARRAY_TYPE(4);
3815 * Sets a quaternion to represent the shortest rotation from one
3816 * vector to another.
3818 * Both vectors are assumed to be unit length.
3820 * @param {quat} out the receiving quaternion.
3821 * @param {vec3} a the initial vector
3822 * @param {vec3} b the destination vector
3823 * @returns {quat} out
3825 quat
.rotationTo
= (function() {
3826 var tmpvec3
= vec3
.create();
3827 var xUnitVec3
= vec3
.fromValues(1,0,0);
3828 var yUnitVec3
= vec3
.fromValues(0,1,0);
3830 return function(out
, a
, b
) {
3831 var dot
= vec3
.dot(a
, b
);
3832 if (dot
< -0.999999) {
3833 vec3
.cross(tmpvec3
, xUnitVec3
, a
);
3834 if (vec3
.length(tmpvec3
) < 0.000001)
3835 vec3
.cross(tmpvec3
, yUnitVec3
, a
);
3836 vec3
.normalize(tmpvec3
, tmpvec3
);
3837 quat
.setAxisAngle(out
, tmpvec3
, Math
.PI
);
3839 } else if (dot
> 0.999999) {
3846 vec3
.cross(tmpvec3
, a
, b
);
3847 out
[0] = tmpvec3
[0];
3848 out
[1] = tmpvec3
[1];
3849 out
[2] = tmpvec3
[2];
3851 return quat
.normalize(out
, out
);
3857 * Sets the specified quaternion with values corresponding to the given
3858 * axes. Each axis is a vec3 and is expected to be unit length and
3859 * perpendicular to all other specified axes.
3861 * @param {vec3} view the vector representing the viewing direction
3862 * @param {vec3} right the vector representing the local "right" direction
3863 * @param {vec3} up the vector representing the local "up" direction
3864 * @returns {quat} out
3866 quat
.setAxes
= (function() {
3867 var matr
= mat3
.create();
3869 return function(out
, view
, right
, up
) {
3882 return quat
.normalize(out
, quat
.fromMat3(out
, matr
));
3887 * Creates a new quat initialized with values from an existing quaternion
3889 * @param {quat} a quaternion to clone
3890 * @returns {quat} a new quaternion
3893 quat
.clone
= vec4
.clone
;
3896 * Creates a new quat initialized with the given values
3898 * @param {Number} x X component
3899 * @param {Number} y Y component
3900 * @param {Number} z Z component
3901 * @param {Number} w W component
3902 * @returns {quat} a new quaternion
3905 quat
.fromValues
= vec4
.fromValues
;
3908 * Copy the values from one quat to another
3910 * @param {quat} out the receiving quaternion
3911 * @param {quat} a the source quaternion
3912 * @returns {quat} out
3915 quat
.copy
= vec4
.copy
;
3918 * Set the components of a quat to the given values
3920 * @param {quat} out the receiving quaternion
3921 * @param {Number} x X component
3922 * @param {Number} y Y component
3923 * @param {Number} z Z component
3924 * @param {Number} w W component
3925 * @returns {quat} out
3928 quat
.set = vec4
.set;
3931 * Set a quat to the identity quaternion
3933 * @param {quat} out the receiving quaternion
3934 * @returns {quat} out
3936 quat
.identity = function(out
) {
3945 * Sets a quat from the given angle and rotation axis,
3948 * @param {quat} out the receiving quaternion
3949 * @param {vec3} axis the axis around which to rotate
3950 * @param {Number} rad the angle in radians
3951 * @returns {quat} out
3953 quat
.setAxisAngle = function(out
, axis
, rad
) {
3955 var s
= Math
.sin(rad
);
3956 out
[0] = s
* axis
[0];
3957 out
[1] = s
* axis
[1];
3958 out
[2] = s
* axis
[2];
3959 out
[3] = Math
.cos(rad
);
3966 * @param {quat} out the receiving quaternion
3967 * @param {quat} a the first operand
3968 * @param {quat} b the second operand
3969 * @returns {quat} out
3972 quat
.add
= vec4
.add
;
3975 * Multiplies two quat's
3977 * @param {quat} out the receiving quaternion
3978 * @param {quat} a the first operand
3979 * @param {quat} b the second operand
3980 * @returns {quat} out
3982 quat
.multiply = function(out
, a
, b
) {
3983 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
3984 bx
= b
[0], by
= b
[1], bz
= b
[2], bw
= b
[3];
3986 out
[0] = ax
* bw
+ aw
* bx
+ ay
* bz
- az
* by
;
3987 out
[1] = ay
* bw
+ aw
* by
+ az
* bx
- ax
* bz
;
3988 out
[2] = az
* bw
+ aw
* bz
+ ax
* by
- ay
* bx
;
3989 out
[3] = aw
* bw
- ax
* bx
- ay
* by
- az
* bz
;
3994 * Alias for {@link quat.multiply}
3997 quat
.mul
= quat
.multiply
;
4000 * Scales a quat by a scalar number
4002 * @param {quat} out the receiving vector
4003 * @param {quat} a the vector to scale
4004 * @param {Number} b amount to scale the vector by
4005 * @returns {quat} out
4008 quat
.scale
= vec4
.scale
;
4011 * Rotates a quaternion by the given angle about the X axis
4013 * @param {quat} out quat receiving operation result
4014 * @param {quat} a quat to rotate
4015 * @param {number} rad angle (in radians) to rotate
4016 * @returns {quat} out
4018 quat
.rotateX = function (out
, a
, rad
) {
4021 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
4022 bx
= Math
.sin(rad
), bw
= Math
.cos(rad
);
4024 out
[0] = ax
* bw
+ aw
* bx
;
4025 out
[1] = ay
* bw
+ az
* bx
;
4026 out
[2] = az
* bw
- ay
* bx
;
4027 out
[3] = aw
* bw
- ax
* bx
;
4032 * Rotates a quaternion by the given angle about the Y axis
4034 * @param {quat} out quat receiving operation result
4035 * @param {quat} a quat to rotate
4036 * @param {number} rad angle (in radians) to rotate
4037 * @returns {quat} out
4039 quat
.rotateY = function (out
, a
, rad
) {
4042 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
4043 by
= Math
.sin(rad
), bw
= Math
.cos(rad
);
4045 out
[0] = ax
* bw
- az
* by
;
4046 out
[1] = ay
* bw
+ aw
* by
;
4047 out
[2] = az
* bw
+ ax
* by
;
4048 out
[3] = aw
* bw
- ay
* by
;
4053 * Rotates a quaternion by the given angle about the Z axis
4055 * @param {quat} out quat receiving operation result
4056 * @param {quat} a quat to rotate
4057 * @param {number} rad angle (in radians) to rotate
4058 * @returns {quat} out
4060 quat
.rotateZ = function (out
, a
, rad
) {
4063 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
4064 bz
= Math
.sin(rad
), bw
= Math
.cos(rad
);
4066 out
[0] = ax
* bw
+ ay
* bz
;
4067 out
[1] = ay
* bw
- ax
* bz
;
4068 out
[2] = az
* bw
+ aw
* bz
;
4069 out
[3] = aw
* bw
- az
* bz
;
4074 * Calculates the W component of a quat from the X, Y, and Z components.
4075 * Assumes that quaternion is 1 unit in length.
4076 * Any existing W component will be ignored.
4078 * @param {quat} out the receiving quaternion
4079 * @param {quat} a quat to calculate W component of
4080 * @returns {quat} out
4082 quat
.calculateW = function (out
, a
) {
4083 var x
= a
[0], y
= a
[1], z
= a
[2];
4088 out
[3] = Math
.sqrt(Math
.abs(1.0 - x
* x
- y
* y
- z
* z
));
4093 * Calculates the dot product of two quat's
4095 * @param {quat} a the first operand
4096 * @param {quat} b the second operand
4097 * @returns {Number} dot product of a and b
4100 quat
.dot
= vec4
.dot
;
4103 * Performs a linear interpolation between two quat's
4105 * @param {quat} out the receiving quaternion
4106 * @param {quat} a the first operand
4107 * @param {quat} b the second operand
4108 * @param {Number} t interpolation amount between the two inputs
4109 * @returns {quat} out
4112 quat
.lerp
= vec4
.lerp
;
4115 * Performs a spherical linear interpolation between two quat
4117 * @param {quat} out the receiving quaternion
4118 * @param {quat} a the first operand
4119 * @param {quat} b the second operand
4120 * @param {Number} t interpolation amount between the two inputs
4121 * @returns {quat} out
4123 quat
.slerp = function (out
, a
, b
, t
) {
4125 // http://jsperf.com/quaternion-slerp-implementations
4127 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
4128 bx
= b
[0], by
= b
[1], bz
= b
[2], bw
= b
[3];
4130 var omega
, cosom
, sinom
, scale0
, scale1
;
4133 cosom
= ax
* bx
+ ay
* by
+ az
* bz
+ aw
* bw
;
4134 // adjust signs (if necessary)
4135 if ( cosom
< 0.0 ) {
4142 // calculate coefficients
4143 if ( (1.0 - cosom
) > 0.000001 ) {
4144 // standard case (slerp)
4145 omega
= Math
.acos(cosom
);
4146 sinom
= Math
.sin(omega
);
4147 scale0
= Math
.sin((1.0 - t
) * omega
) / sinom
;
4148 scale1
= Math
.sin(t
* omega
) / sinom
;
4150 // "from" and "to" quaternions are very close
4151 // ... so we can do a linear interpolation
4155 // calculate final values
4156 out
[0] = scale0
* ax
+ scale1
* bx
;
4157 out
[1] = scale0
* ay
+ scale1
* by
;
4158 out
[2] = scale0
* az
+ scale1
* bz
;
4159 out
[3] = scale0
* aw
+ scale1
* bw
;
4165 * Calculates the inverse of a quat
4167 * @param {quat} out the receiving quaternion
4168 * @param {quat} a quat to calculate inverse of
4169 * @returns {quat} out
4171 quat
.invert = function(out
, a
) {
4172 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
4173 dot
= a0
*a0
+ a1
*a1
+ a2
*a2
+ a3
*a3
,
4174 invDot
= dot
? 1.0/dot
: 0;
4176 // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
4178 out
[0] = -a0
*invDot
;
4179 out
[1] = -a1
*invDot
;
4180 out
[2] = -a2
*invDot
;
4186 * Calculates the conjugate of a quat
4187 * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
4189 * @param {quat} out the receiving quaternion
4190 * @param {quat} a quat to calculate conjugate of
4191 * @returns {quat} out
4193 quat
.conjugate = function (out
, a
) {
4202 * Calculates the length of a quat
4204 * @param {quat} a vector to calculate length of
4205 * @returns {Number} length of a
4208 quat
.length
= vec4
.length
;
4211 * Alias for {@link quat.length}
4214 quat
.len
= quat
.length
;
4217 * Calculates the squared length of a quat
4219 * @param {quat} a vector to calculate squared length of
4220 * @returns {Number} squared length of a
4223 quat
.squaredLength
= vec4
.squaredLength
;
4226 * Alias for {@link quat.squaredLength}
4229 quat
.sqrLen
= quat
.squaredLength
;
4234 * @param {quat} out the receiving quaternion
4235 * @param {quat} a quaternion to normalize
4236 * @returns {quat} out
4239 quat
.normalize
= vec4
.normalize
;
4242 * Creates a quaternion from the given 3x3 rotation matrix.
4244 * NOTE: The resultant quaternion is not normalized, so you should be sure
4245 * to renormalize the quaternion yourself where necessary.
4247 * @param {quat} out the receiving quaternion
4248 * @param {mat3} m rotation matrix
4249 * @returns {quat} out
4252 quat
.fromMat3 = function(out
, m
) {
4253 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
4254 // article "Quaternion Calculus and Fast Animation".
4255 var fTrace
= m
[0] + m
[4] + m
[8];
4258 if ( fTrace
> 0.0 ) {
4259 // |w| > 1/2, may as well choose w > 1/2
4260 fRoot
= Math
.sqrt(fTrace
+ 1.0); // 2w
4261 out
[3] = 0.5 * fRoot
;
4262 fRoot
= 0.5/fRoot; // 1/(4w
)
4263 out
[0] = (m
[5]-m
[7])*fRoot
;
4264 out
[1] = (m
[6]-m
[2])*fRoot
;
4265 out
[2] = (m
[1]-m
[3])*fRoot
;
4271 if ( m
[8] > m
[i
*3+i
] )
4276 fRoot
= Math
.sqrt(m
[i
*3+i
]-m
[j
*3+j
]-m
[k
*3+k
] + 1.0);
4277 out
[i
] = 0.5 * fRoot
;
4278 fRoot
= 0.5 / fRoot
;
4279 out
[3] = (m
[j
*3+k
] - m
[k
*3+j
]) * fRoot
;
4280 out
[j
] = (m
[j
*3+i
] + m
[i
*3+j
]) * fRoot
;
4281 out
[k
] = (m
[k
*3+i
] + m
[i
*3+k
]) * fRoot
;
4288 * Returns a string representation of a quatenion
4290 * @param {quat} vec vector to represent as a string
4291 * @returns {String} string representation of the vector
4293 quat
.str = function (a
) {
4294 return 'quat(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
4297 if(typeof(exports
) !== 'undefined') {
4298 exports
.quat
= quat
;