mat4.js 51 KB

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