mat4f.js 57 KB

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