Debian Codegraph @

closure-library/closure/goog/vec/mat4.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Implements 4x4 matrices and their related functions which are
17 * compatible with WebGL. The API is structured to avoid unnecessary memory
18 * allocations. The last parameter will typically be the output vector and
19 * an object can be both an input and output parameter to all methods except
20 * where noted. Matrix operations follow the mathematical form when multiplying
21 * vectors as follows: resultVec = matrix * vec.
22 *
23 * The matrices are stored in column-major order.
24 *
25 */
26 goog.provide('goog.vec.Mat4');
27
28 goog.require('goog.vec');
29 goog.require('goog.vec.Vec3');
30 goog.require('goog.vec.Vec4');
31
32
33 /** @typedef {goog.vec.Float32} */ goog.vec.Mat4.Float32;
34 /** @typedef {goog.vec.Float64} */ goog.vec.Mat4.Float64;
35 /** @typedef {goog.vec.Number} */ goog.vec.Mat4.Number;
36 /** @typedef {goog.vec.AnyType} */ goog.vec.Mat4.AnyType;
37
38 // The following two types are deprecated - use the above types instead.
39 /** @typedef {!Float32Array} */ goog.vec.Mat4.Type;
40 /** @typedef {goog.vec.ArrayType} */ goog.vec.Mat4.Mat4Like;
41
42
43 /**
44 * Creates the array representation of a 4x4 matrix of Float32.
45 * The use of the array directly instead of a class reduces overhead.
46 * The returned matrix is cleared to all zeros.
47 *
48 * @return {!goog.vec.Mat4.Float32} The new matrix.
49 */
50 goog.vec.Mat4.createFloat32 = function() {
51 return new Float32Array(16);
52 };
53
54
55 /**
56 * Creates the array representation of a 4x4 matrix of Float64.
57 * The returned matrix is cleared to all zeros.
58 *
59 * @return {!goog.vec.Mat4.Float64} The new matrix.
60 */
61 goog.vec.Mat4.createFloat64 = function() {
62 return new Float64Array(16);
63 };
64
65
66 /**
67 * Creates the array representation of a 4x4 matrix of Number.
68 * The returned matrix is cleared to all zeros.
69 *
70 * @return {!goog.vec.Mat4.Number} The new matrix.
71 */
72 goog.vec.Mat4.createNumber = function() {
73 var a = new Array(16);
74 goog.vec.Mat4.setFromValues(
75 a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
76 return a;
77 };
78
79
80 /**
81 * Creates the array representation of a 4x4 matrix of Float32.
82 * The returned matrix is cleared to all zeros.
83 *
84 * @deprecated Use createFloat32.
85 * @return {!goog.vec.Mat4.Type} The new matrix.
86 */
87 goog.vec.Mat4.create = function() {
88 return goog.vec.Mat4.createFloat32();
89 };
90
91
92 /**
93 * Creates a 4x4 identity matrix of Float32.
94 *
95 * @return {!goog.vec.Mat4.Float32} The new 16 element array.
96 */
97 goog.vec.Mat4.createFloat32Identity = function() {
98 var mat = goog.vec.Mat4.createFloat32();
99 mat[0] = mat[5] = mat[10] = mat[15] = 1;
100 return mat;
101 };
102
103
104 /**
105 * Creates a 4x4 identity matrix of Float64.
106 *
107 * @return {!goog.vec.Mat4.Float64} The new 16 element array.
108 */
109 goog.vec.Mat4.createFloat64Identity = function() {
110 var mat = goog.vec.Mat4.createFloat64();
111 mat[0] = mat[5] = mat[10] = mat[15] = 1;
112 return mat;
113 };
114
115
116 /**
117 * Creates a 4x4 identity matrix of Number.
118 * The returned matrix is cleared to all zeros.
119 *
120 * @return {!goog.vec.Mat4.Number} The new 16 element array.
121 */
122 goog.vec.Mat4.createNumberIdentity = function() {
123 var a = new Array(16);
124 goog.vec.Mat4.setFromValues(
125 a, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
126 return a;
127 };
128
129
130 /**
131 * Creates the array representation of a 4x4 matrix of Float32.
132 * The returned matrix is cleared to all zeros.
133 *
134 * @deprecated Use createFloat32Identity.
135 * @return {!goog.vec.Mat4.Type} The new 16 element array.
136 */
137 goog.vec.Mat4.createIdentity = function() {
138 return goog.vec.Mat4.createFloat32Identity();
139 };
140
141
142 /**
143 * Creates a 4x4 matrix of Float32 initialized from the given array.
144 *
145 * @param {goog.vec.Mat4.AnyType} matrix The array containing the
146 * matrix values in column major order.
147 * @return {!goog.vec.Mat4.Float32} The new, 16 element array.
148 */
149 goog.vec.Mat4.createFloat32FromArray = function(matrix) {
150 var newMatrix = goog.vec.Mat4.createFloat32();
151 goog.vec.Mat4.setFromArray(newMatrix, matrix);
152 return newMatrix;
153 };
154
155
156 /**
157 * Creates a 4x4 matrix of Float32 initialized from the given values.
158 *
159 * @param {number} v00 The values at (0, 0).
160 * @param {number} v10 The values at (1, 0).
161 * @param {number} v20 The values at (2, 0).
162 * @param {number} v30 The values at (3, 0).
163 * @param {number} v01 The values at (0, 1).
164 * @param {number} v11 The values at (1, 1).
165 * @param {number} v21 The values at (2, 1).
166 * @param {number} v31 The values at (3, 1).
167 * @param {number} v02 The values at (0, 2).
168 * @param {number} v12 The values at (1, 2).
169 * @param {number} v22 The values at (2, 2).
170 * @param {number} v32 The values at (3, 2).
171 * @param {number} v03 The values at (0, 3).
172 * @param {number} v13 The values at (1, 3).
173 * @param {number} v23 The values at (2, 3).
174 * @param {number} v33 The values at (3, 3).
175 * @return {!goog.vec.Mat4.Float32} The new, 16 element array.
176 */
177 goog.vec.Mat4.createFloat32FromValues = function(
178 v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
179 v33) {
180 var newMatrix = goog.vec.Mat4.createFloat32();
181 goog.vec.Mat4.setFromValues(
182 newMatrix, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32,
183 v03, v13, v23, v33);
184 return newMatrix;
185 };
186
187
188 /**
189 * Creates a clone of a 4x4 matrix of Float32.
190 *
191 * @param {goog.vec.Mat4.Float32} matrix The source 4x4 matrix.
192 * @return {!goog.vec.Mat4.Float32} The new 4x4 element matrix.
193 */
194 goog.vec.Mat4.cloneFloat32 = goog.vec.Mat4.createFloat32FromArray;
195
196
197 /**
198 * Creates a 4x4 matrix of Float64 initialized from the given array.
199 *
200 * @param {goog.vec.Mat4.AnyType} matrix The array containing the
201 * matrix values in column major order.
202 * @return {!goog.vec.Mat4.Float64} The new, nine element array.
203 */
204 goog.vec.Mat4.createFloat64FromArray = function(matrix) {
205 var newMatrix = goog.vec.Mat4.createFloat64();
206 goog.vec.Mat4.setFromArray(newMatrix, matrix);
207 return newMatrix;
208 };
209
210
211 /**
212 * Creates a 4x4 matrix of Float64 initialized from the given values.
213 *
214 * @param {number} v00 The values at (0, 0).
215 * @param {number} v10 The values at (1, 0).
216 * @param {number} v20 The values at (2, 0).
217 * @param {number} v30 The values at (3, 0).
218 * @param {number} v01 The values at (0, 1).
219 * @param {number} v11 The values at (1, 1).
220 * @param {number} v21 The values at (2, 1).
221 * @param {number} v31 The values at (3, 1).
222 * @param {number} v02 The values at (0, 2).
223 * @param {number} v12 The values at (1, 2).
224 * @param {number} v22 The values at (2, 2).
225 * @param {number} v32 The values at (3, 2).
226 * @param {number} v03 The values at (0, 3).
227 * @param {number} v13 The values at (1, 3).
228 * @param {number} v23 The values at (2, 3).
229 * @param {number} v33 The values at (3, 3).
230 * @return {!goog.vec.Mat4.Float64} The new, 16 element array.
231 */
232 goog.vec.Mat4.createFloat64FromValues = function(
233 v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
234 v33) {
235 var newMatrix = goog.vec.Mat4.createFloat64();
236 goog.vec.Mat4.setFromValues(
237 newMatrix, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32,
238 v03, v13, v23, v33);
239 return newMatrix;
240 };
241
242
243 /**
244 * Creates a clone of a 4x4 matrix of Float64.
245 *
246 * @param {goog.vec.Mat4.Float64} matrix The source 4x4 matrix.
247 * @return {!goog.vec.Mat4.Float64} The new 4x4 element matrix.
248 */
249 goog.vec.Mat4.cloneFloat64 = goog.vec.Mat4.createFloat64FromArray;
250
251
252 /**
253 * Creates a 4x4 matrix of Float32 initialized from the given array.
254 *
255 * @deprecated Use createFloat32FromArray.
256 * @param {goog.vec.Mat4.Mat4Like} matrix The array containing the
257 * matrix values in column major order.
258 * @return {!goog.vec.Mat4.Type} The new, nine element array.
259 */
260 goog.vec.Mat4.createFromArray = function(matrix) {
261 var newMatrix = goog.vec.Mat4.createFloat32();
262 goog.vec.Mat4.setFromArray(newMatrix, matrix);
263 return newMatrix;
264 };
265
266
267 /**
268 * Creates a 4x4 matrix of Float32 initialized from the given values.
269 *
270 * @deprecated Use createFloat32FromValues.
271 * @param {number} v00 The values at (0, 0).
272 * @param {number} v10 The values at (1, 0).
273 * @param {number} v20 The values at (2, 0).
274 * @param {number} v30 The values at (3, 0).
275 * @param {number} v01 The values at (0, 1).
276 * @param {number} v11 The values at (1, 1).
277 * @param {number} v21 The values at (2, 1).
278 * @param {number} v31 The values at (3, 1).
279 * @param {number} v02 The values at (0, 2).
280 * @param {number} v12 The values at (1, 2).
281 * @param {number} v22 The values at (2, 2).
282 * @param {number} v32 The values at (3, 2).
283 * @param {number} v03 The values at (0, 3).
284 * @param {number} v13 The values at (1, 3).
285 * @param {number} v23 The values at (2, 3).
286 * @param {number} v33 The values at (3, 3).
287 * @return {!goog.vec.Mat4.Type} The new, 16 element array.
288 */
289 goog.vec.Mat4.createFromValues = function(
290 v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
291 v33) {
292 return goog.vec.Mat4.createFloat32FromValues(
293 v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
294 v33);
295 };
296
297
298 /**
299 * Creates a clone of a 4x4 matrix of Float32.
300 *
301 * @deprecated Use cloneFloat32.
302 * @param {goog.vec.Mat4.Mat4Like} matrix The source 4x4 matrix.
303 * @return {!goog.vec.Mat4.Type} The new 4x4 element matrix.
304 */
305 goog.vec.Mat4.clone = goog.vec.Mat4.createFromArray;
306
307
308 /**
309 * Retrieves the element at the requested row and column.
310 *
311 * @param {goog.vec.Mat4.AnyType} mat The matrix containing the
312 * value to retrieve.
313 * @param {number} row The row index.
314 * @param {number} column The column index.
315 * @return {number} The element value at the requested row, column indices.
316 */
317 goog.vec.Mat4.getElement = function(mat, row, column) {
318 return mat[row + column * 4];
319 };
320
321
322 /**
323 * Sets the element at the requested row and column.
324 *
325 * @param {goog.vec.Mat4.AnyType} mat The matrix to set the value on.
326 * @param {number} row The row index.
327 * @param {number} column The column index.
328 * @param {number} value The value to set at the requested row, column.
329 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
330 * chained together.
331 */
332 goog.vec.Mat4.setElement = function(mat, row, column, value) {
333 mat[row + column * 4] = value;
334 return mat;
335 };
336
337
338 /**
339 * Initializes the matrix from the set of values. Note the values supplied are
340 * in column major order.
341 *
342 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the
343 * values.
344 * @param {number} v00 The values at (0, 0).
345 * @param {number} v10 The values at (1, 0).
346 * @param {number} v20 The values at (2, 0).
347 * @param {number} v30 The values at (3, 0).
348 * @param {number} v01 The values at (0, 1).
349 * @param {number} v11 The values at (1, 1).
350 * @param {number} v21 The values at (2, 1).
351 * @param {number} v31 The values at (3, 1).
352 * @param {number} v02 The values at (0, 2).
353 * @param {number} v12 The values at (1, 2).
354 * @param {number} v22 The values at (2, 2).
355 * @param {number} v32 The values at (3, 2).
356 * @param {number} v03 The values at (0, 3).
357 * @param {number} v13 The values at (1, 3).
358 * @param {number} v23 The values at (2, 3).
359 * @param {number} v33 The values at (3, 3).
360 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
361 * chained together.
362 */
363 goog.vec.Mat4.setFromValues = function(
364 mat, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13,
365 v23, v33) {
366 mat[0] = v00;
367 mat[1] = v10;
368 mat[2] = v20;
369 mat[3] = v30;
370 mat[4] = v01;
371 mat[5] = v11;
372 mat[6] = v21;
373 mat[7] = v31;
374 mat[8] = v02;
375 mat[9] = v12;
376 mat[10] = v22;
377 mat[11] = v32;
378 mat[12] = v03;
379 mat[13] = v13;
380 mat[14] = v23;
381 mat[15] = v33;
382 return mat;
383 };
384
385
386 /**
387 * Sets the matrix from the array of values stored in column major order.
388 *
389 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
390 * @param {goog.vec.Mat4.AnyType} values The column major ordered
391 * array of values to store in the matrix.
392 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
393 * chained together.
394 */
395 goog.vec.Mat4.setFromArray = function(mat, values) {
396 mat[0] = values[0];
397 mat[1] = values[1];
398 mat[2] = values[2];
399 mat[3] = values[3];
400 mat[4] = values[4];
401 mat[5] = values[5];
402 mat[6] = values[6];
403 mat[7] = values[7];
404 mat[8] = values[8];
405 mat[9] = values[9];
406 mat[10] = values[10];
407 mat[11] = values[11];
408 mat[12] = values[12];
409 mat[13] = values[13];
410 mat[14] = values[14];
411 mat[15] = values[15];
412 return mat;
413 };
414
415
416 /**
417 * Sets the matrix from the array of values stored in row major order.
418 *
419 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
420 * @param {goog.vec.Mat4.AnyType} values The row major ordered array of
421 * values to store in the matrix.
422 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
423 * chained together.
424 */
425 goog.vec.Mat4.setFromRowMajorArray = function(mat, values) {
426 mat[0] = values[0];
427 mat[1] = values[4];
428 mat[2] = values[8];
429 mat[3] = values[12];
430
431 mat[4] = values[1];
432 mat[5] = values[5];
433 mat[6] = values[9];
434 mat[7] = values[13];
435
436 mat[8] = values[2];
437 mat[9] = values[6];
438 mat[10] = values[10];
439 mat[11] = values[14];
440
441 mat[12] = values[3];
442 mat[13] = values[7];
443 mat[14] = values[11];
444 mat[15] = values[15];
445
446 return mat;
447 };
448
449
450 /**
451 * Sets the diagonal values of the matrix from the given values.
452 *
453 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
454 * @param {number} v00 The values for (0, 0).
455 * @param {number} v11 The values for (1, 1).
456 * @param {number} v22 The values for (2, 2).
457 * @param {number} v33 The values for (3, 3).
458 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
459 * chained together.
460 */
461 goog.vec.Mat4.setDiagonalValues = function(mat, v00, v11, v22, v33) {
462 mat[0] = v00;
463 mat[5] = v11;
464 mat[10] = v22;
465 mat[15] = v33;
466 return mat;
467 };
468
469
470 /**
471 * Sets the diagonal values of the matrix from the given vector.
472 *
473 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
474 * @param {goog.vec.Vec4.AnyType} vec The vector containing the values.
475 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
476 * chained together.
477 */
478 goog.vec.Mat4.setDiagonal = function(mat, vec) {
479 mat[0] = vec[0];
480 mat[5] = vec[1];
481 mat[10] = vec[2];
482 mat[15] = vec[3];
483 return mat;
484 };
485
486
487 /**
488 * Gets the diagonal values of the matrix into the given vector.
489 *
490 * @param {goog.vec.Mat4.AnyType} mat The matrix containing the values.
491 * @param {goog.vec.Vec4.AnyType} vec The vector to receive the values.
492 * @param {number=} opt_diagonal Which diagonal to get. A value of 0 selects the
493 * main diagonal, a positive number selects a super diagonal and a negative
494 * number selects a sub diagonal.
495 * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
496 * chained together.
497 */
498 goog.vec.Mat4.getDiagonal = function(mat, vec, opt_diagonal) {
499 if (!opt_diagonal) {
500 // This is the most common case, so we avoid the for loop.
501 vec[0] = mat[0];
502 vec[1] = mat[5];
503 vec[2] = mat[10];
504 vec[3] = mat[15];
505 } else {
506 var offset = opt_diagonal > 0 ? 4 * opt_diagonal : -opt_diagonal;
507 for (var i = 0; i < 4 - Math.abs(opt_diagonal); i++) {
508 vec[i] = mat[offset + 5 * i];
509 }
510 }
511 return vec;
512 };
513
514
515 /**
516 * Sets the specified column with the supplied values.
517 *
518 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
519 * @param {number} column The column index to set the values on.
520 * @param {number} v0 The value for row 0.
521 * @param {number} v1 The value for row 1.
522 * @param {number} v2 The value for row 2.
523 * @param {number} v3 The value for row 3.
524 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
525 * chained together.
526 */
527 goog.vec.Mat4.setColumnValues = function(mat, column, v0, v1, v2, v3) {
528 var i = column * 4;
529 mat[i] = v0;
530 mat[i + 1] = v1;
531 mat[i + 2] = v2;
532 mat[i + 3] = v3;
533 return mat;
534 };
535
536
537 /**
538 * Sets the specified column with the value from the supplied vector.
539 *
540 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
541 * @param {number} column The column index to set the values on.
542 * @param {goog.vec.Vec4.AnyType} vec The vector of elements for the column.
543 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
544 * chained together.
545 */
546 goog.vec.Mat4.setColumn = function(mat, column, vec) {
547 var i = column * 4;
548 mat[i] = vec[0];
549 mat[i + 1] = vec[1];
550 mat[i + 2] = vec[2];
551 mat[i + 3] = vec[3];
552 return mat;
553 };
554
555
556 /**
557 * Retrieves the specified column from the matrix into the given vector.
558 *
559 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the values.
560 * @param {number} column The column to get the values from.
561 * @param {goog.vec.Vec4.AnyType} vec The vector of elements to
562 * receive the column.
563 * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
564 * chained together.
565 */
566 goog.vec.Mat4.getColumn = function(mat, column, vec) {
567 var i = column * 4;
568 vec[0] = mat[i];
569 vec[1] = mat[i + 1];
570 vec[2] = mat[i + 2];
571 vec[3] = mat[i + 3];
572 return vec;
573 };
574
575
576 /**
577 * Sets the columns of the matrix from the given vectors.
578 *
579 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
580 * @param {goog.vec.Vec4.AnyType} vec0 The values for column 0.
581 * @param {goog.vec.Vec4.AnyType} vec1 The values for column 1.
582 * @param {goog.vec.Vec4.AnyType} vec2 The values for column 2.
583 * @param {goog.vec.Vec4.AnyType} vec3 The values for column 3.
584 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
585 * chained together.
586 */
587 goog.vec.Mat4.setColumns = function(mat, vec0, vec1, vec2, vec3) {
588 goog.vec.Mat4.setColumn(mat, 0, vec0);
589 goog.vec.Mat4.setColumn(mat, 1, vec1);
590 goog.vec.Mat4.setColumn(mat, 2, vec2);
591 goog.vec.Mat4.setColumn(mat, 3, vec3);
592 return mat;
593 };
594
595
596 /**
597 * Retrieves the column values from the given matrix into the given vectors.
598 *
599 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the columns.
600 * @param {goog.vec.Vec4.AnyType} vec0 The vector to receive column 0.
601 * @param {goog.vec.Vec4.AnyType} vec1 The vector to receive column 1.
602 * @param {goog.vec.Vec4.AnyType} vec2 The vector to receive column 2.
603 * @param {goog.vec.Vec4.AnyType} vec3 The vector to receive column 3.
604 */
605 goog.vec.Mat4.getColumns = function(mat, vec0, vec1, vec2, vec3) {
606 goog.vec.Mat4.getColumn(mat, 0, vec0);
607 goog.vec.Mat4.getColumn(mat, 1, vec1);
608 goog.vec.Mat4.getColumn(mat, 2, vec2);
609 goog.vec.Mat4.getColumn(mat, 3, vec3);
610 };
611
612
613 /**
614 * Sets the row values from the supplied values.
615 *
616 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
617 * @param {number} row The index of the row to receive the values.
618 * @param {number} v0 The value for column 0.
619 * @param {number} v1 The value for column 1.
620 * @param {number} v2 The value for column 2.
621 * @param {number} v3 The value for column 3.
622 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
623 * chained together.
624 */
625 goog.vec.Mat4.setRowValues = function(mat, row, v0, v1, v2, v3) {
626 mat[row] = v0;
627 mat[row + 4] = v1;
628 mat[row + 8] = v2;
629 mat[row + 12] = v3;
630 return mat;
631 };
632
633
634 /**
635 * Sets the row values from the supplied vector.
636 *
637 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the row values.
638 * @param {number} row The index of the row.
639 * @param {goog.vec.Vec4.AnyType} vec The vector containing the values.
640 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
641 * chained together.
642 */
643 goog.vec.Mat4.setRow = function(mat, row, vec) {
644 mat[row] = vec[0];
645 mat[row + 4] = vec[1];
646 mat[row + 8] = vec[2];
647 mat[row + 12] = vec[3];
648 return mat;
649 };
650
651
652 /**
653 * Retrieves the row values into the given vector.
654 *
655 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the values.
656 * @param {number} row The index of the row supplying the values.
657 * @param {goog.vec.Vec4.AnyType} vec The vector to receive the row.
658 * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
659 * chained together.
660 */
661 goog.vec.Mat4.getRow = function(mat, row, vec) {
662 vec[0] = mat[row];
663 vec[1] = mat[row + 4];
664 vec[2] = mat[row + 8];
665 vec[3] = mat[row + 12];
666 return vec;
667 };
668
669
670 /**
671 * Sets the rows of the matrix from the supplied vectors.
672 *
673 * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
674 * @param {goog.vec.Vec4.AnyType} vec0 The values for row 0.
675 * @param {goog.vec.Vec4.AnyType} vec1 The values for row 1.
676 * @param {goog.vec.Vec4.AnyType} vec2 The values for row 2.
677 * @param {goog.vec.Vec4.AnyType} vec3 The values for row 3.
678 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
679 * chained together.
680 */
681 goog.vec.Mat4.setRows = function(mat, vec0, vec1, vec2, vec3) {
682 goog.vec.Mat4.setRow(mat, 0, vec0);
683 goog.vec.Mat4.setRow(mat, 1, vec1);
684 goog.vec.Mat4.setRow(mat, 2, vec2);
685 goog.vec.Mat4.setRow(mat, 3, vec3);
686 return mat;
687 };
688
689
690 /**
691 * Retrieves the rows of the matrix into the supplied vectors.
692 *
693 * @param {goog.vec.Mat4.AnyType} mat The matrix to supply the values.
694 * @param {goog.vec.Vec4.AnyType} vec0 The vector to receive row 0.
695 * @param {goog.vec.Vec4.AnyType} vec1 The vector to receive row 1.
696 * @param {goog.vec.Vec4.AnyType} vec2 The vector to receive row 2.
697 * @param {goog.vec.Vec4.AnyType} vec3 The vector to receive row 3.
698 */
699 goog.vec.Mat4.getRows = function(mat, vec0, vec1, vec2, vec3) {
700 goog.vec.Mat4.getRow(mat, 0, vec0);
701 goog.vec.Mat4.getRow(mat, 1, vec1);
702 goog.vec.Mat4.getRow(mat, 2, vec2);
703 goog.vec.Mat4.getRow(mat, 3, vec3);
704 };
705
706
707 /**
708 * Makes the given 4x4 matrix the zero matrix.
709 *
710 * @param {goog.vec.Mat4.AnyType} mat The matrix.
711 * @return {!goog.vec.Mat4.AnyType} return mat so operations can be chained.
712 */
713 goog.vec.Mat4.makeZero = function(mat) {
714 mat[0] = 0;
715 mat[1] = 0;
716 mat[2] = 0;
717 mat[3] = 0;
718 mat[4] = 0;
719 mat[5] = 0;
720 mat[6] = 0;
721 mat[7] = 0;
722 mat[8] = 0;
723 mat[9] = 0;
724 mat[10] = 0;
725 mat[11] = 0;
726 mat[12] = 0;
727 mat[13] = 0;
728 mat[14] = 0;
729 mat[15] = 0;
730 return mat;
731 };
732
733
734 /**
735 * Makes the given 4x4 matrix the identity matrix.
736 *
737 * @param {goog.vec.Mat4.AnyType} mat The matrix.
738 * @return {goog.vec.Mat4.AnyType} return mat so operations can be chained.
739 */
740 goog.vec.Mat4.makeIdentity = function(mat) {
741 mat[0] = 1;
742 mat[1] = 0;
743 mat[2] = 0;
744 mat[3] = 0;
745 mat[4] = 0;
746 mat[5] = 1;
747 mat[6] = 0;
748 mat[7] = 0;
749 mat[8] = 0;
750 mat[9] = 0;
751 mat[10] = 1;
752 mat[11] = 0;
753 mat[12] = 0;
754 mat[13] = 0;
755 mat[14] = 0;
756 mat[15] = 1;
757 return mat;
758 };
759
760
761 /**
762 * Performs a per-component addition of the matrix mat0 and mat1, storing
763 * the result into resultMat.
764 *
765 * @param {goog.vec.Mat4.AnyType} mat0 The first addend.
766 * @param {goog.vec.Mat4.AnyType} mat1 The second addend.
767 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to
768 * receive the results (may be either mat0 or mat1).
769 * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
770 * chained together.
771 */
772 goog.vec.Mat4.addMat = function(mat0, mat1, resultMat) {
773 resultMat[0] = mat0[0] + mat1[0];
774 resultMat[1] = mat0[1] + mat1[1];
775 resultMat[2] = mat0[2] + mat1[2];
776 resultMat[3] = mat0[3] + mat1[3];
777 resultMat[4] = mat0[4] + mat1[4];
778 resultMat[5] = mat0[5] + mat1[5];
779 resultMat[6] = mat0[6] + mat1[6];
780 resultMat[7] = mat0[7] + mat1[7];
781 resultMat[8] = mat0[8] + mat1[8];
782 resultMat[9] = mat0[9] + mat1[9];
783 resultMat[10] = mat0[10] + mat1[10];
784 resultMat[11] = mat0[11] + mat1[11];
785 resultMat[12] = mat0[12] + mat1[12];
786 resultMat[13] = mat0[13] + mat1[13];
787 resultMat[14] = mat0[14] + mat1[14];
788 resultMat[15] = mat0[15] + mat1[15];
789 return resultMat;
790 };
791
792
793 /**
794 * Performs a per-component subtraction of the matrix mat0 and mat1,
795 * storing the result into resultMat.
796 *
797 * @param {goog.vec.Mat4.AnyType} mat0 The minuend.
798 * @param {goog.vec.Mat4.AnyType} mat1 The subtrahend.
799 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
800 * the results (may be either mat0 or mat1).
801 * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
802 * chained together.
803 */
804 goog.vec.Mat4.subMat = function(mat0, mat1, resultMat) {
805 resultMat[0] = mat0[0] - mat1[0];
806 resultMat[1] = mat0[1] - mat1[1];
807 resultMat[2] = mat0[2] - mat1[2];
808 resultMat[3] = mat0[3] - mat1[3];
809 resultMat[4] = mat0[4] - mat1[4];
810 resultMat[5] = mat0[5] - mat1[5];
811 resultMat[6] = mat0[6] - mat1[6];
812 resultMat[7] = mat0[7] - mat1[7];
813 resultMat[8] = mat0[8] - mat1[8];
814 resultMat[9] = mat0[9] - mat1[9];
815 resultMat[10] = mat0[10] - mat1[10];
816 resultMat[11] = mat0[11] - mat1[11];
817 resultMat[12] = mat0[12] - mat1[12];
818 resultMat[13] = mat0[13] - mat1[13];
819 resultMat[14] = mat0[14] - mat1[14];
820 resultMat[15] = mat0[15] - mat1[15];
821 return resultMat;
822 };
823
824
825 /**
826 * Multiplies matrix mat with the given scalar, storing the result
827 * into resultMat.
828 *
829 * @param {goog.vec.Mat4.AnyType} mat The matrix.
830 * @param {number} scalar The scalar value to multiply to each element of mat.
831 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
832 * the results (may be mat).
833 * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
834 * chained together.
835 */
836 goog.vec.Mat4.multScalar = function(mat, scalar, resultMat) {
837 resultMat[0] = mat[0] * scalar;
838 resultMat[1] = mat[1] * scalar;
839 resultMat[2] = mat[2] * scalar;
840 resultMat[3] = mat[3] * scalar;
841 resultMat[4] = mat[4] * scalar;
842 resultMat[5] = mat[5] * scalar;
843 resultMat[6] = mat[6] * scalar;
844 resultMat[7] = mat[7] * scalar;
845 resultMat[8] = mat[8] * scalar;
846 resultMat[9] = mat[9] * scalar;
847 resultMat[10] = mat[10] * scalar;
848 resultMat[11] = mat[11] * scalar;
849 resultMat[12] = mat[12] * scalar;
850 resultMat[13] = mat[13] * scalar;
851 resultMat[14] = mat[14] * scalar;
852 resultMat[15] = mat[15] * scalar;
853 return resultMat;
854 };
855
856
857 /**
858 * Multiplies the two matrices mat0 and mat1 using matrix multiplication,
859 * storing the result into resultMat.
860 *
861 * @param {goog.vec.Mat4.AnyType} mat0 The first (left hand) matrix.
862 * @param {goog.vec.Mat4.AnyType} mat1 The second (right hand) matrix.
863 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
864 * the results (may be either mat0 or mat1).
865 * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
866 * chained together.
867 */
868 goog.vec.Mat4.multMat = function(mat0, mat1, resultMat) {
869 var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2], a30 = mat0[3];
870 var a01 = mat0[4], a11 = mat0[5], a21 = mat0[6], a31 = mat0[7];
871 var a02 = mat0[8], a12 = mat0[9], a22 = mat0[10], a32 = mat0[11];
872 var a03 = mat0[12], a13 = mat0[13], a23 = mat0[14], a33 = mat0[15];
873
874 var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2], b30 = mat1[3];
875 var b01 = mat1[4], b11 = mat1[5], b21 = mat1[6], b31 = mat1[7];
876 var b02 = mat1[8], b12 = mat1[9], b22 = mat1[10], b32 = mat1[11];
877 var b03 = mat1[12], b13 = mat1[13], b23 = mat1[14], b33 = mat1[15];
878
879 resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20 + a03 * b30;
880 resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20 + a13 * b30;
881 resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20 + a23 * b30;
882 resultMat[3] = a30 * b00 + a31 * b10 + a32 * b20 + a33 * b30;
883
884 resultMat[4] = a00 * b01 + a01 * b11 + a02 * b21 + a03 * b31;
885 resultMat[5] = a10 * b01 + a11 * b11 + a12 * b21 + a13 * b31;
886 resultMat[6] = a20 * b01 + a21 * b11 + a22 * b21 + a23 * b31;
887 resultMat[7] = a30 * b01 + a31 * b11 + a32 * b21 + a33 * b31;
888
889 resultMat[8] = a00 * b02 + a01 * b12 + a02 * b22 + a03 * b32;
890 resultMat[9] = a10 * b02 + a11 * b12 + a12 * b22 + a13 * b32;
891 resultMat[10] = a20 * b02 + a21 * b12 + a22 * b22 + a23 * b32;
892 resultMat[11] = a30 * b02 + a31 * b12 + a32 * b22 + a33 * b32;
893
894 resultMat[12] = a00 * b03 + a01 * b13 + a02 * b23 + a03 * b33;
895 resultMat[13] = a10 * b03 + a11 * b13 + a12 * b23 + a13 * b33;
896 resultMat[14] = a20 * b03 + a21 * b13 + a22 * b23 + a23 * b33;
897 resultMat[15] = a30 * b03 + a31 * b13 + a32 * b23 + a33 * b33;
898 return resultMat;
899 };
900
901
902 /**
903 * Transposes the given matrix mat storing the result into resultMat.
904 *
905 * @param {goog.vec.Mat4.AnyType} mat The matrix to transpose.
906 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
907 * the results (may be mat).
908 * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
909 * chained together.
910 */
911 goog.vec.Mat4.transpose = function(mat, resultMat) {
912 if (resultMat == mat) {
913 var a10 = mat[1], a20 = mat[2], a30 = mat[3];
914 var a21 = mat[6], a31 = mat[7];
915 var a32 = mat[11];
916 resultMat[1] = mat[4];
917 resultMat[2] = mat[8];
918 resultMat[3] = mat[12];
919 resultMat[4] = a10;
920 resultMat[6] = mat[9];
921 resultMat[7] = mat[13];
922 resultMat[8] = a20;
923 resultMat[9] = a21;
924 resultMat[11] = mat[14];
925 resultMat[12] = a30;
926 resultMat[13] = a31;
927 resultMat[14] = a32;
928 } else {
929 resultMat[0] = mat[0];
930 resultMat[1] = mat[4];
931 resultMat[2] = mat[8];
932 resultMat[3] = mat[12];
933
934 resultMat[4] = mat[1];
935 resultMat[5] = mat[5];
936 resultMat[6] = mat[9];
937 resultMat[7] = mat[13];
938
939 resultMat[8] = mat[2];
940 resultMat[9] = mat[6];
941 resultMat[10] = mat[10];
942 resultMat[11] = mat[14];
943
944 resultMat[12] = mat[3];
945 resultMat[13] = mat[7];
946 resultMat[14] = mat[11];
947 resultMat[15] = mat[15];
948 }
949 return resultMat;
950 };
951
952
953 /**
954 * Computes the determinant of the matrix.
955 *
956 * @param {goog.vec.Mat4.AnyType} mat The matrix to compute the matrix for.
957 * @return {number} The determinant of the matrix.
958 */
959 goog.vec.Mat4.determinant = function(mat) {
960 var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
961 var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
962 var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
963 var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
964
965 var a0 = m00 * m11 - m10 * m01;
966 var a1 = m00 * m21 - m20 * m01;
967 var a2 = m00 * m31 - m30 * m01;
968 var a3 = m10 * m21 - m20 * m11;
969 var a4 = m10 * m31 - m30 * m11;
970 var a5 = m20 * m31 - m30 * m21;
971 var b0 = m02 * m13 - m12 * m03;
972 var b1 = m02 * m23 - m22 * m03;
973 var b2 = m02 * m33 - m32 * m03;
974 var b3 = m12 * m23 - m22 * m13;
975 var b4 = m12 * m33 - m32 * m13;
976 var b5 = m22 * m33 - m32 * m23;
977
978 return a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
979 };
980
981
982 /**
983 * Computes the inverse of mat storing the result into resultMat. If the
984 * inverse is defined, this function returns true, false otherwise.
985 *
986 * @param {goog.vec.Mat4.AnyType} mat The matrix to invert.
987 * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
988 * the result (may be mat).
989 * @return {boolean} True if the inverse is defined. If false is returned,
990 * resultMat is not modified.
991 */
992 goog.vec.Mat4.invert = function(mat, resultMat) {
993 var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
994 var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
995 var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
996 var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
997
998 var a0 = m00 * m11 - m10 * m01;
999 var a1 = m00 * m21 - m20 * m01;
1000 var a2 = m00 * m31 - m30 * m01;
1001 var a3 = m10 * m21 - m20 * m11;
1002 var a4 = m10 * m31 - m30 * m11;
1003 var a5 = m20 * m31 - m30 * m21;
1004 var b0 = m02 * m13 - m12 * m03;
1005 var b1 = m02 * m23 - m22 * m03;
1006 var b2 = m02 * m33 - m32 * m03;
1007 var b3 = m12 * m23 - m22 * m13;
1008 var b4 = m12 * m33 - m32 * m13;
1009 var b5 = m22 * m33 - m32 * m23;
1010
1011 var det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
1012 if (det == 0) {
1013 return false;
1014 }
1015
1016 var idet = 1.0 / det;
1017 resultMat[0] = (m11 * b5 - m21 * b4 + m31 * b3) * idet;
1018 resultMat[1] = (-m10 * b5 + m20 * b4 - m30 * b3) * idet;
1019 resultMat[2] = (m13 * a5 - m23 * a4 + m33 * a3) * idet;
1020 resultMat[3] = (-m12 * a5 + m22 * a4 - m32 * a3) * idet;
1021 resultMat[4] = (-m01 * b5 + m21 * b2 - m31 * b1) * idet;
1022 resultMat[5] = (m00 * b5 - m20 * b2 + m30 * b1) * idet;
1023 resultMat[6] = (-m03 * a5 + m23 * a2 - m33 * a1) * idet;
1024 resultMat[7] = (m02 * a5 - m22 * a2 + m32 * a1) * idet;
1025 resultMat[8] = (m01 * b4 - m11 * b2 + m31 * b0) * idet;
1026 resultMat[9] = (-m00 * b4 + m10 * b2 - m30 * b0) * idet;
1027 resultMat[10] = (m03 * a4 - m13 * a2 + m33 * a0) * idet;
1028 resultMat[11] = (-m02 * a4 + m12 * a2 - m32 * a0) * idet;
1029 resultMat[12] = (-m01 * b3 + m11 * b1 - m21 * b0) * idet;
1030 resultMat[13] = (m00 * b3 - m10 * b1 + m20 * b0) * idet;
1031 resultMat[14] = (-m03 * a3 + m13 * a1 - m23 * a0) * idet;
1032 resultMat[15] = (m02 * a3 - m12 * a1 + m22 * a0) * idet;
1033 return true;
1034 };
1035
1036
1037 /**
1038 * Returns true if the components of mat0 are equal to the components of mat1.
1039 *
1040 * @param {goog.vec.Mat4.AnyType} mat0 The first matrix.
1041 * @param {goog.vec.Mat4.AnyType} mat1 The second matrix.
1042 * @return {boolean} True if the the two matrices are equivalent.
1043 */
1044 goog.vec.Mat4.equals = function(mat0, mat1) {
1045 return mat0.length == mat1.length && mat0[0] == mat1[0] &&
1046 mat0[1] == mat1[1] && mat0[2] == mat1[2] && mat0[3] == mat1[3] &&
1047 mat0[4] == mat1[4] && mat0[5] == mat1[5] && mat0[6] == mat1[6] &&
1048 mat0[7] == mat1[7] && mat0[8] == mat1[8] && mat0[9] == mat1[9] &&
1049 mat0[10] == mat1[10] && mat0[11] == mat1[11] && mat0[12] == mat1[12] &&
1050 mat0[13] == mat1[13] && mat0[14] == mat1[14] && mat0[15] == mat1[15];
1051 };
1052
1053
1054 /**
1055 * Transforms the given vector with the given matrix storing the resulting,
1056 * transformed vector into resultVec. The input vector is multiplied against the
1057 * upper 3x4 matrix omitting the projective component.
1058 *
1059 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
1060 * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
1061 * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector to
1062 * receive the results (may be vec).
1063 * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
1064 * chained together.
1065 */
1066 goog.vec.Mat4.multVec3 = function(mat, vec, resultVec) {
1067 var x = vec[0], y = vec[1], z = vec[2];
1068 resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8] + mat[12];
1069 resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9] + mat[13];
1070 resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10] + mat[14];
1071 return resultVec;
1072 };
1073
1074
1075 /**
1076 * Transforms the given vector with the given matrix storing the resulting,
1077 * transformed vector into resultVec. The input vector is multiplied against the
1078 * upper 3x3 matrix omitting the projective component and translation
1079 * components.
1080 *
1081 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
1082 * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
1083 * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector to
1084 * receive the results (may be vec).
1085 * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
1086 * chained together.
1087 */
1088 goog.vec.Mat4.multVec3NoTranslate = function(mat, vec, resultVec) {
1089 var x = vec[0], y = vec[1], z = vec[2];
1090 resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8];
1091 resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9];
1092 resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10];
1093 return resultVec;
1094 };
1095
1096
1097 /**
1098 * Transforms the given vector with the given matrix storing the resulting,
1099 * transformed vector into resultVec. The input vector is multiplied against the
1100 * full 4x4 matrix with the homogeneous divide applied to reduce the 4 element
1101 * vector to a 3 element vector.
1102 *
1103 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
1104 * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
1105 * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector
1106 * to receive the results (may be vec).
1107 * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
1108 * chained together.
1109 */
1110 goog.vec.Mat4.multVec3Projective = function(mat, vec, resultVec) {
1111 var x = vec[0], y = vec[1], z = vec[2];
1112 var invw = 1 / (x * mat[3] + y * mat[7] + z * mat[11] + mat[15]);
1113 resultVec[0] = (x * mat[0] + y * mat[4] + z * mat[8] + mat[12]) * invw;
1114 resultVec[1] = (x * mat[1] + y * mat[5] + z * mat[9] + mat[13]) * invw;
1115 resultVec[2] = (x * mat[2] + y * mat[6] + z * mat[10] + mat[14]) * invw;
1116 return resultVec;
1117 };
1118
1119
1120 /**
1121 * Transforms the given vector with the given matrix storing the resulting,
1122 * transformed vector into resultVec.
1123 *
1124 * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
1125 * @param {goog.vec.Vec4.AnyType} vec The vector to transform.
1126 * @param {goog.vec.Vec4.AnyType} resultVec The vector to
1127 * receive the results (may be vec).
1128 * @return {goog.vec.Vec4.AnyType} return resultVec so that operations can be
1129 * chained together.
1130 */
1131 goog.vec.Mat4.multVec4 = function(mat, vec, resultVec) {
1132 var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
1133 resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8] + w * mat[12];
1134 resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9] + w * mat[13];
1135 resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10] + w * mat[14];
1136 resultVec[3] = x * mat[3] + y * mat[7] + z * mat[11] + w * mat[15];
1137 return resultVec;
1138 };
1139
1140
1141 /**
1142 * Makes the given 4x4 matrix a translation matrix with x, y and z
1143 * translation factors.
1144 *
1145 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1146 * @param {number} x The translation along the x axis.
1147 * @param {number} y The translation along the y axis.
1148 * @param {number} z The translation along the z axis.
1149 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1150 * chained.
1151 */
1152 goog.vec.Mat4.makeTranslate = function(mat, x, y, z) {
1153 goog.vec.Mat4.makeIdentity(mat);
1154 return goog.vec.Mat4.setColumnValues(mat, 3, x, y, z, 1);
1155 };
1156
1157
1158 /**
1159 * Makes the given 4x4 matrix as a scale matrix with x, y and z scale factors.
1160 *
1161 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1162 * @param {number} x The scale along the x axis.
1163 * @param {number} y The scale along the y axis.
1164 * @param {number} z The scale along the z axis.
1165 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1166 * chained.
1167 */
1168 goog.vec.Mat4.makeScale = function(mat, x, y, z) {
1169 goog.vec.Mat4.makeIdentity(mat);
1170 return goog.vec.Mat4.setDiagonalValues(mat, x, y, z, 1);
1171 };
1172
1173
1174 /**
1175 * Makes the given 4x4 matrix a rotation matrix with the given rotation
1176 * angle about the axis defined by the vector (ax, ay, az).
1177 *
1178 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1179 * @param {number} angle The rotation angle in radians.
1180 * @param {number} ax The x component of the rotation axis.
1181 * @param {number} ay The y component of the rotation axis.
1182 * @param {number} az The z component of the rotation axis.
1183 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1184 * chained.
1185 */
1186 goog.vec.Mat4.makeRotate = function(mat, angle, ax, ay, az) {
1187 var c = Math.cos(angle);
1188 var d = 1 - c;
1189 var s = Math.sin(angle);
1190
1191 return goog.vec.Mat4.setFromValues(
1192 mat, ax * ax * d + c, ax * ay * d + az * s, ax * az * d - ay * s, 0,
1193
1194 ax * ay * d - az * s, ay * ay * d + c, ay * az * d + ax * s, 0,
1195
1196 ax * az * d + ay * s, ay * az * d - ax * s, az * az * d + c, 0,
1197
1198 0, 0, 0, 1);
1199 };
1200
1201
1202 /**
1203 * Makes the given 4x4 matrix a rotation matrix with the given rotation
1204 * angle about the X axis.
1205 *
1206 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1207 * @param {number} angle The rotation angle in radians.
1208 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1209 * chained.
1210 */
1211 goog.vec.Mat4.makeRotateX = function(mat, angle) {
1212 var c = Math.cos(angle);
1213 var s = Math.sin(angle);
1214 return goog.vec.Mat4.setFromValues(
1215 mat, 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1);
1216 };
1217
1218
1219 /**
1220 * Makes the given 4x4 matrix a rotation matrix with the given rotation
1221 * angle about the Y axis.
1222 *
1223 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1224 * @param {number} angle The rotation angle in radians.
1225 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1226 * chained.
1227 */
1228 goog.vec.Mat4.makeRotateY = function(mat, angle) {
1229 var c = Math.cos(angle);
1230 var s = Math.sin(angle);
1231 return goog.vec.Mat4.setFromValues(
1232 mat, c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1);
1233 };
1234
1235
1236 /**
1237 * Makes the given 4x4 matrix a rotation matrix with the given rotation
1238 * angle about the Z axis.
1239 *
1240 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1241 * @param {number} angle The rotation angle in radians.
1242 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1243 * chained.
1244 */
1245 goog.vec.Mat4.makeRotateZ = function(mat, angle) {
1246 var c = Math.cos(angle);
1247 var s = Math.sin(angle);
1248 return goog.vec.Mat4.setFromValues(
1249 mat, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
1250 };
1251
1252
1253 /**
1254 * Makes the given 4x4 matrix a perspective projection matrix.
1255 *
1256 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1257 * @param {number} left The coordinate of the left clipping plane.
1258 * @param {number} right The coordinate of the right clipping plane.
1259 * @param {number} bottom The coordinate of the bottom clipping plane.
1260 * @param {number} top The coordinate of the top clipping plane.
1261 * @param {number} near The distance to the near clipping plane.
1262 * @param {number} far The distance to the far clipping plane.
1263 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1264 * chained.
1265 */
1266 goog.vec.Mat4.makeFrustum = function(mat, left, right, bottom, top, near, far) {
1267 var x = (2 * near) / (right - left);
1268 var y = (2 * near) / (top - bottom);
1269 var a = (right + left) / (right - left);
1270 var b = (top + bottom) / (top - bottom);
1271 var c = -(far + near) / (far - near);
1272 var d = -(2 * far * near) / (far - near);
1273
1274 return goog.vec.Mat4.setFromValues(
1275 mat, x, 0, 0, 0, 0, y, 0, 0, a, b, c, -1, 0, 0, d, 0);
1276 };
1277
1278
1279 /**
1280 * Makes the given 4x4 matrix perspective projection matrix given a
1281 * field of view and aspect ratio.
1282 *
1283 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1284 * @param {number} fovy The field of view along the y (vertical) axis in
1285 * radians.
1286 * @param {number} aspect The x (width) to y (height) aspect ratio.
1287 * @param {number} near The distance to the near clipping plane.
1288 * @param {number} far The distance to the far clipping plane.
1289 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1290 * chained.
1291 */
1292 goog.vec.Mat4.makePerspective = function(mat, fovy, aspect, near, far) {
1293 var angle = fovy / 2;
1294 var dz = far - near;
1295 var sinAngle = Math.sin(angle);
1296 if (dz == 0 || sinAngle == 0 || aspect == 0) {
1297 return mat;
1298 }
1299
1300 var cot = Math.cos(angle) / sinAngle;
1301 return goog.vec.Mat4.setFromValues(
1302 mat, cot / aspect, 0, 0, 0, 0, cot, 0, 0, 0, 0, -(far + near) / dz, -1, 0,
1303 0, -(2 * near * far) / dz, 0);
1304 };
1305
1306
1307 /**
1308 * Makes the given 4x4 matrix an orthographic projection matrix.
1309 *
1310 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1311 * @param {number} left The coordinate of the left clipping plane.
1312 * @param {number} right The coordinate of the right clipping plane.
1313 * @param {number} bottom The coordinate of the bottom clipping plane.
1314 * @param {number} top The coordinate of the top clipping plane.
1315 * @param {number} near The distance to the near clipping plane.
1316 * @param {number} far The distance to the far clipping plane.
1317 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1318 * chained.
1319 */
1320 goog.vec.Mat4.makeOrtho = function(mat, left, right, bottom, top, near, far) {
1321 var x = 2 / (right - left);
1322 var y = 2 / (top - bottom);
1323 var z = -2 / (far - near);
1324 var a = -(right + left) / (right - left);
1325 var b = -(top + bottom) / (top - bottom);
1326 var c = -(far + near) / (far - near);
1327
1328 return goog.vec.Mat4.setFromValues(
1329 mat, x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, a, b, c, 1);
1330 };
1331
1332
1333 /**
1334 * Makes the given 4x4 matrix a modelview matrix of a camera so that
1335 * the camera is 'looking at' the given center point.
1336 *
1337 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1338 * @param {goog.vec.Vec3.AnyType} eyePt The position of the eye point
1339 * (camera origin).
1340 * @param {goog.vec.Vec3.AnyType} centerPt The point to aim the camera at.
1341 * @param {goog.vec.Vec3.AnyType} worldUpVec The vector that identifies
1342 * the up direction for the camera.
1343 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1344 * chained.
1345 */
1346 goog.vec.Mat4.makeLookAt = function(mat, eyePt, centerPt, worldUpVec) {
1347 // Compute the direction vector from the eye point to the center point and
1348 // normalize.
1349 var fwdVec = goog.vec.Mat4.tmpVec4_[0];
1350 goog.vec.Vec3.subtract(centerPt, eyePt, fwdVec);
1351 goog.vec.Vec3.normalize(fwdVec, fwdVec);
1352 fwdVec[3] = 0;
1353
1354 // Compute the side vector from the forward vector and the input up vector.
1355 var sideVec = goog.vec.Mat4.tmpVec4_[1];
1356 goog.vec.Vec3.cross(fwdVec, worldUpVec, sideVec);
1357 goog.vec.Vec3.normalize(sideVec, sideVec);
1358 sideVec[3] = 0;
1359
1360 // Now the up vector to form the orthonormal basis.
1361 var upVec = goog.vec.Mat4.tmpVec4_[2];
1362 goog.vec.Vec3.cross(sideVec, fwdVec, upVec);
1363 goog.vec.Vec3.normalize(upVec, upVec);
1364 upVec[3] = 0;
1365
1366 // Update the view matrix with the new orthonormal basis and position the
1367 // camera at the given eye point.
1368 goog.vec.Vec3.negate(fwdVec, fwdVec);
1369 goog.vec.Mat4.setRow(mat, 0, sideVec);
1370 goog.vec.Mat4.setRow(mat, 1, upVec);
1371 goog.vec.Mat4.setRow(mat, 2, fwdVec);
1372 goog.vec.Mat4.setRowValues(mat, 3, 0, 0, 0, 1);
1373 goog.vec.Mat4.translate(mat, -eyePt[0], -eyePt[1], -eyePt[2]);
1374
1375 return mat;
1376 };
1377
1378
1379 /**
1380 * Decomposes a matrix into the lookAt vectors eyePt, fwdVec and worldUpVec.
1381 * The matrix represents the modelview matrix of a camera. It is the inverse
1382 * of lookAt except for the output of the fwdVec instead of centerPt.
1383 * The centerPt itself cannot be recovered from a modelview matrix.
1384 *
1385 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1386 * @param {goog.vec.Vec3.AnyType} eyePt The position of the eye point
1387 * (camera origin).
1388 * @param {goog.vec.Vec3.AnyType} fwdVec The vector describing where
1389 * the camera points to.
1390 * @param {goog.vec.Vec3.AnyType} worldUpVec The vector that
1391 * identifies the up direction for the camera.
1392 * @return {boolean} True if the method succeeds, false otherwise.
1393 * The method can only fail if the inverse of viewMatrix is not defined.
1394 */
1395 goog.vec.Mat4.toLookAt = function(mat, eyePt, fwdVec, worldUpVec) {
1396 // Get eye of the camera.
1397 var matInverse = goog.vec.Mat4.tmpMat4_[0];
1398 if (!goog.vec.Mat4.invert(mat, matInverse)) {
1399 // The input matrix does not have a valid inverse.
1400 return false;
1401 }
1402
1403 if (eyePt) {
1404 eyePt[0] = matInverse[12];
1405 eyePt[1] = matInverse[13];
1406 eyePt[2] = matInverse[14];
1407 }
1408
1409 // Get forward vector from the definition of lookAt.
1410 if (fwdVec || worldUpVec) {
1411 if (!fwdVec) {
1412 fwdVec = goog.vec.Mat4.tmpVec3_[0];
1413 }
1414 fwdVec[0] = -mat[2];
1415 fwdVec[1] = -mat[6];
1416 fwdVec[2] = -mat[10];
1417 // Normalize forward vector.
1418 goog.vec.Vec3.normalize(fwdVec, fwdVec);
1419 }
1420
1421 if (worldUpVec) {
1422 // Get side vector from the definition of gluLookAt.
1423 var side = goog.vec.Mat4.tmpVec3_[1];
1424 side[0] = mat[0];
1425 side[1] = mat[4];
1426 side[2] = mat[8];
1427 // Compute up vector as a up = side x forward.
1428 goog.vec.Vec3.cross(side, fwdVec, worldUpVec);
1429 // Normalize up vector.
1430 goog.vec.Vec3.normalize(worldUpVec, worldUpVec);
1431 }
1432 return true;
1433 };
1434
1435
1436 /**
1437 * Makes the given 4x4 matrix a rotation matrix given Euler angles using
1438 * the ZXZ convention.
1439 * Given the euler angles [theta1, theta2, theta3], the rotation is defined as
1440 * rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
1441 * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
1442 * rotation_x(theta) means rotation around the X axis of theta radians,
1443 *
1444 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1445 * @param {number} theta1 The angle of rotation around the Z axis in radians.
1446 * @param {number} theta2 The angle of rotation around the X axis in radians.
1447 * @param {number} theta3 The angle of rotation around the Z axis in radians.
1448 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1449 * chained.
1450 */
1451 goog.vec.Mat4.makeEulerZXZ = function(mat, theta1, theta2, theta3) {
1452 var c1 = Math.cos(theta1);
1453 var s1 = Math.sin(theta1);
1454
1455 var c2 = Math.cos(theta2);
1456 var s2 = Math.sin(theta2);
1457
1458 var c3 = Math.cos(theta3);
1459 var s3 = Math.sin(theta3);
1460
1461 mat[0] = c1 * c3 - c2 * s1 * s3;
1462 mat[1] = c2 * c1 * s3 + c3 * s1;
1463 mat[2] = s3 * s2;
1464 mat[3] = 0;
1465
1466 mat[4] = -c1 * s3 - c3 * c2 * s1;
1467 mat[5] = c1 * c2 * c3 - s1 * s3;
1468 mat[6] = c3 * s2;
1469 mat[7] = 0;
1470
1471 mat[8] = s2 * s1;
1472 mat[9] = -c1 * s2;
1473 mat[10] = c2;
1474 mat[11] = 0;
1475
1476 mat[12] = 0;
1477 mat[13] = 0;
1478 mat[14] = 0;
1479 mat[15] = 1;
1480
1481 return mat;
1482 };
1483
1484
1485 /**
1486 * Decomposes a rotation matrix into Euler angles using the ZXZ convention so
1487 * that rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
1488 * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
1489 * rotation_x(theta) means rotation around the X axis of theta radians.
1490 *
1491 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1492 * @param {goog.vec.Vec3.AnyType} euler The ZXZ Euler angles in
1493 * radians as [theta1, theta2, theta3].
1494 * @param {boolean=} opt_theta2IsNegative Whether theta2 is in [-pi, 0] instead
1495 * of the default [0, pi].
1496 * @return {goog.vec.Vec4.AnyType} return euler so that operations can be
1497 * chained together.
1498 */
1499 goog.vec.Mat4.toEulerZXZ = function(mat, euler, opt_theta2IsNegative) {
1500 // There is an ambiguity in the sign of sinTheta2 because of the sqrt.
1501 var sinTheta2 = Math.sqrt(mat[2] * mat[2] + mat[6] * mat[6]);
1502
1503 // By default we explicitely constrain theta2 to be in [0, pi],
1504 // so sinTheta2 is always positive. We can change the behavior and specify
1505 // theta2 to be negative in [-pi, 0] with opt_Theta2IsNegative.
1506 var signTheta2 = opt_theta2IsNegative ? -1 : 1;
1507
1508 if (sinTheta2 > goog.vec.EPSILON) {
1509 euler[2] = Math.atan2(mat[2] * signTheta2, mat[6] * signTheta2);
1510 euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[10]);
1511 euler[0] = Math.atan2(mat[8] * signTheta2, -mat[9] * signTheta2);
1512 } else {
1513 // There is also an arbitrary choice for theta1 = 0 or theta2 = 0 here.
1514 // We assume theta1 = 0 as some applications do not allow the camera to roll
1515 // (i.e. have theta1 != 0).
1516 euler[0] = 0;
1517 euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[10]);
1518 euler[2] = Math.atan2(mat[1], mat[0]);
1519 }
1520
1521 // Atan2 outputs angles in [-pi, pi] so we bring them back to [0, 2 * pi].
1522 euler[0] = (euler[0] + Math.PI * 2) % (Math.PI * 2);
1523 euler[2] = (euler[2] + Math.PI * 2) % (Math.PI * 2);
1524 // For theta2 we want the angle to be in [0, pi] or [-pi, 0] depending on
1525 // signTheta2.
1526 euler[1] =
1527 ((euler[1] * signTheta2 + Math.PI * 2) % (Math.PI * 2)) * signTheta2;
1528
1529 return euler;
1530 };
1531
1532
1533 /**
1534 * Translates the given matrix by x,y,z. Equvialent to:
1535 * goog.vec.Mat4.multMat(
1536 * mat,
1537 * goog.vec.Mat4.makeTranslate(goog.vec.Mat4.create(), x, y, z),
1538 * mat);
1539 *
1540 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1541 * @param {number} x The translation along the x axis.
1542 * @param {number} y The translation along the y axis.
1543 * @param {number} z The translation along the z axis.
1544 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1545 * chained.
1546 */
1547 goog.vec.Mat4.translate = function(mat, x, y, z) {
1548 return goog.vec.Mat4.setColumnValues(
1549 mat, 3, mat[0] * x + mat[4] * y + mat[8] * z + mat[12],
1550 mat[1] * x + mat[5] * y + mat[9] * z + mat[13],
1551 mat[2] * x + mat[6] * y + mat[10] * z + mat[14],
1552 mat[3] * x + mat[7] * y + mat[11] * z + mat[15]);
1553 };
1554
1555
1556 /**
1557 * Scales the given matrix by x,y,z. Equivalent to:
1558 * goog.vec.Mat4.multMat(
1559 * mat,
1560 * goog.vec.Mat4.makeScale(goog.vec.Mat4.create(), x, y, z),
1561 * mat);
1562 *
1563 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1564 * @param {number} x The x scale factor.
1565 * @param {number} y The y scale factor.
1566 * @param {number} z The z scale factor.
1567 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1568 * chained.
1569 */
1570 goog.vec.Mat4.scale = function(mat, x, y, z) {
1571 return goog.vec.Mat4.setFromValues(
1572 mat, mat[0] * x, mat[1] * x, mat[2] * x, mat[3] * x, mat[4] * y,
1573 mat[5] * y, mat[6] * y, mat[7] * y, mat[8] * z, mat[9] * z, mat[10] * z,
1574 mat[11] * z, mat[12], mat[13], mat[14], mat[15]);
1575 };
1576
1577
1578 /**
1579 * Rotate the given matrix by angle about the x,y,z axis. Equivalent to:
1580 * goog.vec.Mat4.multMat(
1581 * mat,
1582 * goog.vec.Mat4.makeRotate(goog.vec.Mat4.create(), angle, x, y, z),
1583 * mat);
1584 *
1585 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1586 * @param {number} angle The angle in radians.
1587 * @param {number} x The x component of the rotation axis.
1588 * @param {number} y The y component of the rotation axis.
1589 * @param {number} z The z component of the rotation axis.
1590 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1591 * chained.
1592 */
1593 goog.vec.Mat4.rotate = function(mat, angle, x, y, z) {
1594 var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
1595 var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
1596 var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
1597 var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
1598
1599 var cosAngle = Math.cos(angle);
1600 var sinAngle = Math.sin(angle);
1601 var diffCosAngle = 1 - cosAngle;
1602 var r00 = x * x * diffCosAngle + cosAngle;
1603 var r10 = x * y * diffCosAngle + z * sinAngle;
1604 var r20 = x * z * diffCosAngle - y * sinAngle;
1605
1606 var r01 = x * y * diffCosAngle - z * sinAngle;
1607 var r11 = y * y * diffCosAngle + cosAngle;
1608 var r21 = y * z * diffCosAngle + x * sinAngle;
1609
1610 var r02 = x * z * diffCosAngle + y * sinAngle;
1611 var r12 = y * z * diffCosAngle - x * sinAngle;
1612 var r22 = z * z * diffCosAngle + cosAngle;
1613
1614 return goog.vec.Mat4.setFromValues(
1615 mat, m00 * r00 + m01 * r10 + m02 * r20, m10 * r00 + m11 * r10 + m12 * r20,
1616 m20 * r00 + m21 * r10 + m22 * r20, m30 * r00 + m31 * r10 + m32 * r20,
1617
1618 m00 * r01 + m01 * r11 + m02 * r21, m10 * r01 + m11 * r11 + m12 * r21,
1619 m20 * r01 + m21 * r11 + m22 * r21, m30 * r01 + m31 * r11 + m32 * r21,
1620
1621 m00 * r02 + m01 * r12 + m02 * r22, m10 * r02 + m11 * r12 + m12 * r22,
1622 m20 * r02 + m21 * r12 + m22 * r22, m30 * r02 + m31 * r12 + m32 * r22,
1623
1624 m03, m13, m23, m33);
1625 };
1626
1627
1628 /**
1629 * Rotate the given matrix by angle about the x axis. Equivalent to:
1630 * goog.vec.Mat4.multMat(
1631 * mat,
1632 * goog.vec.Mat4.makeRotateX(goog.vec.Mat4.create(), angle),
1633 * mat);
1634 *
1635 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1636 * @param {number} angle The angle in radians.
1637 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1638 * chained.
1639 */
1640 goog.vec.Mat4.rotateX = function(mat, angle) {
1641 var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
1642 var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
1643
1644 var c = Math.cos(angle);
1645 var s = Math.sin(angle);
1646
1647 mat[4] = m01 * c + m02 * s;
1648 mat[5] = m11 * c + m12 * s;
1649 mat[6] = m21 * c + m22 * s;
1650 mat[7] = m31 * c + m32 * s;
1651 mat[8] = m01 * -s + m02 * c;
1652 mat[9] = m11 * -s + m12 * c;
1653 mat[10] = m21 * -s + m22 * c;
1654 mat[11] = m31 * -s + m32 * c;
1655
1656 return mat;
1657 };
1658
1659
1660 /**
1661 * Rotate the given matrix by angle about the y axis. Equivalent to:
1662 * goog.vec.Mat4.multMat(
1663 * mat,
1664 * goog.vec.Mat4.makeRotateY(goog.vec.Mat4.create(), angle),
1665 * mat);
1666 *
1667 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1668 * @param {number} angle The angle in radians.
1669 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1670 * chained.
1671 */
1672 goog.vec.Mat4.rotateY = function(mat, angle) {
1673 var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
1674 var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
1675
1676 var c = Math.cos(angle);
1677 var s = Math.sin(angle);
1678
1679 mat[0] = m00 * c + m02 * -s;
1680 mat[1] = m10 * c + m12 * -s;
1681 mat[2] = m20 * c + m22 * -s;
1682 mat[3] = m30 * c + m32 * -s;
1683 mat[8] = m00 * s + m02 * c;
1684 mat[9] = m10 * s + m12 * c;
1685 mat[10] = m20 * s + m22 * c;
1686 mat[11] = m30 * s + m32 * c;
1687
1688 return mat;
1689 };
1690
1691
1692 /**
1693 * Rotate the given matrix by angle about the z axis. Equivalent to:
1694 * goog.vec.Mat4.multMat(
1695 * mat,
1696 * goog.vec.Mat4.makeRotateZ(goog.vec.Mat4.create(), angle),
1697 * mat);
1698 *
1699 * @param {goog.vec.Mat4.AnyType} mat The matrix.
1700 * @param {number} angle The angle in radians.
1701 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1702 * chained.
1703 */
1704 goog.vec.Mat4.rotateZ = function(mat, angle) {
1705 var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
1706 var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
1707
1708 var c = Math.cos(angle);
1709 var s = Math.sin(angle);
1710
1711 mat[0] = m00 * c + m01 * s;
1712 mat[1] = m10 * c + m11 * s;
1713 mat[2] = m20 * c + m21 * s;
1714 mat[3] = m30 * c + m31 * s;
1715 mat[4] = m00 * -s + m01 * c;
1716 mat[5] = m10 * -s + m11 * c;
1717 mat[6] = m20 * -s + m21 * c;
1718 mat[7] = m30 * -s + m31 * c;
1719
1720 return mat;
1721 };
1722
1723
1724 /**
1725 * Retrieves the translation component of the transformation matrix.
1726 *
1727 * @param {goog.vec.Mat4.AnyType} mat The transformation matrix.
1728 * @param {goog.vec.Vec3.AnyType} translation The vector for storing the
1729 * result.
1730 * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
1731 * chained.
1732 */
1733 goog.vec.Mat4.getTranslation = function(mat, translation) {
1734 translation[0] = mat[12];
1735 translation[1] = mat[13];
1736 translation[2] = mat[14];
1737 return translation;
1738 };
1739
1740
1741 /**
1742 * @type {!Array<!goog.vec.Mat4.Float64>}
1743 * @private
1744 */
1745 goog.vec.Mat4.tmpVec3_ =
1746 [goog.vec.Vec3.createFloat64(), goog.vec.Vec3.createFloat64()];
1747
1748
1749 /**
1750 * @type {!Array<!goog.vec.Mat4.Float64>}
1751 * @private
1752 */
1753 goog.vec.Mat4.tmpVec4_ = [
1754 goog.vec.Vec4.createFloat64(), goog.vec.Vec4.createFloat64(),
1755 goog.vec.Vec4.createFloat64()
1756 ];
1757
1758
1759 /**
1760 * @type {!Array<!goog.vec.Mat4.Float64>}
1761 * @private
1762 */
1763 goog.vec.Mat4.tmpMat4_ = [goog.vec.Mat4.createFloat64()];