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 matricies
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
) {
433 * @param {vec2} out the receiving vector
434 * @param {vec2} a vector to normalize
435 * @returns {vec2} out
437 vec2
.normalize = function(out
, a
) {
442 //TODO: evaluate use of glm_invsqrt here?
443 len
= 1 / Math
.sqrt(len
);
451 * Calculates the dot product of two vec2's
453 * @param {vec2} a the first operand
454 * @param {vec2} b the second operand
455 * @returns {Number} dot product of a and b
457 vec2
.dot = function (a
, b
) {
458 return a
[0] * b
[0] + a
[1] * b
[1];
462 * Computes the cross product of two vec2's
463 * Note that the cross product must by definition produce a 3D vector
465 * @param {vec3} out the receiving vector
466 * @param {vec2} a the first operand
467 * @param {vec2} b the second operand
468 * @returns {vec3} out
470 vec2
.cross = function(out
, a
, b
) {
471 var z
= a
[0] * b
[1] - a
[1] * b
[0];
478 * Performs a linear interpolation between two vec2's
480 * @param {vec2} out the receiving vector
481 * @param {vec2} a the first operand
482 * @param {vec2} b the second operand
483 * @param {Number} t interpolation amount between the two inputs
484 * @returns {vec2} out
486 vec2
.lerp = function (out
, a
, b
, t
) {
489 out
[0] = ax
+ t
* (b
[0] - ax
);
490 out
[1] = ay
+ t
* (b
[1] - ay
);
495 * Generates a random vector with the given scale
497 * @param {vec2} out the receiving vector
498 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
499 * @returns {vec2} out
501 vec2
.random = function (out
, scale
) {
502 scale
= scale
|| 1.0;
503 var r
= GLMAT_RANDOM() * 2.0 * Math
.PI
;
504 out
[0] = Math
.cos(r
) * scale
;
505 out
[1] = Math
.sin(r
) * scale
;
510 * Transforms the vec2 with a mat2
512 * @param {vec2} out the receiving vector
513 * @param {vec2} a the vector to transform
514 * @param {mat2} m matrix to transform with
515 * @returns {vec2} out
517 vec2
.transformMat2 = function(out
, a
, m
) {
520 out
[0] = m
[0] * x
+ m
[2] * y
;
521 out
[1] = m
[1] * x
+ m
[3] * y
;
526 * Transforms the vec2 with a mat2d
528 * @param {vec2} out the receiving vector
529 * @param {vec2} a the vector to transform
530 * @param {mat2d} m matrix to transform with
531 * @returns {vec2} out
533 vec2
.transformMat2d = function(out
, a
, m
) {
536 out
[0] = m
[0] * x
+ m
[2] * y
+ m
[4];
537 out
[1] = m
[1] * x
+ m
[3] * y
+ m
[5];
542 * Transforms the vec2 with a mat3
543 * 3rd vector component is implicitly '1'
545 * @param {vec2} out the receiving vector
546 * @param {vec2} a the vector to transform
547 * @param {mat3} m matrix to transform with
548 * @returns {vec2} out
550 vec2
.transformMat3 = function(out
, a
, m
) {
553 out
[0] = m
[0] * x
+ m
[3] * y
+ m
[6];
554 out
[1] = m
[1] * x
+ m
[4] * y
+ m
[7];
559 * Transforms the vec2 with a mat4
560 * 3rd vector component is implicitly '0'
561 * 4th vector component is implicitly '1'
563 * @param {vec2} out the receiving vector
564 * @param {vec2} a the vector to transform
565 * @param {mat4} m matrix to transform with
566 * @returns {vec2} out
568 vec2
.transformMat4 = function(out
, a
, m
) {
571 out
[0] = m
[0] * x
+ m
[4] * y
+ m
[12];
572 out
[1] = m
[1] * x
+ m
[5] * y
+ m
[13];
577 * Perform some operation over an array of vec2s.
579 * @param {Array} a the array of vectors to iterate over
580 * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
581 * @param {Number} offset Number of elements to skip at the beginning of the array
582 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
583 * @param {Function} fn Function to call for each vector in the array
584 * @param {Object} [arg] additional argument to pass to fn
588 vec2
.forEach
= (function() {
589 var vec
= vec2
.create();
591 return function(a
, stride
, offset
, count
, fn
, arg
) {
602 l
= Math
.min((count
* stride
) + offset
, a
.length
);
607 for(i
= offset
; i
< l
; i
+= stride
) {
608 vec
[0] = a
[i
]; vec
[1] = a
[i
+1];
610 a
[i
] = vec
[0]; a
[i
+1] = vec
[1];
618 * Returns a string representation of a vector
620 * @param {vec2} vec vector to represent as a string
621 * @returns {String} string representation of the vector
623 vec2
.str = function (a
) {
624 return 'vec2(' + a
[0] + ', ' + a
[1] + ')';
627 if(typeof(exports
) !== 'undefined') {
631 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
633 Redistribution and use in source and binary forms, with or without modification,
634 are permitted provided that the following conditions are met:
636 * Redistributions of source code must retain the above copyright notice, this
637 list of conditions and the following disclaimer.
638 * Redistributions in binary form must reproduce the above copyright notice,
639 this list of conditions and the following disclaimer in the documentation
640 and/or other materials provided with the distribution.
642 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
643 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
644 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
645 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
646 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
647 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
648 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
649 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
650 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
651 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
654 * @class 3 Dimensional Vector
661 * Creates a new, empty vec3
663 * @returns {vec3} a new 3D vector
665 vec3
.create = function() {
666 var out
= new GLMAT_ARRAY_TYPE(3);
674 * Creates a new vec3 initialized with values from an existing vector
676 * @param {vec3} a vector to clone
677 * @returns {vec3} a new 3D vector
679 vec3
.clone = function(a
) {
680 var out
= new GLMAT_ARRAY_TYPE(3);
688 * Creates a new vec3 initialized with the given values
690 * @param {Number} x X component
691 * @param {Number} y Y component
692 * @param {Number} z Z component
693 * @returns {vec3} a new 3D vector
695 vec3
.fromValues = function(x
, y
, z
) {
696 var out
= new GLMAT_ARRAY_TYPE(3);
704 * Copy the values from one vec3 to another
706 * @param {vec3} out the receiving vector
707 * @param {vec3} a the source vector
708 * @returns {vec3} out
710 vec3
.copy = function(out
, a
) {
718 * Set the components of a vec3 to the given values
720 * @param {vec3} out the receiving vector
721 * @param {Number} x X component
722 * @param {Number} y Y component
723 * @param {Number} z Z component
724 * @returns {vec3} out
726 vec3
.set = function(out
, x
, y
, z
) {
736 * @param {vec3} out the receiving vector
737 * @param {vec3} a the first operand
738 * @param {vec3} b the second operand
739 * @returns {vec3} out
741 vec3
.add = function(out
, a
, b
) {
742 out
[0] = a
[0] + b
[0];
743 out
[1] = a
[1] + b
[1];
744 out
[2] = a
[2] + b
[2];
749 * Subtracts vector b from vector a
751 * @param {vec3} out the receiving vector
752 * @param {vec3} a the first operand
753 * @param {vec3} b the second operand
754 * @returns {vec3} out
756 vec3
.subtract = function(out
, a
, b
) {
757 out
[0] = a
[0] - b
[0];
758 out
[1] = a
[1] - b
[1];
759 out
[2] = a
[2] - b
[2];
764 * Alias for {@link vec3.subtract}
767 vec3
.sub
= vec3
.subtract
;
770 * Multiplies two vec3's
772 * @param {vec3} out the receiving vector
773 * @param {vec3} a the first operand
774 * @param {vec3} b the second operand
775 * @returns {vec3} out
777 vec3
.multiply = function(out
, a
, b
) {
778 out
[0] = a
[0] * b
[0];
779 out
[1] = a
[1] * b
[1];
780 out
[2] = a
[2] * b
[2];
785 * Alias for {@link vec3.multiply}
788 vec3
.mul
= vec3
.multiply
;
793 * @param {vec3} out the receiving vector
794 * @param {vec3} a the first operand
795 * @param {vec3} b the second operand
796 * @returns {vec3} out
798 vec3
.divide = function(out
, a
, b
) {
799 out
[0] = a
[0] / b
[0];
800 out
[1] = a
[1] / b
[1];
801 out
[2] = a
[2] / b
[2];
806 * Alias for {@link vec3.divide}
809 vec3
.div
= vec3
.divide
;
812 * Returns the minimum of two vec3's
814 * @param {vec3} out the receiving vector
815 * @param {vec3} a the first operand
816 * @param {vec3} b the second operand
817 * @returns {vec3} out
819 vec3
.min = function(out
, a
, b
) {
820 out
[0] = Math
.min(a
[0], b
[0]);
821 out
[1] = Math
.min(a
[1], b
[1]);
822 out
[2] = Math
.min(a
[2], b
[2]);
827 * Returns the maximum of two vec3's
829 * @param {vec3} out the receiving vector
830 * @param {vec3} a the first operand
831 * @param {vec3} b the second operand
832 * @returns {vec3} out
834 vec3
.max = function(out
, a
, b
) {
835 out
[0] = Math
.max(a
[0], b
[0]);
836 out
[1] = Math
.max(a
[1], b
[1]);
837 out
[2] = Math
.max(a
[2], b
[2]);
842 * Scales a vec3 by a scalar number
844 * @param {vec3} out the receiving vector
845 * @param {vec3} a the vector to scale
846 * @param {Number} b amount to scale the vector by
847 * @returns {vec3} out
849 vec3
.scale = function(out
, a
, b
) {
857 * Adds two vec3's after scaling the second operand by a scalar value
859 * @param {vec3} out the receiving vector
860 * @param {vec3} a the first operand
861 * @param {vec3} b the second operand
862 * @param {Number} scale the amount to scale b by before adding
863 * @returns {vec3} out
865 vec3
.scaleAndAdd = function(out
, a
, b
, scale
) {
866 out
[0] = a
[0] + (b
[0] * scale
);
867 out
[1] = a
[1] + (b
[1] * scale
);
868 out
[2] = a
[2] + (b
[2] * scale
);
873 * Calculates the euclidian distance between two vec3's
875 * @param {vec3} a the first operand
876 * @param {vec3} b the second operand
877 * @returns {Number} distance between a and b
879 vec3
.distance = function(a
, b
) {
883 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
);
887 * Alias for {@link vec3.distance}
890 vec3
.dist
= vec3
.distance
;
893 * Calculates the squared euclidian distance between two vec3's
895 * @param {vec3} a the first operand
896 * @param {vec3} b the second operand
897 * @returns {Number} squared distance between a and b
899 vec3
.squaredDistance = function(a
, b
) {
903 return x
*x
+ y
*y
+ z
*z
;
907 * Alias for {@link vec3.squaredDistance}
910 vec3
.sqrDist
= vec3
.squaredDistance
;
913 * Calculates the length of a vec3
915 * @param {vec3} a vector to calculate length of
916 * @returns {Number} length of a
918 vec3
.length = function (a
) {
922 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
);
926 * Alias for {@link vec3.length}
929 vec3
.len
= vec3
.length
;
932 * Calculates the squared length of a vec3
934 * @param {vec3} a vector to calculate squared length of
935 * @returns {Number} squared length of a
937 vec3
.squaredLength = function (a
) {
941 return x
*x
+ y
*y
+ z
*z
;
945 * Alias for {@link vec3.squaredLength}
948 vec3
.sqrLen
= vec3
.squaredLength
;
951 * Negates the components of a vec3
953 * @param {vec3} out the receiving vector
954 * @param {vec3} a vector to negate
955 * @returns {vec3} out
957 vec3
.negate = function(out
, a
) {
967 * @param {vec3} out the receiving vector
968 * @param {vec3} a vector to normalize
969 * @returns {vec3} out
971 vec3
.normalize = function(out
, a
) {
975 var len
= x
*x
+ y
*y
+ z
*z
;
977 //TODO: evaluate use of glm_invsqrt here?
978 len
= 1 / Math
.sqrt(len
);
987 * Calculates the dot product of two vec3's
989 * @param {vec3} a the first operand
990 * @param {vec3} b the second operand
991 * @returns {Number} dot product of a and b
993 vec3
.dot = function (a
, b
) {
994 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
998 * Computes the cross product of two vec3's
1000 * @param {vec3} out the receiving vector
1001 * @param {vec3} a the first operand
1002 * @param {vec3} b the second operand
1003 * @returns {vec3} out
1005 vec3
.cross = function(out
, a
, b
) {
1006 var ax
= a
[0], ay
= a
[1], az
= a
[2],
1007 bx
= b
[0], by
= b
[1], bz
= b
[2];
1009 out
[0] = ay
* bz
- az
* by
;
1010 out
[1] = az
* bx
- ax
* bz
;
1011 out
[2] = ax
* by
- ay
* bx
;
1016 * Performs a linear interpolation between two vec3's
1018 * @param {vec3} out the receiving vector
1019 * @param {vec3} a the first operand
1020 * @param {vec3} b the second operand
1021 * @param {Number} t interpolation amount between the two inputs
1022 * @returns {vec3} out
1024 vec3
.lerp = function (out
, a
, b
, t
) {
1028 out
[0] = ax
+ t
* (b
[0] - ax
);
1029 out
[1] = ay
+ t
* (b
[1] - ay
);
1030 out
[2] = az
+ t
* (b
[2] - az
);
1035 * Generates a random vector with the given scale
1037 * @param {vec3} out the receiving vector
1038 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1039 * @returns {vec3} out
1041 vec3
.random = function (out
, scale
) {
1042 scale
= scale
|| 1.0;
1044 var r
= GLMAT_RANDOM() * 2.0 * Math
.PI
;
1045 var z
= (GLMAT_RANDOM() * 2.0) - 1.0;
1046 var zScale
= Math
.sqrt(1.0-z
*z
) * scale
;
1048 out
[0] = Math
.cos(r
) * zScale
;
1049 out
[1] = Math
.sin(r
) * zScale
;
1055 * Transforms the vec3 with a mat4.
1056 * 4th vector component is implicitly '1'
1058 * @param {vec3} out the receiving vector
1059 * @param {vec3} a the vector to transform
1060 * @param {mat4} m matrix to transform with
1061 * @returns {vec3} out
1063 vec3
.transformMat4 = function(out
, a
, m
) {
1064 var x
= a
[0], y
= a
[1], z
= a
[2];
1065 out
[0] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1066 out
[1] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1067 out
[2] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1072 * Transforms the vec3 with a mat3.
1074 * @param {vec3} out the receiving vector
1075 * @param {vec3} a the vector to transform
1076 * @param {mat4} m the 3x3 matrix to transform with
1077 * @returns {vec3} out
1079 vec3
.transformMat3 = function(out
, a
, m
) {
1080 var x
= a
[0], y
= a
[1], z
= a
[2];
1081 out
[0] = x
* m
[0] + y
* m
[3] + z
* m
[6];
1082 out
[1] = x
* m
[1] + y
* m
[4] + z
* m
[7];
1083 out
[2] = x
* m
[2] + y
* m
[5] + z
* m
[8];
1088 * Transforms the vec3 with a quat
1090 * @param {vec3} out the receiving vector
1091 * @param {vec3} a the vector to transform
1092 * @param {quat} q quaternion to transform with
1093 * @returns {vec3} out
1095 vec3
.transformQuat = function(out
, a
, q
) {
1096 // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
1098 var x
= a
[0], y
= a
[1], z
= a
[2],
1099 qx
= q
[0], qy
= q
[1], qz
= q
[2], qw
= q
[3],
1101 // calculate quat * vec
1102 ix
= qw
* x
+ qy
* z
- qz
* y
,
1103 iy
= qw
* y
+ qz
* x
- qx
* z
,
1104 iz
= qw
* z
+ qx
* y
- qy
* x
,
1105 iw
= -qx
* x
- qy
* y
- qz
* z
;
1107 // calculate result * inverse quat
1108 out
[0] = ix
* qw
+ iw
* -qx
+ iy
* -qz
- iz
* -qy
;
1109 out
[1] = iy
* qw
+ iw
* -qy
+ iz
* -qx
- ix
* -qz
;
1110 out
[2] = iz
* qw
+ iw
* -qz
+ ix
* -qy
- iy
* -qx
;
1115 * Rotate a 3D vector around the x-axis
1116 * @param {vec3} out The receiving vec3
1117 * @param {vec3} a The vec3 point to rotate
1118 * @param {vec3} b The origin of the rotation
1119 * @param {Number} c The angle of rotation
1120 * @returns {vec3} out
1122 vec3
.rotateX = function(out
, a
, b
, c
){
1124 //Translate point to the origin
1131 r
[1] = p
[1]*Math
.cos(c
) - p
[2]*Math
.sin(c
);
1132 r
[2] = p
[1]*Math
.sin(c
) + p
[2]*Math
.cos(c
);
1134 //translate to correct position
1135 out
[0] = r
[0] + b
[0];
1136 out
[1] = r
[1] + b
[1];
1137 out
[2] = r
[2] + b
[2];
1143 * Rotate a 3D vector around the y-axis
1144 * @param {vec3} out The receiving vec3
1145 * @param {vec3} a The vec3 point to rotate
1146 * @param {vec3} b The origin of the rotation
1147 * @param {Number} c The angle of rotation
1148 * @returns {vec3} out
1150 vec3
.rotateY = function(out
, a
, b
, c
){
1152 //Translate point to the origin
1158 r
[0] = p
[2]*Math
.sin(c
) + p
[0]*Math
.cos(c
);
1160 r
[2] = p
[2]*Math
.cos(c
) - p
[0]*Math
.sin(c
);
1162 //translate to correct position
1163 out
[0] = r
[0] + b
[0];
1164 out
[1] = r
[1] + b
[1];
1165 out
[2] = r
[2] + b
[2];
1171 * Rotate a 3D vector around the z-axis
1172 * @param {vec3} out The receiving vec3
1173 * @param {vec3} a The vec3 point to rotate
1174 * @param {vec3} b The origin of the rotation
1175 * @param {Number} c The angle of rotation
1176 * @returns {vec3} out
1178 vec3
.rotateZ = function(out
, a
, b
, c
){
1180 //Translate point to the origin
1186 r
[0] = p
[0]*Math
.cos(c
) - p
[1]*Math
.sin(c
);
1187 r
[1] = p
[0]*Math
.sin(c
) + p
[1]*Math
.cos(c
);
1190 //translate to correct position
1191 out
[0] = r
[0] + b
[0];
1192 out
[1] = r
[1] + b
[1];
1193 out
[2] = r
[2] + b
[2];
1199 * Perform some operation over an array of vec3s.
1201 * @param {Array} a the array of vectors to iterate over
1202 * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
1203 * @param {Number} offset Number of elements to skip at the beginning of the array
1204 * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
1205 * @param {Function} fn Function to call for each vector in the array
1206 * @param {Object} [arg] additional argument to pass to fn
1207 * @returns {Array} a
1210 vec3
.forEach
= (function() {
1211 var vec
= vec3
.create();
1213 return function(a
, stride
, offset
, count
, fn
, arg
) {
1224 l
= Math
.min((count
* stride
) + offset
, a
.length
);
1229 for(i
= offset
; i
< l
; i
+= stride
) {
1230 vec
[0] = a
[i
]; vec
[1] = a
[i
+1]; vec
[2] = a
[i
+2];
1232 a
[i
] = vec
[0]; a
[i
+1] = vec
[1]; a
[i
+2] = vec
[2];
1240 * Returns a string representation of a vector
1242 * @param {vec3} vec vector to represent as a string
1243 * @returns {String} string representation of the vector
1245 vec3
.str = function (a
) {
1246 return 'vec3(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ')';
1249 if(typeof(exports
) !== 'undefined') {
1250 exports
.vec3
= vec3
;
1253 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1255 Redistribution and use in source and binary forms, with or without modification,
1256 are permitted provided that the following conditions are met:
1258 * Redistributions of source code must retain the above copyright notice, this
1259 list of conditions and the following disclaimer.
1260 * Redistributions in binary form must reproduce the above copyright notice,
1261 this list of conditions and the following disclaimer in the documentation
1262 and/or other materials provided with the distribution.
1264 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1265 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1266 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1267 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1268 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1269 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1270 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1271 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1272 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1273 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1276 * @class 4 Dimensional Vector
1283 * Creates a new, empty vec4
1285 * @returns {vec4} a new 4D vector
1287 vec4
.create = function() {
1288 var out
= new GLMAT_ARRAY_TYPE(4);
1297 * Creates a new vec4 initialized with values from an existing vector
1299 * @param {vec4} a vector to clone
1300 * @returns {vec4} a new 4D vector
1302 vec4
.clone = function(a
) {
1303 var out
= new GLMAT_ARRAY_TYPE(4);
1312 * Creates a new vec4 initialized with the given values
1314 * @param {Number} x X component
1315 * @param {Number} y Y component
1316 * @param {Number} z Z component
1317 * @param {Number} w W component
1318 * @returns {vec4} a new 4D vector
1320 vec4
.fromValues = function(x
, y
, z
, w
) {
1321 var out
= new GLMAT_ARRAY_TYPE(4);
1330 * Copy the values from one vec4 to another
1332 * @param {vec4} out the receiving vector
1333 * @param {vec4} a the source vector
1334 * @returns {vec4} out
1336 vec4
.copy = function(out
, a
) {
1345 * Set the components of a vec4 to the given values
1347 * @param {vec4} out the receiving vector
1348 * @param {Number} x X component
1349 * @param {Number} y Y component
1350 * @param {Number} z Z component
1351 * @param {Number} w W component
1352 * @returns {vec4} out
1354 vec4
.set = function(out
, x
, y
, z
, w
) {
1365 * @param {vec4} out the receiving vector
1366 * @param {vec4} a the first operand
1367 * @param {vec4} b the second operand
1368 * @returns {vec4} out
1370 vec4
.add = function(out
, a
, b
) {
1371 out
[0] = a
[0] + b
[0];
1372 out
[1] = a
[1] + b
[1];
1373 out
[2] = a
[2] + b
[2];
1374 out
[3] = a
[3] + b
[3];
1379 * Subtracts vector b from vector a
1381 * @param {vec4} out the receiving vector
1382 * @param {vec4} a the first operand
1383 * @param {vec4} b the second operand
1384 * @returns {vec4} out
1386 vec4
.subtract = function(out
, a
, b
) {
1387 out
[0] = a
[0] - b
[0];
1388 out
[1] = a
[1] - b
[1];
1389 out
[2] = a
[2] - b
[2];
1390 out
[3] = a
[3] - b
[3];
1395 * Alias for {@link vec4.subtract}
1398 vec4
.sub
= vec4
.subtract
;
1401 * Multiplies two vec4's
1403 * @param {vec4} out the receiving vector
1404 * @param {vec4} a the first operand
1405 * @param {vec4} b the second operand
1406 * @returns {vec4} out
1408 vec4
.multiply = function(out
, a
, b
) {
1409 out
[0] = a
[0] * b
[0];
1410 out
[1] = a
[1] * b
[1];
1411 out
[2] = a
[2] * b
[2];
1412 out
[3] = a
[3] * b
[3];
1417 * Alias for {@link vec4.multiply}
1420 vec4
.mul
= vec4
.multiply
;
1423 * Divides two vec4's
1425 * @param {vec4} out the receiving vector
1426 * @param {vec4} a the first operand
1427 * @param {vec4} b the second operand
1428 * @returns {vec4} out
1430 vec4
.divide = function(out
, a
, b
) {
1431 out
[0] = a
[0] / b
[0];
1432 out
[1] = a
[1] / b
[1];
1433 out
[2] = a
[2] / b
[2];
1434 out
[3] = a
[3] / b
[3];
1439 * Alias for {@link vec4.divide}
1442 vec4
.div
= vec4
.divide
;
1445 * Returns the minimum of two vec4's
1447 * @param {vec4} out the receiving vector
1448 * @param {vec4} a the first operand
1449 * @param {vec4} b the second operand
1450 * @returns {vec4} out
1452 vec4
.min = function(out
, a
, b
) {
1453 out
[0] = Math
.min(a
[0], b
[0]);
1454 out
[1] = Math
.min(a
[1], b
[1]);
1455 out
[2] = Math
.min(a
[2], b
[2]);
1456 out
[3] = Math
.min(a
[3], b
[3]);
1461 * Returns the maximum of two vec4's
1463 * @param {vec4} out the receiving vector
1464 * @param {vec4} a the first operand
1465 * @param {vec4} b the second operand
1466 * @returns {vec4} out
1468 vec4
.max = function(out
, a
, b
) {
1469 out
[0] = Math
.max(a
[0], b
[0]);
1470 out
[1] = Math
.max(a
[1], b
[1]);
1471 out
[2] = Math
.max(a
[2], b
[2]);
1472 out
[3] = Math
.max(a
[3], b
[3]);
1477 * Scales a vec4 by a scalar number
1479 * @param {vec4} out the receiving vector
1480 * @param {vec4} a the vector to scale
1481 * @param {Number} b amount to scale the vector by
1482 * @returns {vec4} out
1484 vec4
.scale = function(out
, a
, b
) {
1493 * Adds two vec4's after scaling the second operand by a scalar value
1495 * @param {vec4} out the receiving vector
1496 * @param {vec4} a the first operand
1497 * @param {vec4} b the second operand
1498 * @param {Number} scale the amount to scale b by before adding
1499 * @returns {vec4} out
1501 vec4
.scaleAndAdd = function(out
, a
, b
, scale
) {
1502 out
[0] = a
[0] + (b
[0] * scale
);
1503 out
[1] = a
[1] + (b
[1] * scale
);
1504 out
[2] = a
[2] + (b
[2] * scale
);
1505 out
[3] = a
[3] + (b
[3] * scale
);
1510 * Calculates the euclidian distance between two vec4's
1512 * @param {vec4} a the first operand
1513 * @param {vec4} b the second operand
1514 * @returns {Number} distance between a and b
1516 vec4
.distance = function(a
, b
) {
1517 var x
= b
[0] - a
[0],
1521 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
+ w
*w
);
1525 * Alias for {@link vec4.distance}
1528 vec4
.dist
= vec4
.distance
;
1531 * Calculates the squared euclidian distance between two vec4's
1533 * @param {vec4} a the first operand
1534 * @param {vec4} b the second operand
1535 * @returns {Number} squared distance between a and b
1537 vec4
.squaredDistance = function(a
, b
) {
1538 var x
= b
[0] - a
[0],
1542 return x
*x
+ y
*y
+ z
*z
+ w
*w
;
1546 * Alias for {@link vec4.squaredDistance}
1549 vec4
.sqrDist
= vec4
.squaredDistance
;
1552 * Calculates the length of a vec4
1554 * @param {vec4} a vector to calculate length of
1555 * @returns {Number} length of a
1557 vec4
.length = function (a
) {
1562 return Math
.sqrt(x
*x
+ y
*y
+ z
*z
+ w
*w
);
1566 * Alias for {@link vec4.length}
1569 vec4
.len
= vec4
.length
;
1572 * Calculates the squared length of a vec4
1574 * @param {vec4} a vector to calculate squared length of
1575 * @returns {Number} squared length of a
1577 vec4
.squaredLength = function (a
) {
1582 return x
*x
+ y
*y
+ z
*z
+ w
*w
;
1586 * Alias for {@link vec4.squaredLength}
1589 vec4
.sqrLen
= vec4
.squaredLength
;
1592 * Negates the components of a vec4
1594 * @param {vec4} out the receiving vector
1595 * @param {vec4} a vector to negate
1596 * @returns {vec4} out
1598 vec4
.negate = function(out
, a
) {
1609 * @param {vec4} out the receiving vector
1610 * @param {vec4} a vector to normalize
1611 * @returns {vec4} out
1613 vec4
.normalize = function(out
, a
) {
1618 var len
= x
*x
+ y
*y
+ z
*z
+ w
*w
;
1620 len
= 1 / Math
.sqrt(len
);
1621 out
[0] = a
[0] * len
;
1622 out
[1] = a
[1] * len
;
1623 out
[2] = a
[2] * len
;
1624 out
[3] = a
[3] * len
;
1630 * Calculates the dot product of two vec4's
1632 * @param {vec4} a the first operand
1633 * @param {vec4} b the second operand
1634 * @returns {Number} dot product of a and b
1636 vec4
.dot = function (a
, b
) {
1637 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
1641 * Performs a linear interpolation between two vec4's
1643 * @param {vec4} out the receiving vector
1644 * @param {vec4} a the first operand
1645 * @param {vec4} b the second operand
1646 * @param {Number} t interpolation amount between the two inputs
1647 * @returns {vec4} out
1649 vec4
.lerp = function (out
, a
, b
, t
) {
1654 out
[0] = ax
+ t
* (b
[0] - ax
);
1655 out
[1] = ay
+ t
* (b
[1] - ay
);
1656 out
[2] = az
+ t
* (b
[2] - az
);
1657 out
[3] = aw
+ t
* (b
[3] - aw
);
1662 * Generates a random vector with the given scale
1664 * @param {vec4} out the receiving vector
1665 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1666 * @returns {vec4} out
1668 vec4
.random = function (out
, scale
) {
1669 scale
= scale
|| 1.0;
1671 //TODO: This is a pretty awful way of doing this. Find something better.
1672 out
[0] = GLMAT_RANDOM();
1673 out
[1] = GLMAT_RANDOM();
1674 out
[2] = GLMAT_RANDOM();
1675 out
[3] = GLMAT_RANDOM();
1676 vec4
.normalize(out
, out
);
1677 vec4
.scale(out
, out
, scale
);
1682 * Transforms the vec4 with a mat4.
1684 * @param {vec4} out the receiving vector
1685 * @param {vec4} a the vector to transform
1686 * @param {mat4} m matrix to transform with
1687 * @returns {vec4} out
1689 vec4
.transformMat4 = function(out
, a
, m
) {
1690 var x
= a
[0], y
= a
[1], z
= a
[2], w
= a
[3];
1691 out
[0] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12] * w
;
1692 out
[1] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13] * w
;
1693 out
[2] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14] * w
;
1694 out
[3] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15] * w
;
1699 * Transforms the vec4 with a quat
1701 * @param {vec4} out the receiving vector
1702 * @param {vec4} a the vector to transform
1703 * @param {quat} q quaternion to transform with
1704 * @returns {vec4} out
1706 vec4
.transformQuat = function(out
, a
, q
) {
1707 var x
= a
[0], y
= a
[1], z
= a
[2],
1708 qx
= q
[0], qy
= q
[1], qz
= q
[2], qw
= q
[3],
1710 // calculate quat * vec
1711 ix
= qw
* x
+ qy
* z
- qz
* y
,
1712 iy
= qw
* y
+ qz
* x
- qx
* z
,
1713 iz
= qw
* z
+ qx
* y
- qy
* x
,
1714 iw
= -qx
* x
- qy
* y
- qz
* z
;
1716 // calculate result * inverse quat
1717 out
[0] = ix
* qw
+ iw
* -qx
+ iy
* -qz
- iz
* -qy
;
1718 out
[1] = iy
* qw
+ iw
* -qy
+ iz
* -qx
- ix
* -qz
;
1719 out
[2] = iz
* qw
+ iw
* -qz
+ ix
* -qy
- iy
* -qx
;
1724 * Perform some operation over an array of vec4s.
1726 * @param {Array} a the array of vectors to iterate over
1727 * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
1728 * @param {Number} offset Number of elements to skip at the beginning of the array
1729 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
1730 * @param {Function} fn Function to call for each vector in the array
1731 * @param {Object} [arg] additional argument to pass to fn
1732 * @returns {Array} a
1735 vec4
.forEach
= (function() {
1736 var vec
= vec4
.create();
1738 return function(a
, stride
, offset
, count
, fn
, arg
) {
1749 l
= Math
.min((count
* stride
) + offset
, a
.length
);
1754 for(i
= offset
; i
< l
; i
+= stride
) {
1755 vec
[0] = a
[i
]; vec
[1] = a
[i
+1]; vec
[2] = a
[i
+2]; vec
[3] = a
[i
+3];
1757 a
[i
] = vec
[0]; a
[i
+1] = vec
[1]; a
[i
+2] = vec
[2]; a
[i
+3] = vec
[3];
1765 * Returns a string representation of a vector
1767 * @param {vec4} vec vector to represent as a string
1768 * @returns {String} string representation of the vector
1770 vec4
.str = function (a
) {
1771 return 'vec4(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
1774 if(typeof(exports
) !== 'undefined') {
1775 exports
.vec4
= vec4
;
1778 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1780 Redistribution and use in source and binary forms, with or without modification,
1781 are permitted provided that the following conditions are met:
1783 * Redistributions of source code must retain the above copyright notice, this
1784 list of conditions and the following disclaimer.
1785 * Redistributions in binary form must reproduce the above copyright notice,
1786 this list of conditions and the following disclaimer in the documentation
1787 and/or other materials provided with the distribution.
1789 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1790 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1791 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1792 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1793 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1794 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1795 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1796 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1797 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1798 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1808 * Creates a new identity mat2
1810 * @returns {mat2} a new 2x2 matrix
1812 mat2
.create = function() {
1813 var out
= new GLMAT_ARRAY_TYPE(4);
1822 * Creates a new mat2 initialized with values from an existing matrix
1824 * @param {mat2} a matrix to clone
1825 * @returns {mat2} a new 2x2 matrix
1827 mat2
.clone = function(a
) {
1828 var out
= new GLMAT_ARRAY_TYPE(4);
1837 * Copy the values from one mat2 to another
1839 * @param {mat2} out the receiving matrix
1840 * @param {mat2} a the source matrix
1841 * @returns {mat2} out
1843 mat2
.copy = function(out
, a
) {
1852 * Set a mat2 to the identity matrix
1854 * @param {mat2} out the receiving matrix
1855 * @returns {mat2} out
1857 mat2
.identity = function(out
) {
1866 * Transpose the values of a mat2
1868 * @param {mat2} out the receiving matrix
1869 * @param {mat2} a the source matrix
1870 * @returns {mat2} out
1872 mat2
.transpose = function(out
, a
) {
1873 // If we are transposing ourselves we can skip a few steps but have to cache some values
1891 * @param {mat2} out the receiving matrix
1892 * @param {mat2} a the source matrix
1893 * @returns {mat2} out
1895 mat2
.invert = function(out
, a
) {
1896 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
1898 // Calculate the determinant
1899 det
= a0
* a3
- a2
* a1
;
1915 * Calculates the adjugate of a mat2
1917 * @param {mat2} out the receiving matrix
1918 * @param {mat2} a the source matrix
1919 * @returns {mat2} out
1921 mat2
.adjoint = function(out
, a
) {
1922 // Caching this value is nessecary if out == a
1933 * Calculates the determinant of a mat2
1935 * @param {mat2} a the source matrix
1936 * @returns {Number} determinant of a
1938 mat2
.determinant = function (a
) {
1939 return a
[0] * a
[3] - a
[2] * a
[1];
1943 * Multiplies two mat2's
1945 * @param {mat2} out the receiving matrix
1946 * @param {mat2} a the first operand
1947 * @param {mat2} b the second operand
1948 * @returns {mat2} out
1950 mat2
.multiply = function (out
, a
, b
) {
1951 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3];
1952 var b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3];
1953 out
[0] = a0
* b0
+ a2
* b1
;
1954 out
[1] = a1
* b0
+ a3
* b1
;
1955 out
[2] = a0
* b2
+ a2
* b3
;
1956 out
[3] = a1
* b2
+ a3
* b3
;
1961 * Alias for {@link mat2.multiply}
1964 mat2
.mul
= mat2
.multiply
;
1967 * Rotates a mat2 by the given angle
1969 * @param {mat2} out the receiving matrix
1970 * @param {mat2} a the matrix to rotate
1971 * @param {Number} rad the angle to rotate the matrix by
1972 * @returns {mat2} out
1974 mat2
.rotate = function (out
, a
, rad
) {
1975 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
1978 out
[0] = a0
* c
+ a2
* s
;
1979 out
[1] = a1
* c
+ a3
* s
;
1980 out
[2] = a0
* -s
+ a2
* c
;
1981 out
[3] = a1
* -s
+ a3
* c
;
1986 * Scales the mat2 by the dimensions in the given vec2
1988 * @param {mat2} out the receiving matrix
1989 * @param {mat2} a the matrix to rotate
1990 * @param {vec2} v the vec2 to scale the matrix by
1991 * @returns {mat2} out
1993 mat2
.scale = function(out
, a
, v
) {
1994 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
1995 v0
= v
[0], v1
= v
[1];
2004 * Returns a string representation of a mat2
2006 * @param {mat2} mat matrix to represent as a string
2007 * @returns {String} string representation of the matrix
2009 mat2
.str = function (a
) {
2010 return 'mat2(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
2014 * Returns Frobenius norm of a mat2
2016 * @param {mat2} a the matrix to calculate Frobenius norm of
2017 * @returns {Number} Frobenius norm
2019 mat2
.frob = function (a
) {
2020 return(Math
.sqrt(Math
.pow(a
[0], 2) + Math
.pow(a
[1], 2) + Math
.pow(a
[2], 2) + Math
.pow(a
[3], 2)))
2024 * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
2025 * @param {mat2} L the lower triangular matrix
2026 * @param {mat2} D the diagonal matrix
2027 * @param {mat2} U the upper triangular matrix
2028 * @param {mat2} a the input matrix to factorize
2031 mat2
.LDU = function (L
, D
, U
, a
) {
2035 U
[3] = a
[3] - L
[2] * U
[1];
2039 if(typeof(exports
) !== 'undefined') {
2040 exports
.mat2
= mat2
;
2043 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2045 Redistribution and use in source and binary forms, with or without modification,
2046 are permitted provided that the following conditions are met:
2048 * Redistributions of source code must retain the above copyright notice, this
2049 list of conditions and the following disclaimer.
2050 * Redistributions in binary form must reproduce the above copyright notice,
2051 this list of conditions and the following disclaimer in the documentation
2052 and/or other materials provided with the distribution.
2054 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2055 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2056 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2057 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2058 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2059 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2060 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2061 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2062 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2063 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2070 * A mat2d contains six elements defined as:
2075 * This is a short form for the 3x3 matrix:
2081 * The last row is ignored so the array is shorter and operations are faster.
2087 * Creates a new identity mat2d
2089 * @returns {mat2d} a new 2x3 matrix
2091 mat2d
.create = function() {
2092 var out
= new GLMAT_ARRAY_TYPE(6);
2103 * Creates a new mat2d initialized with values from an existing matrix
2105 * @param {mat2d} a matrix to clone
2106 * @returns {mat2d} a new 2x3 matrix
2108 mat2d
.clone = function(a
) {
2109 var out
= new GLMAT_ARRAY_TYPE(6);
2120 * Copy the values from one mat2d to another
2122 * @param {mat2d} out the receiving matrix
2123 * @param {mat2d} a the source matrix
2124 * @returns {mat2d} out
2126 mat2d
.copy = function(out
, a
) {
2137 * Set a mat2d to the identity matrix
2139 * @param {mat2d} out the receiving matrix
2140 * @returns {mat2d} out
2142 mat2d
.identity = function(out
) {
2155 * @param {mat2d} out the receiving matrix
2156 * @param {mat2d} a the source matrix
2157 * @returns {mat2d} out
2159 mat2d
.invert = function(out
, a
) {
2160 var aa
= a
[0], ab
= a
[1], ac
= a
[2], ad
= a
[3],
2161 atx
= a
[4], aty
= a
[5];
2163 var det
= aa
* ad
- ab
* ac
;
2173 out
[4] = (ac
* aty
- ad
* atx
) * det
;
2174 out
[5] = (ab
* atx
- aa
* aty
) * det
;
2179 * Calculates the determinant of a mat2d
2181 * @param {mat2d} a the source matrix
2182 * @returns {Number} determinant of a
2184 mat2d
.determinant = function (a
) {
2185 return a
[0] * a
[3] - a
[1] * a
[2];
2189 * Multiplies two mat2d's
2191 * @param {mat2d} out the receiving matrix
2192 * @param {mat2d} a the first operand
2193 * @param {mat2d} b the second operand
2194 * @returns {mat2d} out
2196 mat2d
.multiply = function (out
, a
, b
) {
2197 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2198 b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3], b4
= b
[4], b5
= b
[5];
2199 out
[0] = a0
* b0
+ a2
* b1
;
2200 out
[1] = a1
* b0
+ a3
* b1
;
2201 out
[2] = a0
* b2
+ a2
* b3
;
2202 out
[3] = a1
* b2
+ a3
* b3
;
2203 out
[4] = a0
* b4
+ a2
* b5
+ a4
;
2204 out
[5] = a1
* b4
+ a3
* b5
+ a5
;
2209 * Alias for {@link mat2d.multiply}
2212 mat2d
.mul
= mat2d
.multiply
;
2216 * Rotates a mat2d by the given angle
2218 * @param {mat2d} out the receiving matrix
2219 * @param {mat2d} a the matrix to rotate
2220 * @param {Number} rad the angle to rotate the matrix by
2221 * @returns {mat2d} out
2223 mat2d
.rotate = function (out
, a
, rad
) {
2224 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2227 out
[0] = a0
* c
+ a2
* s
;
2228 out
[1] = a1
* c
+ a3
* s
;
2229 out
[2] = a0
* -s
+ a2
* c
;
2230 out
[3] = a1
* -s
+ a3
* c
;
2237 * Scales the mat2d by the dimensions in the given vec2
2239 * @param {mat2d} out the receiving matrix
2240 * @param {mat2d} a the matrix to translate
2241 * @param {vec2} v the vec2 to scale the matrix by
2242 * @returns {mat2d} out
2244 mat2d
.scale = function(out
, a
, v
) {
2245 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2246 v0
= v
[0], v1
= v
[1];
2257 * Translates the mat2d by the dimensions in the given vec2
2259 * @param {mat2d} out the receiving matrix
2260 * @param {mat2d} a the matrix to translate
2261 * @param {vec2} v the vec2 to translate the matrix by
2262 * @returns {mat2d} out
2264 mat2d
.translate = function(out
, a
, v
) {
2265 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3], a4
= a
[4], a5
= a
[5],
2266 v0
= v
[0], v1
= v
[1];
2271 out
[4] = a0
* v0
+ a2
* v1
+ a4
;
2272 out
[5] = a1
* v0
+ a3
* v1
+ a5
;
2277 * Returns a string representation of a mat2d
2279 * @param {mat2d} a matrix to represent as a string
2280 * @returns {String} string representation of the matrix
2282 mat2d
.str = function (a
) {
2283 return 'mat2d(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' +
2284 a
[3] + ', ' + a
[4] + ', ' + a
[5] + ')';
2288 * Returns Frobenius norm of a mat2d
2290 * @param {mat2d} a the matrix to calculate Frobenius norm of
2291 * @returns {Number} Frobenius norm
2293 mat2d
.frob = function (a
) {
2294 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))
2297 if(typeof(exports
) !== 'undefined') {
2298 exports
.mat2d
= mat2d
;
2301 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2303 Redistribution and use in source and binary forms, with or without modification,
2304 are permitted provided that the following conditions are met:
2306 * Redistributions of source code must retain the above copyright notice, this
2307 list of conditions and the following disclaimer.
2308 * Redistributions in binary form must reproduce the above copyright notice,
2309 this list of conditions and the following disclaimer in the documentation
2310 and/or other materials provided with the distribution.
2312 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2313 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2314 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2315 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2316 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2317 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2318 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2319 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2320 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2321 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2331 * Creates a new identity mat3
2333 * @returns {mat3} a new 3x3 matrix
2335 mat3
.create = function() {
2336 var out
= new GLMAT_ARRAY_TYPE(9);
2350 * Copies the upper-left 3x3 values into the given mat3.
2352 * @param {mat3} out the receiving 3x3 matrix
2353 * @param {mat4} a the source 4x4 matrix
2354 * @returns {mat3} out
2356 mat3
.fromMat4 = function(out
, a
) {
2370 * Creates a new mat3 initialized with values from an existing matrix
2372 * @param {mat3} a matrix to clone
2373 * @returns {mat3} a new 3x3 matrix
2375 mat3
.clone = function(a
) {
2376 var out
= new GLMAT_ARRAY_TYPE(9);
2390 * Copy the values from one mat3 to another
2392 * @param {mat3} out the receiving matrix
2393 * @param {mat3} a the source matrix
2394 * @returns {mat3} out
2396 mat3
.copy = function(out
, a
) {
2410 * Set a mat3 to the identity matrix
2412 * @param {mat3} out the receiving matrix
2413 * @returns {mat3} out
2415 mat3
.identity = function(out
) {
2429 * Transpose the values of a mat3
2431 * @param {mat3} out the receiving matrix
2432 * @param {mat3} a the source matrix
2433 * @returns {mat3} out
2435 mat3
.transpose = function(out
, a
) {
2436 // If we are transposing ourselves we can skip a few steps but have to cache some values
2438 var a01
= a
[1], a02
= a
[2], a12
= a
[5];
2463 * @param {mat3} out the receiving matrix
2464 * @param {mat3} a the source matrix
2465 * @returns {mat3} out
2467 mat3
.invert = function(out
, a
) {
2468 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2469 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2470 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2472 b01
= a22
* a11
- a12
* a21
,
2473 b11
= -a22
* a10
+ a12
* a20
,
2474 b21
= a21
* a10
- a11
* a20
,
2476 // Calculate the determinant
2477 det
= a00
* b01
+ a01
* b11
+ a02
* b21
;
2485 out
[1] = (-a22
* a01
+ a02
* a21
) * det
;
2486 out
[2] = (a12
* a01
- a02
* a11
) * det
;
2488 out
[4] = (a22
* a00
- a02
* a20
) * det
;
2489 out
[5] = (-a12
* a00
+ a02
* a10
) * det
;
2491 out
[7] = (-a21
* a00
+ a01
* a20
) * det
;
2492 out
[8] = (a11
* a00
- a01
* a10
) * det
;
2497 * Calculates the adjugate of a mat3
2499 * @param {mat3} out the receiving matrix
2500 * @param {mat3} a the source matrix
2501 * @returns {mat3} out
2503 mat3
.adjoint = function(out
, a
) {
2504 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2505 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2506 a20
= a
[6], a21
= a
[7], a22
= a
[8];
2508 out
[0] = (a11
* a22
- a12
* a21
);
2509 out
[1] = (a02
* a21
- a01
* a22
);
2510 out
[2] = (a01
* a12
- a02
* a11
);
2511 out
[3] = (a12
* a20
- a10
* a22
);
2512 out
[4] = (a00
* a22
- a02
* a20
);
2513 out
[5] = (a02
* a10
- a00
* a12
);
2514 out
[6] = (a10
* a21
- a11
* a20
);
2515 out
[7] = (a01
* a20
- a00
* a21
);
2516 out
[8] = (a00
* a11
- a01
* a10
);
2521 * Calculates the determinant of a mat3
2523 * @param {mat3} a the source matrix
2524 * @returns {Number} determinant of a
2526 mat3
.determinant = function (a
) {
2527 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2528 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2529 a20
= a
[6], a21
= a
[7], a22
= a
[8];
2531 return a00
* (a22
* a11
- a12
* a21
) + a01
* (-a22
* a10
+ a12
* a20
) + a02
* (a21
* a10
- a11
* a20
);
2535 * Multiplies two mat3's
2537 * @param {mat3} out the receiving matrix
2538 * @param {mat3} a the first operand
2539 * @param {mat3} b the second operand
2540 * @returns {mat3} out
2542 mat3
.multiply = function (out
, a
, b
) {
2543 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2544 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2545 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2547 b00
= b
[0], b01
= b
[1], b02
= b
[2],
2548 b10
= b
[3], b11
= b
[4], b12
= b
[5],
2549 b20
= b
[6], b21
= b
[7], b22
= b
[8];
2551 out
[0] = b00
* a00
+ b01
* a10
+ b02
* a20
;
2552 out
[1] = b00
* a01
+ b01
* a11
+ b02
* a21
;
2553 out
[2] = b00
* a02
+ b01
* a12
+ b02
* a22
;
2555 out
[3] = b10
* a00
+ b11
* a10
+ b12
* a20
;
2556 out
[4] = b10
* a01
+ b11
* a11
+ b12
* a21
;
2557 out
[5] = b10
* a02
+ b11
* a12
+ b12
* a22
;
2559 out
[6] = b20
* a00
+ b21
* a10
+ b22
* a20
;
2560 out
[7] = b20
* a01
+ b21
* a11
+ b22
* a21
;
2561 out
[8] = b20
* a02
+ b21
* a12
+ b22
* a22
;
2566 * Alias for {@link mat3.multiply}
2569 mat3
.mul
= mat3
.multiply
;
2572 * Translate a mat3 by the given vector
2574 * @param {mat3} out the receiving matrix
2575 * @param {mat3} a the matrix to translate
2576 * @param {vec2} v vector to translate by
2577 * @returns {mat3} out
2579 mat3
.translate = function(out
, a
, v
) {
2580 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2581 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2582 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2593 out
[6] = x
* a00
+ y
* a10
+ a20
;
2594 out
[7] = x
* a01
+ y
* a11
+ a21
;
2595 out
[8] = x
* a02
+ y
* a12
+ a22
;
2600 * Rotates a mat3 by the given angle
2602 * @param {mat3} out the receiving matrix
2603 * @param {mat3} a the matrix to rotate
2604 * @param {Number} rad the angle to rotate the matrix by
2605 * @returns {mat3} out
2607 mat3
.rotate = function (out
, a
, rad
) {
2608 var a00
= a
[0], a01
= a
[1], a02
= a
[2],
2609 a10
= a
[3], a11
= a
[4], a12
= a
[5],
2610 a20
= a
[6], a21
= a
[7], a22
= a
[8],
2615 out
[0] = c
* a00
+ s
* a10
;
2616 out
[1] = c
* a01
+ s
* a11
;
2617 out
[2] = c
* a02
+ s
* a12
;
2619 out
[3] = c
* a10
- s
* a00
;
2620 out
[4] = c
* a11
- s
* a01
;
2621 out
[5] = c
* a12
- s
* a02
;
2630 * Scales the mat3 by the dimensions in the given vec2
2632 * @param {mat3} out the receiving matrix
2633 * @param {mat3} a the matrix to rotate
2634 * @param {vec2} v the vec2 to scale the matrix by
2635 * @returns {mat3} out
2637 mat3
.scale = function(out
, a
, v
) {
2638 var x
= v
[0], y
= v
[1];
2655 * Copies the values from a mat2d into a mat3
2657 * @param {mat3} out the receiving matrix
2658 * @param {mat2d} a the matrix to copy
2659 * @returns {mat3} out
2661 mat3
.fromMat2d = function(out
, a
) {
2677 * Calculates a 3x3 matrix from the given quaternion
2679 * @param {mat3} out mat3 receiving operation result
2680 * @param {quat} q Quaternion to create matrix from
2682 * @returns {mat3} out
2684 mat3
.fromQuat = function (out
, q
) {
2685 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
2700 out
[0] = 1 - yy
- zz
;
2705 out
[4] = 1 - xx
- zz
;
2710 out
[8] = 1 - xx
- yy
;
2716 * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
2718 * @param {mat3} out mat3 receiving operation result
2719 * @param {mat4} a Mat4 to derive the normal matrix from
2721 * @returns {mat3} out
2723 mat3
.normalFromMat4 = function (out
, a
) {
2724 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
2725 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
2726 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
2727 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
2729 b00
= a00
* a11
- a01
* a10
,
2730 b01
= a00
* a12
- a02
* a10
,
2731 b02
= a00
* a13
- a03
* a10
,
2732 b03
= a01
* a12
- a02
* a11
,
2733 b04
= a01
* a13
- a03
* a11
,
2734 b05
= a02
* a13
- a03
* a12
,
2735 b06
= a20
* a31
- a21
* a30
,
2736 b07
= a20
* a32
- a22
* a30
,
2737 b08
= a20
* a33
- a23
* a30
,
2738 b09
= a21
* a32
- a22
* a31
,
2739 b10
= a21
* a33
- a23
* a31
,
2740 b11
= a22
* a33
- a23
* a32
,
2742 // Calculate the determinant
2743 det
= b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
2750 out
[0] = (a11
* b11
- a12
* b10
+ a13
* b09
) * det
;
2751 out
[1] = (a12
* b08
- a10
* b11
- a13
* b07
) * det
;
2752 out
[2] = (a10
* b10
- a11
* b08
+ a13
* b06
) * det
;
2754 out
[3] = (a02
* b10
- a01
* b11
- a03
* b09
) * det
;
2755 out
[4] = (a00
* b11
- a02
* b08
+ a03
* b07
) * det
;
2756 out
[5] = (a01
* b08
- a00
* b10
- a03
* b06
) * det
;
2758 out
[6] = (a31
* b05
- a32
* b04
+ a33
* b03
) * det
;
2759 out
[7] = (a32
* b02
- a30
* b05
- a33
* b01
) * det
;
2760 out
[8] = (a30
* b04
- a31
* b02
+ a33
* b00
) * det
;
2766 * Returns a string representation of a mat3
2768 * @param {mat3} mat matrix to represent as a string
2769 * @returns {String} string representation of the matrix
2771 mat3
.str = function (a
) {
2772 return 'mat3(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' +
2773 a
[3] + ', ' + a
[4] + ', ' + a
[5] + ', ' +
2774 a
[6] + ', ' + a
[7] + ', ' + a
[8] + ')';
2778 * Returns Frobenius norm of a mat3
2780 * @param {mat3} a the matrix to calculate Frobenius norm of
2781 * @returns {Number} Frobenius norm
2783 mat3
.frob = function (a
) {
2784 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)))
2788 if(typeof(exports
) !== 'undefined') {
2789 exports
.mat3
= mat3
;
2792 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2794 Redistribution and use in source and binary forms, with or without modification,
2795 are permitted provided that the following conditions are met:
2797 * Redistributions of source code must retain the above copyright notice, this
2798 list of conditions and the following disclaimer.
2799 * Redistributions in binary form must reproduce the above copyright notice,
2800 this list of conditions and the following disclaimer in the documentation
2801 and/or other materials provided with the distribution.
2803 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2804 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2805 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2806 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2807 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2808 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2809 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2810 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2811 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2812 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2822 * Creates a new identity mat4
2824 * @returns {mat4} a new 4x4 matrix
2826 mat4
.create = function() {
2827 var out
= new GLMAT_ARRAY_TYPE(16);
2848 * Creates a new mat4 initialized with values from an existing matrix
2850 * @param {mat4} a matrix to clone
2851 * @returns {mat4} a new 4x4 matrix
2853 mat4
.clone = function(a
) {
2854 var out
= new GLMAT_ARRAY_TYPE(16);
2875 * Copy the values from one mat4 to another
2877 * @param {mat4} out the receiving matrix
2878 * @param {mat4} a the source matrix
2879 * @returns {mat4} out
2881 mat4
.copy = function(out
, a
) {
2902 * Set a mat4 to the identity matrix
2904 * @param {mat4} out the receiving matrix
2905 * @returns {mat4} out
2907 mat4
.identity = function(out
) {
2928 * Transpose the values of a mat4
2930 * @param {mat4} out the receiving matrix
2931 * @param {mat4} a the source matrix
2932 * @returns {mat4} out
2934 mat4
.transpose = function(out
, a
) {
2935 // If we are transposing ourselves we can skip a few steps but have to cache some values
2937 var a01
= a
[1], a02
= a
[2], a03
= a
[3],
2938 a12
= a
[6], a13
= a
[7],
2978 * @param {mat4} out the receiving matrix
2979 * @param {mat4} a the source matrix
2980 * @returns {mat4} out
2982 mat4
.invert = function(out
, a
) {
2983 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
2984 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
2985 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
2986 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
2988 b00
= a00
* a11
- a01
* a10
,
2989 b01
= a00
* a12
- a02
* a10
,
2990 b02
= a00
* a13
- a03
* a10
,
2991 b03
= a01
* a12
- a02
* a11
,
2992 b04
= a01
* a13
- a03
* a11
,
2993 b05
= a02
* a13
- a03
* a12
,
2994 b06
= a20
* a31
- a21
* a30
,
2995 b07
= a20
* a32
- a22
* a30
,
2996 b08
= a20
* a33
- a23
* a30
,
2997 b09
= a21
* a32
- a22
* a31
,
2998 b10
= a21
* a33
- a23
* a31
,
2999 b11
= a22
* a33
- a23
* a32
,
3001 // Calculate the determinant
3002 det
= b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
3009 out
[0] = (a11
* b11
- a12
* b10
+ a13
* b09
) * det
;
3010 out
[1] = (a02
* b10
- a01
* b11
- a03
* b09
) * det
;
3011 out
[2] = (a31
* b05
- a32
* b04
+ a33
* b03
) * det
;
3012 out
[3] = (a22
* b04
- a21
* b05
- a23
* b03
) * det
;
3013 out
[4] = (a12
* b08
- a10
* b11
- a13
* b07
) * det
;
3014 out
[5] = (a00
* b11
- a02
* b08
+ a03
* b07
) * det
;
3015 out
[6] = (a32
* b02
- a30
* b05
- a33
* b01
) * det
;
3016 out
[7] = (a20
* b05
- a22
* b02
+ a23
* b01
) * det
;
3017 out
[8] = (a10
* b10
- a11
* b08
+ a13
* b06
) * det
;
3018 out
[9] = (a01
* b08
- a00
* b10
- a03
* b06
) * det
;
3019 out
[10] = (a30
* b04
- a31
* b02
+ a33
* b00
) * det
;
3020 out
[11] = (a21
* b02
- a20
* b04
- a23
* b00
) * det
;
3021 out
[12] = (a11
* b07
- a10
* b09
- a12
* b06
) * det
;
3022 out
[13] = (a00
* b09
- a01
* b07
+ a02
* b06
) * det
;
3023 out
[14] = (a31
* b01
- a30
* b03
- a32
* b00
) * det
;
3024 out
[15] = (a20
* b03
- a21
* b01
+ a22
* b00
) * det
;
3030 * Calculates the adjugate of a mat4
3032 * @param {mat4} out the receiving matrix
3033 * @param {mat4} a the source matrix
3034 * @returns {mat4} out
3036 mat4
.adjoint = function(out
, a
) {
3037 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3038 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3039 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3040 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15];
3042 out
[0] = (a11
* (a22
* a33
- a23
* a32
) - a21
* (a12
* a33
- a13
* a32
) + a31
* (a12
* a23
- a13
* a22
));
3043 out
[1] = -(a01
* (a22
* a33
- a23
* a32
) - a21
* (a02
* a33
- a03
* a32
) + a31
* (a02
* a23
- a03
* a22
));
3044 out
[2] = (a01
* (a12
* a33
- a13
* a32
) - a11
* (a02
* a33
- a03
* a32
) + a31
* (a02
* a13
- a03
* a12
));
3045 out
[3] = -(a01
* (a12
* a23
- a13
* a22
) - a11
* (a02
* a23
- a03
* a22
) + a21
* (a02
* a13
- a03
* a12
));
3046 out
[4] = -(a10
* (a22
* a33
- a23
* a32
) - a20
* (a12
* a33
- a13
* a32
) + a30
* (a12
* a23
- a13
* a22
));
3047 out
[5] = (a00
* (a22
* a33
- a23
* a32
) - a20
* (a02
* a33
- a03
* a32
) + a30
* (a02
* a23
- a03
* a22
));
3048 out
[6] = -(a00
* (a12
* a33
- a13
* a32
) - a10
* (a02
* a33
- a03
* a32
) + a30
* (a02
* a13
- a03
* a12
));
3049 out
[7] = (a00
* (a12
* a23
- a13
* a22
) - a10
* (a02
* a23
- a03
* a22
) + a20
* (a02
* a13
- a03
* a12
));
3050 out
[8] = (a10
* (a21
* a33
- a23
* a31
) - a20
* (a11
* a33
- a13
* a31
) + a30
* (a11
* a23
- a13
* a21
));
3051 out
[9] = -(a00
* (a21
* a33
- a23
* a31
) - a20
* (a01
* a33
- a03
* a31
) + a30
* (a01
* a23
- a03
* a21
));
3052 out
[10] = (a00
* (a11
* a33
- a13
* a31
) - a10
* (a01
* a33
- a03
* a31
) + a30
* (a01
* a13
- a03
* a11
));
3053 out
[11] = -(a00
* (a11
* a23
- a13
* a21
) - a10
* (a01
* a23
- a03
* a21
) + a20
* (a01
* a13
- a03
* a11
));
3054 out
[12] = -(a10
* (a21
* a32
- a22
* a31
) - a20
* (a11
* a32
- a12
* a31
) + a30
* (a11
* a22
- a12
* a21
));
3055 out
[13] = (a00
* (a21
* a32
- a22
* a31
) - a20
* (a01
* a32
- a02
* a31
) + a30
* (a01
* a22
- a02
* a21
));
3056 out
[14] = -(a00
* (a11
* a32
- a12
* a31
) - a10
* (a01
* a32
- a02
* a31
) + a30
* (a01
* a12
- a02
* a11
));
3057 out
[15] = (a00
* (a11
* a22
- a12
* a21
) - a10
* (a01
* a22
- a02
* a21
) + a20
* (a01
* a12
- a02
* a11
));
3062 * Calculates the determinant of a mat4
3064 * @param {mat4} a the source matrix
3065 * @returns {Number} determinant of a
3067 mat4
.determinant = function (a
) {
3068 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3069 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3070 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3071 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15],
3073 b00
= a00
* a11
- a01
* a10
,
3074 b01
= a00
* a12
- a02
* a10
,
3075 b02
= a00
* a13
- a03
* a10
,
3076 b03
= a01
* a12
- a02
* a11
,
3077 b04
= a01
* a13
- a03
* a11
,
3078 b05
= a02
* a13
- a03
* a12
,
3079 b06
= a20
* a31
- a21
* a30
,
3080 b07
= a20
* a32
- a22
* a30
,
3081 b08
= a20
* a33
- a23
* a30
,
3082 b09
= a21
* a32
- a22
* a31
,
3083 b10
= a21
* a33
- a23
* a31
,
3084 b11
= a22
* a33
- a23
* a32
;
3086 // Calculate the determinant
3087 return b00
* b11
- b01
* b10
+ b02
* b09
+ b03
* b08
- b04
* b07
+ b05
* b06
;
3091 * Multiplies two mat4's
3093 * @param {mat4} out the receiving matrix
3094 * @param {mat4} a the first operand
3095 * @param {mat4} b the second operand
3096 * @returns {mat4} out
3098 mat4
.multiply = function (out
, a
, b
) {
3099 var a00
= a
[0], a01
= a
[1], a02
= a
[2], a03
= a
[3],
3100 a10
= a
[4], a11
= a
[5], a12
= a
[6], a13
= a
[7],
3101 a20
= a
[8], a21
= a
[9], a22
= a
[10], a23
= a
[11],
3102 a30
= a
[12], a31
= a
[13], a32
= a
[14], a33
= a
[15];
3104 // Cache only the current line of the second matrix
3105 var b0
= b
[0], b1
= b
[1], b2
= b
[2], b3
= b
[3];
3106 out
[0] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3107 out
[1] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3108 out
[2] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3109 out
[3] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3111 b0
= b
[4]; b1
= b
[5]; b2
= b
[6]; b3
= b
[7];
3112 out
[4] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3113 out
[5] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3114 out
[6] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3115 out
[7] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3117 b0
= b
[8]; b1
= b
[9]; b2
= b
[10]; b3
= b
[11];
3118 out
[8] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3119 out
[9] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3120 out
[10] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3121 out
[11] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3123 b0
= b
[12]; b1
= b
[13]; b2
= b
[14]; b3
= b
[15];
3124 out
[12] = b0
*a00
+ b1
*a10
+ b2
*a20
+ b3
*a30
;
3125 out
[13] = b0
*a01
+ b1
*a11
+ b2
*a21
+ b3
*a31
;
3126 out
[14] = b0
*a02
+ b1
*a12
+ b2
*a22
+ b3
*a32
;
3127 out
[15] = b0
*a03
+ b1
*a13
+ b2
*a23
+ b3
*a33
;
3132 * Alias for {@link mat4.multiply}
3135 mat4
.mul
= mat4
.multiply
;
3138 * Translate a mat4 by the given vector
3140 * @param {mat4} out the receiving matrix
3141 * @param {mat4} a the matrix to translate
3142 * @param {vec3} v vector to translate by
3143 * @returns {mat4} out
3145 mat4
.translate = function (out
, a
, v
) {
3146 var x
= v
[0], y
= v
[1], z
= v
[2],
3152 out
[12] = a
[0] * x
+ a
[4] * y
+ a
[8] * z
+ a
[12];
3153 out
[13] = a
[1] * x
+ a
[5] * y
+ a
[9] * z
+ a
[13];
3154 out
[14] = a
[2] * x
+ a
[6] * y
+ a
[10] * z
+ a
[14];
3155 out
[15] = a
[3] * x
+ a
[7] * y
+ a
[11] * z
+ a
[15];
3157 a00
= a
[0]; a01
= a
[1]; a02
= a
[2]; a03
= a
[3];
3158 a10
= a
[4]; a11
= a
[5]; a12
= a
[6]; a13
= a
[7];
3159 a20
= a
[8]; a21
= a
[9]; a22
= a
[10]; a23
= a
[11];
3161 out
[0] = a00
; out
[1] = a01
; out
[2] = a02
; out
[3] = a03
;
3162 out
[4] = a10
; out
[5] = a11
; out
[6] = a12
; out
[7] = a13
;
3163 out
[8] = a20
; out
[9] = a21
; out
[10] = a22
; out
[11] = a23
;
3165 out
[12] = a00
* x
+ a10
* y
+ a20
* z
+ a
[12];
3166 out
[13] = a01
* x
+ a11
* y
+ a21
* z
+ a
[13];
3167 out
[14] = a02
* x
+ a12
* y
+ a22
* z
+ a
[14];
3168 out
[15] = a03
* x
+ a13
* y
+ a23
* z
+ a
[15];
3175 * Scales the mat4 by the dimensions in the given vec3
3177 * @param {mat4} out the receiving matrix
3178 * @param {mat4} a the matrix to scale
3179 * @param {vec3} v the vec3 to scale the matrix by
3180 * @returns {mat4} out
3182 mat4
.scale = function(out
, a
, v
) {
3183 var x
= v
[0], y
= v
[1], z
= v
[2];
3195 out
[10] = a
[10] * z
;
3196 out
[11] = a
[11] * z
;
3205 * Rotates a mat4 by the given angle
3207 * @param {mat4} out the receiving matrix
3208 * @param {mat4} a the matrix to rotate
3209 * @param {Number} rad the angle to rotate the matrix by
3210 * @param {vec3} axis the axis to rotate around
3211 * @returns {mat4} out
3213 mat4
.rotate = function (out
, a
, rad
, axis
) {
3214 var x
= axis
[0], y
= axis
[1], z
= axis
[2],
3215 len
= Math
.sqrt(x
* x
+ y
* y
+ z
* z
),
3224 if (Math
.abs(len
) < GLMAT_EPSILON
) { return null; }
3235 a00
= a
[0]; a01
= a
[1]; a02
= a
[2]; a03
= a
[3];
3236 a10
= a
[4]; a11
= a
[5]; a12
= a
[6]; a13
= a
[7];
3237 a20
= a
[8]; a21
= a
[9]; a22
= a
[10]; a23
= a
[11];
3239 // Construct the elements of the rotation matrix
3240 b00
= x
* x
* t
+ c
; b01
= y
* x
* t
+ z
* s
; b02
= z
* x
* t
- y
* s
;
3241 b10
= x
* y
* t
- z
* s
; b11
= y
* y
* t
+ c
; b12
= z
* y
* t
+ x
* s
;
3242 b20
= x
* z
* t
+ y
* s
; b21
= y
* z
* t
- x
* s
; b22
= z
* z
* t
+ c
;
3244 // Perform rotation-specific matrix multiplication
3245 out
[0] = a00
* b00
+ a10
* b01
+ a20
* b02
;
3246 out
[1] = a01
* b00
+ a11
* b01
+ a21
* b02
;
3247 out
[2] = a02
* b00
+ a12
* b01
+ a22
* b02
;
3248 out
[3] = a03
* b00
+ a13
* b01
+ a23
* b02
;
3249 out
[4] = a00
* b10
+ a10
* b11
+ a20
* b12
;
3250 out
[5] = a01
* b10
+ a11
* b11
+ a21
* b12
;
3251 out
[6] = a02
* b10
+ a12
* b11
+ a22
* b12
;
3252 out
[7] = a03
* b10
+ a13
* b11
+ a23
* b12
;
3253 out
[8] = a00
* b20
+ a10
* b21
+ a20
* b22
;
3254 out
[9] = a01
* b20
+ a11
* b21
+ a21
* b22
;
3255 out
[10] = a02
* b20
+ a12
* b21
+ a22
* b22
;
3256 out
[11] = a03
* b20
+ a13
* b21
+ a23
* b22
;
3258 if (a
!== out
) { // If the source and destination differ, copy the unchanged last row
3268 * Rotates a matrix by the given angle around the X axis
3270 * @param {mat4} out the receiving matrix
3271 * @param {mat4} a the matrix to rotate
3272 * @param {Number} rad the angle to rotate the matrix by
3273 * @returns {mat4} out
3275 mat4
.rotateX = function (out
, a
, rad
) {
3276 var s
= Math
.sin(rad
),
3287 if (a
!== out
) { // If the source and destination differ, copy the unchanged rows
3298 // Perform axis-specific matrix multiplication
3299 out
[4] = a10
* c
+ a20
* s
;
3300 out
[5] = a11
* c
+ a21
* s
;
3301 out
[6] = a12
* c
+ a22
* s
;
3302 out
[7] = a13
* c
+ a23
* s
;
3303 out
[8] = a20
* c
- a10
* s
;
3304 out
[9] = a21
* c
- a11
* s
;
3305 out
[10] = a22
* c
- a12
* s
;
3306 out
[11] = a23
* c
- a13
* s
;
3311 * Rotates a matrix by the given angle around the Y axis
3313 * @param {mat4} out the receiving matrix
3314 * @param {mat4} a the matrix to rotate
3315 * @param {Number} rad the angle to rotate the matrix by
3316 * @returns {mat4} out
3318 mat4
.rotateY = function (out
, a
, rad
) {
3319 var s
= Math
.sin(rad
),
3330 if (a
!== out
) { // If the source and destination differ, copy the unchanged rows
3341 // Perform axis-specific matrix multiplication
3342 out
[0] = a00
* c
- a20
* s
;
3343 out
[1] = a01
* c
- a21
* s
;
3344 out
[2] = a02
* c
- a22
* s
;
3345 out
[3] = a03
* c
- a23
* s
;
3346 out
[8] = a00
* s
+ a20
* c
;
3347 out
[9] = a01
* s
+ a21
* c
;
3348 out
[10] = a02
* s
+ a22
* c
;
3349 out
[11] = a03
* s
+ a23
* c
;
3354 * Rotates a matrix by the given angle around the Z axis
3356 * @param {mat4} out the receiving matrix
3357 * @param {mat4} a the matrix to rotate
3358 * @param {Number} rad the angle to rotate the matrix by
3359 * @returns {mat4} out
3361 mat4
.rotateZ = function (out
, a
, rad
) {
3362 var s
= Math
.sin(rad
),
3373 if (a
!== out
) { // If the source and destination differ, copy the unchanged last row
3384 // Perform axis-specific matrix multiplication
3385 out
[0] = a00
* c
+ a10
* s
;
3386 out
[1] = a01
* c
+ a11
* s
;
3387 out
[2] = a02
* c
+ a12
* s
;
3388 out
[3] = a03
* c
+ a13
* s
;
3389 out
[4] = a10
* c
- a00
* s
;
3390 out
[5] = a11
* c
- a01
* s
;
3391 out
[6] = a12
* c
- a02
* s
;
3392 out
[7] = a13
* c
- a03
* s
;
3397 * Creates a matrix from a quaternion rotation and vector translation
3398 * This is equivalent to (but much faster than):
3400 * mat4.identity(dest);
3401 * mat4.translate(dest, vec);
3402 * var quatMat = mat4.create();
3403 * quat4.toMat4(quat, quatMat);
3404 * mat4.multiply(dest, quatMat);
3406 * @param {mat4} out mat4 receiving operation result
3407 * @param {quat4} q Rotation quaternion
3408 * @param {vec3} v Translation vector
3409 * @returns {mat4} out
3411 mat4
.fromRotationTranslation = function (out
, q
, v
) {
3413 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
3428 out
[0] = 1 - (yy
+ zz
);
3433 out
[5] = 1 - (xx
+ zz
);
3438 out
[10] = 1 - (xx
+ yy
);
3448 mat4
.fromQuat = function (out
, q
) {
3449 var x
= q
[0], y
= q
[1], z
= q
[2], w
= q
[3],
3464 out
[0] = 1 - yy
- zz
;
3470 out
[5] = 1 - xx
- zz
;
3476 out
[10] = 1 - xx
- yy
;
3488 * Generates a frustum matrix with the given bounds
3490 * @param {mat4} out mat4 frustum matrix will be written into
3491 * @param {Number} left Left bound of the frustum
3492 * @param {Number} right Right bound of the frustum
3493 * @param {Number} bottom Bottom bound of the frustum
3494 * @param {Number} top Top bound of the frustum
3495 * @param {Number} near Near bound of the frustum
3496 * @param {Number} far Far bound of the frustum
3497 * @returns {mat4} out
3499 mat4
.frustum = function (out
, left
, right
, bottom
, top
, near
, far
) {
3500 var rl
= 1 / (right
- left
),
3501 tb
= 1 / (top
- bottom
),
3502 nf
= 1 / (near
- far
);
3503 out
[0] = (near
* 2) * rl
;
3508 out
[5] = (near
* 2) * tb
;
3511 out
[8] = (right
+ left
) * rl
;
3512 out
[9] = (top
+ bottom
) * tb
;
3513 out
[10] = (far
+ near
) * nf
;
3517 out
[14] = (far
* near
* 2) * nf
;
3523 * Generates a perspective projection matrix with the given bounds
3525 * @param {mat4} out mat4 frustum matrix will be written into
3526 * @param {number} fovy Vertical field of view in radians
3527 * @param {number} aspect Aspect ratio. typically viewport width/height
3528 * @param {number} near Near bound of the frustum
3529 * @param {number} far Far bound of the frustum
3530 * @returns {mat4} out
3532 mat4
.perspective = function (out
, fovy
, aspect
, near
, far
) {
3533 var f
= 1.0 / Math
.tan(fovy
/ 2),
3534 nf
= 1 / (near
- far
);
3535 out
[0] = f
/ aspect
;
3545 out
[10] = (far
+ near
) * nf
;
3549 out
[14] = (2 * far
* near
) * nf
;
3555 * Generates a orthogonal projection 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
.ortho = function (out
, left
, right
, bottom
, top
, near
, far
) {
3567 var lr
= 1 / (left
- right
),
3568 bt
= 1 / (bottom
- top
),
3569 nf
= 1 / (near
- far
);
3582 out
[12] = (left
+ right
) * lr
;
3583 out
[13] = (top
+ bottom
) * bt
;
3584 out
[14] = (far
+ near
) * nf
;
3590 * Generates a look-at matrix with the given eye position, focal point, and up axis
3592 * @param {mat4} out mat4 frustum matrix will be written into
3593 * @param {vec3} eye Position of the viewer
3594 * @param {vec3} center Point the viewer is looking at
3595 * @param {vec3} up vec3 pointing up
3596 * @returns {mat4} out
3598 mat4
.lookAt = function (out
, eye
, center
, up
) {
3599 var x0
, x1
, x2
, y0
, y1
, y2
, z0
, z1
, z2
, len
,
3606 centerx
= center
[0],
3607 centery
= center
[1],
3608 centerz
= center
[2];
3610 if (Math
.abs(eyex
- centerx
) < GLMAT_EPSILON
&&
3611 Math
.abs(eyey
- centery
) < GLMAT_EPSILON
&&
3612 Math
.abs(eyez
- centerz
) < GLMAT_EPSILON
) {
3613 return mat4
.identity(out
);
3616 z0
= eyex
- centerx
;
3617 z1
= eyey
- centery
;
3618 z2
= eyez
- centerz
;
3620 len
= 1 / Math
.sqrt(z0
* z0
+ z1
* z1
+ z2
* z2
);
3625 x0
= upy
* z2
- upz
* z1
;
3626 x1
= upz
* z0
- upx
* z2
;
3627 x2
= upx
* z1
- upy
* z0
;
3628 len
= Math
.sqrt(x0
* x0
+ x1
* x1
+ x2
* x2
);
3640 y0
= z1
* x2
- z2
* x1
;
3641 y1
= z2
* x0
- z0
* x2
;
3642 y2
= z0
* x1
- z1
* x0
;
3644 len
= Math
.sqrt(y0
* y0
+ y1
* y1
+ y2
* y2
);
3668 out
[12] = -(x0
* eyex
+ x1
* eyey
+ x2
* eyez
);
3669 out
[13] = -(y0
* eyex
+ y1
* eyey
+ y2
* eyez
);
3670 out
[14] = -(z0
* eyex
+ z1
* eyey
+ z2
* eyez
);
3677 * Returns a string representation of a mat4
3679 * @param {mat4} mat matrix to represent as a string
3680 * @returns {String} string representation of the matrix
3682 mat4
.str = function (a
) {
3683 return 'mat4(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ', ' +
3684 a
[4] + ', ' + a
[5] + ', ' + a
[6] + ', ' + a
[7] + ', ' +
3685 a
[8] + ', ' + a
[9] + ', ' + a
[10] + ', ' + a
[11] + ', ' +
3686 a
[12] + ', ' + a
[13] + ', ' + a
[14] + ', ' + a
[15] + ')';
3690 * Returns Frobenius norm of a mat4
3692 * @param {mat4} a the matrix to calculate Frobenius norm of
3693 * @returns {Number} Frobenius norm
3695 mat4
.frob = function (a
) {
3696 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
[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) ))
3700 if(typeof(exports
) !== 'undefined') {
3701 exports
.mat4
= mat4
;
3704 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3706 Redistribution and use in source and binary forms, with or without modification,
3707 are permitted provided that the following conditions are met:
3709 * Redistributions of source code must retain the above copyright notice, this
3710 list of conditions and the following disclaimer.
3711 * Redistributions in binary form must reproduce the above copyright notice,
3712 this list of conditions and the following disclaimer in the documentation
3713 and/or other materials provided with the distribution.
3715 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
3716 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3717 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3718 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
3719 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3720 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3721 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
3722 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3723 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3724 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
3734 * Creates a new identity quat
3736 * @returns {quat} a new quaternion
3738 quat
.create = function() {
3739 var out
= new GLMAT_ARRAY_TYPE(4);
3748 * Sets a quaternion to represent the shortest rotation from one
3749 * vector to another.
3751 * Both vectors are assumed to be unit length.
3753 * @param {quat} out the receiving quaternion.
3754 * @param {vec3} a the initial vector
3755 * @param {vec3} b the destination vector
3756 * @returns {quat} out
3758 quat
.rotationTo
= (function() {
3759 var tmpvec3
= vec3
.create();
3760 var xUnitVec3
= vec3
.fromValues(1,0,0);
3761 var yUnitVec3
= vec3
.fromValues(0,1,0);
3763 return function(out
, a
, b
) {
3764 var dot
= vec3
.dot(a
, b
);
3765 if (dot
< -0.999999) {
3766 vec3
.cross(tmpvec3
, xUnitVec3
, a
);
3767 if (vec3
.length(tmpvec3
) < 0.000001)
3768 vec3
.cross(tmpvec3
, yUnitVec3
, a
);
3769 vec3
.normalize(tmpvec3
, tmpvec3
);
3770 quat
.setAxisAngle(out
, tmpvec3
, Math
.PI
);
3772 } else if (dot
> 0.999999) {
3779 vec3
.cross(tmpvec3
, a
, b
);
3780 out
[0] = tmpvec3
[0];
3781 out
[1] = tmpvec3
[1];
3782 out
[2] = tmpvec3
[2];
3784 return quat
.normalize(out
, out
);
3790 * Sets the specified quaternion with values corresponding to the given
3791 * axes. Each axis is a vec3 and is expected to be unit length and
3792 * perpendicular to all other specified axes.
3794 * @param {vec3} view the vector representing the viewing direction
3795 * @param {vec3} right the vector representing the local "right" direction
3796 * @param {vec3} up the vector representing the local "up" direction
3797 * @returns {quat} out
3799 quat
.setAxes
= (function() {
3800 var matr
= mat3
.create();
3802 return function(out
, view
, right
, up
) {
3815 return quat
.normalize(out
, quat
.fromMat3(out
, matr
));
3820 * Creates a new quat initialized with values from an existing quaternion
3822 * @param {quat} a quaternion to clone
3823 * @returns {quat} a new quaternion
3826 quat
.clone
= vec4
.clone
;
3829 * Creates a new quat initialized with the given values
3831 * @param {Number} x X component
3832 * @param {Number} y Y component
3833 * @param {Number} z Z component
3834 * @param {Number} w W component
3835 * @returns {quat} a new quaternion
3838 quat
.fromValues
= vec4
.fromValues
;
3841 * Copy the values from one quat to another
3843 * @param {quat} out the receiving quaternion
3844 * @param {quat} a the source quaternion
3845 * @returns {quat} out
3848 quat
.copy
= vec4
.copy
;
3851 * Set the components of a quat to the given values
3853 * @param {quat} out the receiving quaternion
3854 * @param {Number} x X component
3855 * @param {Number} y Y component
3856 * @param {Number} z Z component
3857 * @param {Number} w W component
3858 * @returns {quat} out
3861 quat
.set = vec4
.set;
3864 * Set a quat to the identity quaternion
3866 * @param {quat} out the receiving quaternion
3867 * @returns {quat} out
3869 quat
.identity = function(out
) {
3878 * Sets a quat from the given angle and rotation axis,
3881 * @param {quat} out the receiving quaternion
3882 * @param {vec3} axis the axis around which to rotate
3883 * @param {Number} rad the angle in radians
3884 * @returns {quat} out
3886 quat
.setAxisAngle = function(out
, axis
, rad
) {
3888 var s
= Math
.sin(rad
);
3889 out
[0] = s
* axis
[0];
3890 out
[1] = s
* axis
[1];
3891 out
[2] = s
* axis
[2];
3892 out
[3] = Math
.cos(rad
);
3899 * @param {quat} out the receiving quaternion
3900 * @param {quat} a the first operand
3901 * @param {quat} b the second operand
3902 * @returns {quat} out
3905 quat
.add
= vec4
.add
;
3908 * Multiplies two quat's
3910 * @param {quat} out the receiving quaternion
3911 * @param {quat} a the first operand
3912 * @param {quat} b the second operand
3913 * @returns {quat} out
3915 quat
.multiply = function(out
, a
, b
) {
3916 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
3917 bx
= b
[0], by
= b
[1], bz
= b
[2], bw
= b
[3];
3919 out
[0] = ax
* bw
+ aw
* bx
+ ay
* bz
- az
* by
;
3920 out
[1] = ay
* bw
+ aw
* by
+ az
* bx
- ax
* bz
;
3921 out
[2] = az
* bw
+ aw
* bz
+ ax
* by
- ay
* bx
;
3922 out
[3] = aw
* bw
- ax
* bx
- ay
* by
- az
* bz
;
3927 * Alias for {@link quat.multiply}
3930 quat
.mul
= quat
.multiply
;
3933 * Scales a quat by a scalar number
3935 * @param {quat} out the receiving vector
3936 * @param {quat} a the vector to scale
3937 * @param {Number} b amount to scale the vector by
3938 * @returns {quat} out
3941 quat
.scale
= vec4
.scale
;
3944 * Rotates a quaternion by the given angle about the X axis
3946 * @param {quat} out quat receiving operation result
3947 * @param {quat} a quat to rotate
3948 * @param {number} rad angle (in radians) to rotate
3949 * @returns {quat} out
3951 quat
.rotateX = function (out
, a
, rad
) {
3954 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
3955 bx
= Math
.sin(rad
), bw
= Math
.cos(rad
);
3957 out
[0] = ax
* bw
+ aw
* bx
;
3958 out
[1] = ay
* bw
+ az
* bx
;
3959 out
[2] = az
* bw
- ay
* bx
;
3960 out
[3] = aw
* bw
- ax
* bx
;
3965 * Rotates a quaternion by the given angle about the Y axis
3967 * @param {quat} out quat receiving operation result
3968 * @param {quat} a quat to rotate
3969 * @param {number} rad angle (in radians) to rotate
3970 * @returns {quat} out
3972 quat
.rotateY = function (out
, a
, rad
) {
3975 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
3976 by
= Math
.sin(rad
), bw
= Math
.cos(rad
);
3978 out
[0] = ax
* bw
- az
* by
;
3979 out
[1] = ay
* bw
+ aw
* by
;
3980 out
[2] = az
* bw
+ ax
* by
;
3981 out
[3] = aw
* bw
- ay
* by
;
3986 * Rotates a quaternion by the given angle about the Z axis
3988 * @param {quat} out quat receiving operation result
3989 * @param {quat} a quat to rotate
3990 * @param {number} rad angle (in radians) to rotate
3991 * @returns {quat} out
3993 quat
.rotateZ = function (out
, a
, rad
) {
3996 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
3997 bz
= Math
.sin(rad
), bw
= Math
.cos(rad
);
3999 out
[0] = ax
* bw
+ ay
* bz
;
4000 out
[1] = ay
* bw
- ax
* bz
;
4001 out
[2] = az
* bw
+ aw
* bz
;
4002 out
[3] = aw
* bw
- az
* bz
;
4007 * Calculates the W component of a quat from the X, Y, and Z components.
4008 * Assumes that quaternion is 1 unit in length.
4009 * Any existing W component will be ignored.
4011 * @param {quat} out the receiving quaternion
4012 * @param {quat} a quat to calculate W component of
4013 * @returns {quat} out
4015 quat
.calculateW = function (out
, a
) {
4016 var x
= a
[0], y
= a
[1], z
= a
[2];
4021 out
[3] = -Math
.sqrt(Math
.abs(1.0 - x
* x
- y
* y
- z
* z
));
4026 * Calculates the dot product of two quat's
4028 * @param {quat} a the first operand
4029 * @param {quat} b the second operand
4030 * @returns {Number} dot product of a and b
4033 quat
.dot
= vec4
.dot
;
4036 * Performs a linear interpolation between two quat's
4038 * @param {quat} out the receiving quaternion
4039 * @param {quat} a the first operand
4040 * @param {quat} b the second operand
4041 * @param {Number} t interpolation amount between the two inputs
4042 * @returns {quat} out
4045 quat
.lerp
= vec4
.lerp
;
4048 * Performs a spherical linear interpolation between two quat
4050 * @param {quat} out the receiving quaternion
4051 * @param {quat} a the first operand
4052 * @param {quat} b the second operand
4053 * @param {Number} t interpolation amount between the two inputs
4054 * @returns {quat} out
4056 quat
.slerp = function (out
, a
, b
, t
) {
4058 // http://jsperf.com/quaternion-slerp-implementations
4060 var ax
= a
[0], ay
= a
[1], az
= a
[2], aw
= a
[3],
4061 bx
= b
[0], by
= b
[1], bz
= b
[2], bw
= b
[3];
4063 var omega
, cosom
, sinom
, scale0
, scale1
;
4066 cosom
= ax
* bx
+ ay
* by
+ az
* bz
+ aw
* bw
;
4067 // adjust signs (if necessary)
4068 if ( cosom
< 0.0 ) {
4075 // calculate coefficients
4076 if ( (1.0 - cosom
) > 0.000001 ) {
4077 // standard case (slerp)
4078 omega
= Math
.acos(cosom
);
4079 sinom
= Math
.sin(omega
);
4080 scale0
= Math
.sin((1.0 - t
) * omega
) / sinom
;
4081 scale1
= Math
.sin(t
* omega
) / sinom
;
4083 // "from" and "to" quaternions are very close
4084 // ... so we can do a linear interpolation
4088 // calculate final values
4089 out
[0] = scale0
* ax
+ scale1
* bx
;
4090 out
[1] = scale0
* ay
+ scale1
* by
;
4091 out
[2] = scale0
* az
+ scale1
* bz
;
4092 out
[3] = scale0
* aw
+ scale1
* bw
;
4098 * Calculates the inverse of a quat
4100 * @param {quat} out the receiving quaternion
4101 * @param {quat} a quat to calculate inverse of
4102 * @returns {quat} out
4104 quat
.invert = function(out
, a
) {
4105 var a0
= a
[0], a1
= a
[1], a2
= a
[2], a3
= a
[3],
4106 dot
= a0
*a0
+ a1
*a1
+ a2
*a2
+ a3
*a3
,
4107 invDot
= dot
? 1.0/dot
: 0;
4109 // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
4111 out
[0] = -a0
*invDot
;
4112 out
[1] = -a1
*invDot
;
4113 out
[2] = -a2
*invDot
;
4119 * Calculates the conjugate of a quat
4120 * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
4122 * @param {quat} out the receiving quaternion
4123 * @param {quat} a quat to calculate conjugate of
4124 * @returns {quat} out
4126 quat
.conjugate = function (out
, a
) {
4135 * Calculates the length of a quat
4137 * @param {quat} a vector to calculate length of
4138 * @returns {Number} length of a
4141 quat
.length
= vec4
.length
;
4144 * Alias for {@link quat.length}
4147 quat
.len
= quat
.length
;
4150 * Calculates the squared length of a quat
4152 * @param {quat} a vector to calculate squared length of
4153 * @returns {Number} squared length of a
4156 quat
.squaredLength
= vec4
.squaredLength
;
4159 * Alias for {@link quat.squaredLength}
4162 quat
.sqrLen
= quat
.squaredLength
;
4167 * @param {quat} out the receiving quaternion
4168 * @param {quat} a quaternion to normalize
4169 * @returns {quat} out
4172 quat
.normalize
= vec4
.normalize
;
4175 * Creates a quaternion from the given 3x3 rotation matrix.
4177 * NOTE: The resultant quaternion is not normalized, so you should be sure
4178 * to renormalize the quaternion yourself where necessary.
4180 * @param {quat} out the receiving quaternion
4181 * @param {mat3} m rotation matrix
4182 * @returns {quat} out
4185 quat
.fromMat3 = function(out
, m
) {
4186 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
4187 // article "Quaternion Calculus and Fast Animation".
4188 var fTrace
= m
[0] + m
[4] + m
[8];
4191 if ( fTrace
> 0.0 ) {
4192 // |w| > 1/2, may as well choose w > 1/2
4193 fRoot
= Math
.sqrt(fTrace
+ 1.0); // 2w
4194 out
[3] = 0.5 * fRoot
;
4195 fRoot
= 0.5/fRoot; // 1/(4w
)
4196 out
[0] = (m
[7]-m
[5])*fRoot
;
4197 out
[1] = (m
[2]-m
[6])*fRoot
;
4198 out
[2] = (m
[3]-m
[1])*fRoot
;
4204 if ( m
[8] > m
[i
*3+i
] )
4209 fRoot
= Math
.sqrt(m
[i
*3+i
]-m
[j
*3+j
]-m
[k
*3+k
] + 1.0);
4210 out
[i
] = 0.5 * fRoot
;
4211 fRoot
= 0.5 / fRoot
;
4212 out
[3] = (m
[k
*3+j
] - m
[j
*3+k
]) * fRoot
;
4213 out
[j
] = (m
[j
*3+i
] + m
[i
*3+j
]) * fRoot
;
4214 out
[k
] = (m
[k
*3+i
] + m
[i
*3+k
]) * fRoot
;
4221 * Returns a string representation of a quatenion
4223 * @param {quat} vec vector to represent as a string
4224 * @returns {String} string representation of the vector
4226 quat
.str = function (a
) {
4227 return 'quat(' + a
[0] + ', ' + a
[1] + ', ' + a
[2] + ', ' + a
[3] + ')';
4230 if(typeof(exports
) !== 'undefined') {
4231 exports
.quat
= quat
;