mat4.js 56 KB

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