Debian Codegraph @

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

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