matrix4.js 44 KB

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