PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/glMatrix.js

https://code.google.com/p/glmatrix/
JavaScript | 1834 lines | 957 code | 195 blank | 682 comment | 100 complexity | fae353e312591a653f90c790ddb60645 MD5 | raw file
  1. /*
  2. * glMatrix.js - High performance matrix and vector operations for WebGL
  3. * version 0.9.6
  4. */
  5. /*
  6. * Copyright (c) 2011 Brandon Jones
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgment in the product documentation would be
  19. * appreciated but is not required.
  20. *
  21. * 2. Altered source versions must be plainly marked as such, and must not
  22. * be misrepresented as being the original software.
  23. *
  24. * 3. This notice may not be removed or altered from any source
  25. * distribution.
  26. */
  27. // Fallback for systems that don't support WebGL
  28. if(typeof Float32Array != 'undefined') {
  29. glMatrixArrayType = Float32Array;
  30. } else if(typeof WebGLFloatArray != 'undefined') {
  31. glMatrixArrayType = WebGLFloatArray; // This is officially deprecated and should dissapear in future revisions.
  32. } else {
  33. glMatrixArrayType = Array;
  34. }
  35. /*
  36. * vec3 - 3 Dimensional Vector
  37. */
  38. var vec3 = {};
  39. /*
  40. * vec3.create
  41. * Creates a new instance of a vec3 using the default array type
  42. * Any javascript array containing at least 3 numeric elements can serve as a vec3
  43. *
  44. * Params:
  45. * vec - Optional, vec3 containing values to initialize with
  46. *
  47. * Returns:
  48. * New vec3
  49. */
  50. vec3.create = function(vec) {
  51. var dest = new glMatrixArrayType(3);
  52. if(vec) {
  53. dest[0] = vec[0];
  54. dest[1] = vec[1];
  55. dest[2] = vec[2];
  56. }
  57. return dest;
  58. };
  59. /*
  60. * vec3.set
  61. * Copies the values of one vec3 to another
  62. *
  63. * Params:
  64. * vec - vec3 containing values to copy
  65. * dest - vec3 receiving copied values
  66. *
  67. * Returns:
  68. * dest
  69. */
  70. vec3.set = function(vec, dest) {
  71. dest[0] = vec[0];
  72. dest[1] = vec[1];
  73. dest[2] = vec[2];
  74. return dest;
  75. };
  76. /*
  77. * vec3.add
  78. * Performs a vector addition
  79. *
  80. * Params:
  81. * vec - vec3, first operand
  82. * vec2 - vec3, second operand
  83. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  84. *
  85. * Returns:
  86. * dest if specified, vec otherwise
  87. */
  88. vec3.add = function(vec, vec2, dest) {
  89. if(!dest || vec == dest) {
  90. vec[0] += vec2[0];
  91. vec[1] += vec2[1];
  92. vec[2] += vec2[2];
  93. return vec;
  94. }
  95. dest[0] = vec[0] + vec2[0];
  96. dest[1] = vec[1] + vec2[1];
  97. dest[2] = vec[2] + vec2[2];
  98. return dest;
  99. };
  100. /*
  101. * vec3.subtract
  102. * Performs a vector subtraction
  103. *
  104. * Params:
  105. * vec - vec3, first operand
  106. * vec2 - vec3, second operand
  107. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  108. *
  109. * Returns:
  110. * dest if specified, vec otherwise
  111. */
  112. vec3.subtract = function(vec, vec2, dest) {
  113. if(!dest || vec == dest) {
  114. vec[0] -= vec2[0];
  115. vec[1] -= vec2[1];
  116. vec[2] -= vec2[2];
  117. return vec;
  118. }
  119. dest[0] = vec[0] - vec2[0];
  120. dest[1] = vec[1] - vec2[1];
  121. dest[2] = vec[2] - vec2[2];
  122. return dest;
  123. };
  124. /*
  125. * vec3.negate
  126. * Negates the components of a vec3
  127. *
  128. * Params:
  129. * vec - vec3 to negate
  130. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  131. *
  132. * Returns:
  133. * dest if specified, vec otherwise
  134. */
  135. vec3.negate = function(vec, dest) {
  136. if(!dest) { dest = vec; }
  137. dest[0] = -vec[0];
  138. dest[1] = -vec[1];
  139. dest[2] = -vec[2];
  140. return dest;
  141. };
  142. /*
  143. * vec3.scale
  144. * Multiplies the components of a vec3 by a scalar value
  145. *
  146. * Params:
  147. * vec - vec3 to scale
  148. * val - Numeric value to scale by
  149. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  150. *
  151. * Returns:
  152. * dest if specified, vec otherwise
  153. */
  154. vec3.scale = function(vec, val, dest) {
  155. if(!dest || vec == dest) {
  156. vec[0] *= val;
  157. vec[1] *= val;
  158. vec[2] *= val;
  159. return vec;
  160. }
  161. dest[0] = vec[0]*val;
  162. dest[1] = vec[1]*val;
  163. dest[2] = vec[2]*val;
  164. return dest;
  165. };
  166. /*
  167. * vec3.normalize
  168. * Generates a unit vector of the same direction as the provided vec3
  169. * If vector length is 0, returns [0, 0, 0]
  170. *
  171. * Params:
  172. * vec - vec3 to normalize
  173. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  174. *
  175. * Returns:
  176. * dest if specified, vec otherwise
  177. */
  178. vec3.normalize = function(vec, dest) {
  179. if(!dest) { dest = vec; }
  180. var x = vec[0], y = vec[1], z = vec[2];
  181. var len = Math.sqrt(x*x + y*y + z*z);
  182. if (!len) {
  183. dest[0] = 0;
  184. dest[1] = 0;
  185. dest[2] = 0;
  186. return dest;
  187. } else if (len == 1) {
  188. dest[0] = x;
  189. dest[1] = y;
  190. dest[2] = z;
  191. return dest;
  192. }
  193. len = 1 / len;
  194. dest[0] = x*len;
  195. dest[1] = y*len;
  196. dest[2] = z*len;
  197. return dest;
  198. };
  199. /*
  200. * vec3.cross
  201. * Generates the cross product of two vec3s
  202. *
  203. * Params:
  204. * vec - vec3, first operand
  205. * vec2 - vec3, second operand
  206. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  207. *
  208. * Returns:
  209. * dest if specified, vec otherwise
  210. */
  211. vec3.cross = function(vec, vec2, dest){
  212. if(!dest) { dest = vec; }
  213. var x = vec[0], y = vec[1], z = vec[2];
  214. var x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
  215. dest[0] = y*z2 - z*y2;
  216. dest[1] = z*x2 - x*z2;
  217. dest[2] = x*y2 - y*x2;
  218. return dest;
  219. };
  220. /*
  221. * vec3.length
  222. * Caclulates the length of a vec3
  223. *
  224. * Params:
  225. * vec - vec3 to calculate length of
  226. *
  227. * Returns:
  228. * Length of vec
  229. */
  230. vec3.length = function(vec){
  231. var x = vec[0], y = vec[1], z = vec[2];
  232. return Math.sqrt(x*x + y*y + z*z);
  233. };
  234. /*
  235. * vec3.dot
  236. * Caclulates the dot product of two vec3s
  237. *
  238. * Params:
  239. * vec - vec3, first operand
  240. * vec2 - vec3, second operand
  241. *
  242. * Returns:
  243. * Dot product of vec and vec2
  244. */
  245. vec3.dot = function(vec, vec2){
  246. return vec[0]*vec2[0] + vec[1]*vec2[1] + vec[2]*vec2[2];
  247. };
  248. /*
  249. * vec3.direction
  250. * Generates a unit vector pointing from one vector to another
  251. *
  252. * Params:
  253. * vec - origin vec3
  254. * vec2 - vec3 to point to
  255. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  256. *
  257. * Returns:
  258. * dest if specified, vec otherwise
  259. */
  260. vec3.direction = function(vec, vec2, dest) {
  261. if(!dest) { dest = vec; }
  262. var x = vec[0] - vec2[0];
  263. var y = vec[1] - vec2[1];
  264. var z = vec[2] - vec2[2];
  265. var len = Math.sqrt(x*x + y*y + z*z);
  266. if (!len) {
  267. dest[0] = 0;
  268. dest[1] = 0;
  269. dest[2] = 0;
  270. return dest;
  271. }
  272. len = 1 / len;
  273. dest[0] = x * len;
  274. dest[1] = y * len;
  275. dest[2] = z * len;
  276. return dest;
  277. };
  278. /*
  279. * vec3.lerp
  280. * Performs a linear interpolation between two vec3
  281. *
  282. * Params:
  283. * vec - vec3, first vector
  284. * vec2 - vec3, second vector
  285. * lerp - interpolation amount between the two inputs
  286. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  287. *
  288. * Returns:
  289. * dest if specified, vec otherwise
  290. */
  291. vec3.lerp = function(vec, vec2, lerp, dest){
  292. if(!dest) { dest = vec; }
  293. dest[0] = vec[0] + lerp * (vec2[0] - vec[0]);
  294. dest[1] = vec[1] + lerp * (vec2[1] - vec[1]);
  295. dest[2] = vec[2] + lerp * (vec2[2] - vec[2]);
  296. return dest;
  297. }
  298. /*
  299. * vec3.str
  300. * Returns a string representation of a vector
  301. *
  302. * Params:
  303. * vec - vec3 to represent as a string
  304. *
  305. * Returns:
  306. * string representation of vec
  307. */
  308. vec3.str = function(vec) {
  309. return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ']';
  310. };
  311. /*
  312. * mat3 - 3x3 Matrix
  313. */
  314. var mat3 = {};
  315. /*
  316. * mat3.create
  317. * Creates a new instance of a mat3 using the default array type
  318. * Any javascript array containing at least 9 numeric elements can serve as a mat3
  319. *
  320. * Params:
  321. * mat - Optional, mat3 containing values to initialize with
  322. *
  323. * Returns:
  324. * New mat3
  325. */
  326. mat3.create = function(mat) {
  327. var dest = new glMatrixArrayType(9);
  328. if(mat) {
  329. dest[0] = mat[0];
  330. dest[1] = mat[1];
  331. dest[2] = mat[2];
  332. dest[3] = mat[3];
  333. dest[4] = mat[4];
  334. dest[5] = mat[5];
  335. dest[6] = mat[6];
  336. dest[7] = mat[7];
  337. dest[8] = mat[8];
  338. }
  339. return dest;
  340. };
  341. /*
  342. * mat3.set
  343. * Copies the values of one mat3 to another
  344. *
  345. * Params:
  346. * mat - mat3 containing values to copy
  347. * dest - mat3 receiving copied values
  348. *
  349. * Returns:
  350. * dest
  351. */
  352. mat3.set = function(mat, dest) {
  353. dest[0] = mat[0];
  354. dest[1] = mat[1];
  355. dest[2] = mat[2];
  356. dest[3] = mat[3];
  357. dest[4] = mat[4];
  358. dest[5] = mat[5];
  359. dest[6] = mat[6];
  360. dest[7] = mat[7];
  361. dest[8] = mat[8];
  362. return dest;
  363. };
  364. /*
  365. * mat3.identity
  366. * Sets a mat3 to an identity matrix
  367. *
  368. * Params:
  369. * dest - mat3 to set
  370. *
  371. * Returns:
  372. * dest
  373. */
  374. mat3.identity = function(dest) {
  375. dest[0] = 1;
  376. dest[1] = 0;
  377. dest[2] = 0;
  378. dest[3] = 0;
  379. dest[4] = 1;
  380. dest[5] = 0;
  381. dest[6] = 0;
  382. dest[7] = 0;
  383. dest[8] = 1;
  384. return dest;
  385. };
  386. /*
  387. * mat4.transpose
  388. * Transposes a mat3 (flips the values over the diagonal)
  389. *
  390. * Params:
  391. * mat - mat3 to transpose
  392. * dest - Optional, mat3 receiving transposed values. If not specified result is written to mat
  393. *
  394. * Returns:
  395. * dest is specified, mat otherwise
  396. */
  397. mat3.transpose = function(mat, dest) {
  398. // If we are transposing ourselves we can skip a few steps but have to cache some values
  399. if(!dest || mat == dest) {
  400. var a01 = mat[1], a02 = mat[2];
  401. var a12 = mat[5];
  402. mat[1] = mat[3];
  403. mat[2] = mat[6];
  404. mat[3] = a01;
  405. mat[5] = mat[7];
  406. mat[6] = a02;
  407. mat[7] = a12;
  408. return mat;
  409. }
  410. dest[0] = mat[0];
  411. dest[1] = mat[3];
  412. dest[2] = mat[6];
  413. dest[3] = mat[1];
  414. dest[4] = mat[4];
  415. dest[5] = mat[7];
  416. dest[6] = mat[2];
  417. dest[7] = mat[5];
  418. dest[8] = mat[8];
  419. return dest;
  420. };
  421. /*
  422. * mat3.toMat4
  423. * Copies the elements of a mat3 into the upper 3x3 elements of a mat4
  424. *
  425. * Params:
  426. * mat - mat3 containing values to copy
  427. * dest - Optional, mat4 receiving copied values
  428. *
  429. * Returns:
  430. * dest if specified, a new mat4 otherwise
  431. */
  432. mat3.toMat4 = function(mat, dest) {
  433. if(!dest) { dest = mat4.create(); }
  434. dest[0] = mat[0];
  435. dest[1] = mat[1];
  436. dest[2] = mat[2];
  437. dest[3] = 0;
  438. dest[4] = mat[3];
  439. dest[5] = mat[4];
  440. dest[6] = mat[5];
  441. dest[7] = 0;
  442. dest[8] = mat[6];
  443. dest[9] = mat[7];
  444. dest[10] = mat[8];
  445. dest[11] = 0;
  446. dest[12] = 0;
  447. dest[13] = 0;
  448. dest[14] = 0;
  449. dest[15] = 1;
  450. return dest;
  451. }
  452. /*
  453. * mat3.str
  454. * Returns a string representation of a mat3
  455. *
  456. * Params:
  457. * mat - mat3 to represent as a string
  458. *
  459. * Returns:
  460. * string representation of mat
  461. */
  462. mat3.str = function(mat) {
  463. return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] +
  464. ', ' + mat[3] + ', '+ mat[4] + ', ' + mat[5] +
  465. ', ' + mat[6] + ', ' + mat[7] + ', '+ mat[8] + ']';
  466. };
  467. /*
  468. * mat4 - 4x4 Matrix
  469. */
  470. var mat4 = {};
  471. /*
  472. * mat4.create
  473. * Creates a new instance of a mat4 using the default array type
  474. * Any javascript array containing at least 16 numeric elements can serve as a mat4
  475. *
  476. * Params:
  477. * mat - Optional, mat4 containing values to initialize with
  478. *
  479. * Returns:
  480. * New mat4
  481. */
  482. mat4.create = function(mat) {
  483. var dest = new glMatrixArrayType(16);
  484. if(mat) {
  485. dest[0] = mat[0];
  486. dest[1] = mat[1];
  487. dest[2] = mat[2];
  488. dest[3] = mat[3];
  489. dest[4] = mat[4];
  490. dest[5] = mat[5];
  491. dest[6] = mat[6];
  492. dest[7] = mat[7];
  493. dest[8] = mat[8];
  494. dest[9] = mat[9];
  495. dest[10] = mat[10];
  496. dest[11] = mat[11];
  497. dest[12] = mat[12];
  498. dest[13] = mat[13];
  499. dest[14] = mat[14];
  500. dest[15] = mat[15];
  501. }
  502. return dest;
  503. };
  504. /*
  505. * mat4.set
  506. * Copies the values of one mat4 to another
  507. *
  508. * Params:
  509. * mat - mat4 containing values to copy
  510. * dest - mat4 receiving copied values
  511. *
  512. * Returns:
  513. * dest
  514. */
  515. mat4.set = function(mat, dest) {
  516. dest[0] = mat[0];
  517. dest[1] = mat[1];
  518. dest[2] = mat[2];
  519. dest[3] = mat[3];
  520. dest[4] = mat[4];
  521. dest[5] = mat[5];
  522. dest[6] = mat[6];
  523. dest[7] = mat[7];
  524. dest[8] = mat[8];
  525. dest[9] = mat[9];
  526. dest[10] = mat[10];
  527. dest[11] = mat[11];
  528. dest[12] = mat[12];
  529. dest[13] = mat[13];
  530. dest[14] = mat[14];
  531. dest[15] = mat[15];
  532. return dest;
  533. };
  534. /*
  535. * mat4.identity
  536. * Sets a mat4 to an identity matrix
  537. *
  538. * Params:
  539. * dest - mat4 to set
  540. *
  541. * Returns:
  542. * dest
  543. */
  544. mat4.identity = function(dest) {
  545. dest[0] = 1;
  546. dest[1] = 0;
  547. dest[2] = 0;
  548. dest[3] = 0;
  549. dest[4] = 0;
  550. dest[5] = 1;
  551. dest[6] = 0;
  552. dest[7] = 0;
  553. dest[8] = 0;
  554. dest[9] = 0;
  555. dest[10] = 1;
  556. dest[11] = 0;
  557. dest[12] = 0;
  558. dest[13] = 0;
  559. dest[14] = 0;
  560. dest[15] = 1;
  561. return dest;
  562. };
  563. /*
  564. * mat4.transpose
  565. * Transposes a mat4 (flips the values over the diagonal)
  566. *
  567. * Params:
  568. * mat - mat4 to transpose
  569. * dest - Optional, mat4 receiving transposed values. If not specified result is written to mat
  570. *
  571. * Returns:
  572. * dest is specified, mat otherwise
  573. */
  574. mat4.transpose = function(mat, dest) {
  575. // If we are transposing ourselves we can skip a few steps but have to cache some values
  576. if(!dest || mat == dest) {
  577. var a01 = mat[1], a02 = mat[2], a03 = mat[3];
  578. var a12 = mat[6], a13 = mat[7];
  579. var a23 = mat[11];
  580. mat[1] = mat[4];
  581. mat[2] = mat[8];
  582. mat[3] = mat[12];
  583. mat[4] = a01;
  584. mat[6] = mat[9];
  585. mat[7] = mat[13];
  586. mat[8] = a02;
  587. mat[9] = a12;
  588. mat[11] = mat[14];
  589. mat[12] = a03;
  590. mat[13] = a13;
  591. mat[14] = a23;
  592. return mat;
  593. }
  594. dest[0] = mat[0];
  595. dest[1] = mat[4];
  596. dest[2] = mat[8];
  597. dest[3] = mat[12];
  598. dest[4] = mat[1];
  599. dest[5] = mat[5];
  600. dest[6] = mat[9];
  601. dest[7] = mat[13];
  602. dest[8] = mat[2];
  603. dest[9] = mat[6];
  604. dest[10] = mat[10];
  605. dest[11] = mat[14];
  606. dest[12] = mat[3];
  607. dest[13] = mat[7];
  608. dest[14] = mat[11];
  609. dest[15] = mat[15];
  610. return dest;
  611. };
  612. /*
  613. * mat4.determinant
  614. * Calculates the determinant of a mat4
  615. *
  616. * Params:
  617. * mat - mat4 to calculate determinant of
  618. *
  619. * Returns:
  620. * determinant of mat
  621. */
  622. mat4.determinant = function(mat) {
  623. // Cache the matrix values (makes for huge speed increases!)
  624. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  625. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  626. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  627. var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
  628. return a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
  629. a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
  630. a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
  631. a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
  632. a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
  633. a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
  634. };
  635. /*
  636. * mat4.inverse
  637. * Calculates the inverse matrix of a mat4
  638. *
  639. * Params:
  640. * mat - mat4 to calculate inverse of
  641. * dest - Optional, mat4 receiving inverse matrix. If not specified result is written to mat
  642. *
  643. * Returns:
  644. * dest is specified, mat otherwise
  645. */
  646. mat4.inverse = function(mat, dest) {
  647. if(!dest) { dest = mat; }
  648. // Cache the matrix values (makes for huge speed increases!)
  649. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  650. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  651. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  652. var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
  653. var b00 = a00*a11 - a01*a10;
  654. var b01 = a00*a12 - a02*a10;
  655. var b02 = a00*a13 - a03*a10;
  656. var b03 = a01*a12 - a02*a11;
  657. var b04 = a01*a13 - a03*a11;
  658. var b05 = a02*a13 - a03*a12;
  659. var b06 = a20*a31 - a21*a30;
  660. var b07 = a20*a32 - a22*a30;
  661. var b08 = a20*a33 - a23*a30;
  662. var b09 = a21*a32 - a22*a31;
  663. var b10 = a21*a33 - a23*a31;
  664. var b11 = a22*a33 - a23*a32;
  665. // Calculate the determinant (inlined to avoid double-caching)
  666. var invDet = 1/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
  667. dest[0] = (a11*b11 - a12*b10 + a13*b09)*invDet;
  668. dest[1] = (-a01*b11 + a02*b10 - a03*b09)*invDet;
  669. dest[2] = (a31*b05 - a32*b04 + a33*b03)*invDet;
  670. dest[3] = (-a21*b05 + a22*b04 - a23*b03)*invDet;
  671. dest[4] = (-a10*b11 + a12*b08 - a13*b07)*invDet;
  672. dest[5] = (a00*b11 - a02*b08 + a03*b07)*invDet;
  673. dest[6] = (-a30*b05 + a32*b02 - a33*b01)*invDet;
  674. dest[7] = (a20*b05 - a22*b02 + a23*b01)*invDet;
  675. dest[8] = (a10*b10 - a11*b08 + a13*b06)*invDet;
  676. dest[9] = (-a00*b10 + a01*b08 - a03*b06)*invDet;
  677. dest[10] = (a30*b04 - a31*b02 + a33*b00)*invDet;
  678. dest[11] = (-a20*b04 + a21*b02 - a23*b00)*invDet;
  679. dest[12] = (-a10*b09 + a11*b07 - a12*b06)*invDet;
  680. dest[13] = (a00*b09 - a01*b07 + a02*b06)*invDet;
  681. dest[14] = (-a30*b03 + a31*b01 - a32*b00)*invDet;
  682. dest[15] = (a20*b03 - a21*b01 + a22*b00)*invDet;
  683. return dest;
  684. };
  685. /*
  686. * mat4.toRotationMat
  687. * Copies the upper 3x3 elements of a mat4 into another mat4
  688. *
  689. * Params:
  690. * mat - mat4 containing values to copy
  691. * dest - Optional, mat4 receiving copied values
  692. *
  693. * Returns:
  694. * dest is specified, a new mat4 otherwise
  695. */
  696. mat4.toRotationMat = function(mat, dest) {
  697. if(!dest) { dest = mat4.create(); }
  698. dest[0] = mat[0];
  699. dest[1] = mat[1];
  700. dest[2] = mat[2];
  701. dest[3] = mat[3];
  702. dest[4] = mat[4];
  703. dest[5] = mat[5];
  704. dest[6] = mat[6];
  705. dest[7] = mat[7];
  706. dest[8] = mat[8];
  707. dest[9] = mat[9];
  708. dest[10] = mat[10];
  709. dest[11] = mat[11];
  710. dest[12] = 0;
  711. dest[13] = 0;
  712. dest[14] = 0;
  713. dest[15] = 1;
  714. return dest;
  715. };
  716. /*
  717. * mat4.toMat3
  718. * Copies the upper 3x3 elements of a mat4 into a mat3
  719. *
  720. * Params:
  721. * mat - mat4 containing values to copy
  722. * dest - Optional, mat3 receiving copied values
  723. *
  724. * Returns:
  725. * dest is specified, a new mat3 otherwise
  726. */
  727. mat4.toMat3 = function(mat, dest) {
  728. if(!dest) { dest = mat3.create(); }
  729. dest[0] = mat[0];
  730. dest[1] = mat[1];
  731. dest[2] = mat[2];
  732. dest[3] = mat[4];
  733. dest[4] = mat[5];
  734. dest[5] = mat[6];
  735. dest[6] = mat[8];
  736. dest[7] = mat[9];
  737. dest[8] = mat[10];
  738. return dest;
  739. };
  740. /*
  741. * mat4.toInverseMat3
  742. * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
  743. * The resulting matrix is useful for calculating transformed normals
  744. *
  745. * Params:
  746. * mat - mat4 containing values to invert and copy
  747. * dest - Optional, mat3 receiving values
  748. *
  749. * Returns:
  750. * dest is specified, a new mat3 otherwise
  751. */
  752. mat4.toInverseMat3 = function(mat, dest) {
  753. // Cache the matrix values (makes for huge speed increases!)
  754. var a00 = mat[0], a01 = mat[1], a02 = mat[2];
  755. var a10 = mat[4], a11 = mat[5], a12 = mat[6];
  756. var a20 = mat[8], a21 = mat[9], a22 = mat[10];
  757. var b01 = a22*a11-a12*a21;
  758. var b11 = -a22*a10+a12*a20;
  759. var b21 = a21*a10-a11*a20;
  760. var d = a00*b01 + a01*b11 + a02*b21;
  761. if (!d) { return null; }
  762. var id = 1/d;
  763. if(!dest) { dest = mat3.create(); }
  764. dest[0] = b01*id;
  765. dest[1] = (-a22*a01 + a02*a21)*id;
  766. dest[2] = (a12*a01 - a02*a11)*id;
  767. dest[3] = b11*id;
  768. dest[4] = (a22*a00 - a02*a20)*id;
  769. dest[5] = (-a12*a00 + a02*a10)*id;
  770. dest[6] = b21*id;
  771. dest[7] = (-a21*a00 + a01*a20)*id;
  772. dest[8] = (a11*a00 - a01*a10)*id;
  773. return dest;
  774. };
  775. /*
  776. * mat4.multiply
  777. * Performs a matrix multiplication
  778. *
  779. * Params:
  780. * mat - mat4, first operand
  781. * mat2 - mat4, second operand
  782. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  783. *
  784. * Returns:
  785. * dest if specified, mat otherwise
  786. */
  787. mat4.multiply = function(mat, mat2, dest) {
  788. if(!dest) { dest = mat }
  789. // Cache the matrix values (makes for huge speed increases!)
  790. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  791. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  792. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  793. var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
  794. var b00 = mat2[0], b01 = mat2[1], b02 = mat2[2], b03 = mat2[3];
  795. var b10 = mat2[4], b11 = mat2[5], b12 = mat2[6], b13 = mat2[7];
  796. var b20 = mat2[8], b21 = mat2[9], b22 = mat2[10], b23 = mat2[11];
  797. var b30 = mat2[12], b31 = mat2[13], b32 = mat2[14], b33 = mat2[15];
  798. dest[0] = b00*a00 + b01*a10 + b02*a20 + b03*a30;
  799. dest[1] = b00*a01 + b01*a11 + b02*a21 + b03*a31;
  800. dest[2] = b00*a02 + b01*a12 + b02*a22 + b03*a32;
  801. dest[3] = b00*a03 + b01*a13 + b02*a23 + b03*a33;
  802. dest[4] = b10*a00 + b11*a10 + b12*a20 + b13*a30;
  803. dest[5] = b10*a01 + b11*a11 + b12*a21 + b13*a31;
  804. dest[6] = b10*a02 + b11*a12 + b12*a22 + b13*a32;
  805. dest[7] = b10*a03 + b11*a13 + b12*a23 + b13*a33;
  806. dest[8] = b20*a00 + b21*a10 + b22*a20 + b23*a30;
  807. dest[9] = b20*a01 + b21*a11 + b22*a21 + b23*a31;
  808. dest[10] = b20*a02 + b21*a12 + b22*a22 + b23*a32;
  809. dest[11] = b20*a03 + b21*a13 + b22*a23 + b23*a33;
  810. dest[12] = b30*a00 + b31*a10 + b32*a20 + b33*a30;
  811. dest[13] = b30*a01 + b31*a11 + b32*a21 + b33*a31;
  812. dest[14] = b30*a02 + b31*a12 + b32*a22 + b33*a32;
  813. dest[15] = b30*a03 + b31*a13 + b32*a23 + b33*a33;
  814. return dest;
  815. };
  816. /*
  817. * mat4.multiplyVec3
  818. * Transforms a vec3 with the given matrix
  819. * 4th vector component is implicitly '1'
  820. *
  821. * Params:
  822. * mat - mat4 to transform the vector with
  823. * vec - vec3 to transform
  824. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  825. *
  826. * Returns:
  827. * dest if specified, vec otherwise
  828. */
  829. mat4.multiplyVec3 = function(mat, vec, dest) {
  830. if(!dest) { dest = vec }
  831. var x = vec[0], y = vec[1], z = vec[2];
  832. dest[0] = mat[0]*x + mat[4]*y + mat[8]*z + mat[12];
  833. dest[1] = mat[1]*x + mat[5]*y + mat[9]*z + mat[13];
  834. dest[2] = mat[2]*x + mat[6]*y + mat[10]*z + mat[14];
  835. return dest;
  836. };
  837. /*
  838. * mat4.multiplyVec4
  839. * Transforms a vec4 with the given matrix
  840. *
  841. * Params:
  842. * mat - mat4 to transform the vector with
  843. * vec - vec4 to transform
  844. * dest - Optional, vec4 receiving operation result. If not specified result is written to vec
  845. *
  846. * Returns:
  847. * dest if specified, vec otherwise
  848. */
  849. mat4.multiplyVec4 = function(mat, vec, dest) {
  850. if(!dest) { dest = vec }
  851. var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
  852. dest[0] = mat[0]*x + mat[4]*y + mat[8]*z + mat[12]*w;
  853. dest[1] = mat[1]*x + mat[5]*y + mat[9]*z + mat[13]*w;
  854. dest[2] = mat[2]*x + mat[6]*y + mat[10]*z + mat[14]*w;
  855. dest[3] = mat[3]*x + mat[7]*y + mat[11]*z + mat[15]*w;
  856. return dest;
  857. };
  858. /*
  859. * mat4.translate
  860. * Translates a matrix by the given vector
  861. *
  862. * Params:
  863. * mat - mat4 to translate
  864. * vec - vec3 specifying the translation
  865. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  866. *
  867. * Returns:
  868. * dest if specified, mat otherwise
  869. */
  870. mat4.translate = function(mat, vec, dest) {
  871. var x = vec[0], y = vec[1], z = vec[2];
  872. if(!dest || mat == dest) {
  873. mat[12] = mat[0]*x + mat[4]*y + mat[8]*z + mat[12];
  874. mat[13] = mat[1]*x + mat[5]*y + mat[9]*z + mat[13];
  875. mat[14] = mat[2]*x + mat[6]*y + mat[10]*z + mat[14];
  876. mat[15] = mat[3]*x + mat[7]*y + mat[11]*z + mat[15];
  877. return mat;
  878. }
  879. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  880. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  881. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  882. dest[0] = a00;
  883. dest[1] = a01;
  884. dest[2] = a02;
  885. dest[3] = a03;
  886. dest[4] = a10;
  887. dest[5] = a11;
  888. dest[6] = a12;
  889. dest[7] = a13;
  890. dest[8] = a20;
  891. dest[9] = a21;
  892. dest[10] = a22;
  893. dest[11] = a23;
  894. dest[12] = a00*x + a10*y + a20*z + mat[12];
  895. dest[13] = a01*x + a11*y + a21*z + mat[13];
  896. dest[14] = a02*x + a12*y + a22*z + mat[14];
  897. dest[15] = a03*x + a13*y + a23*z + mat[15];
  898. return dest;
  899. };
  900. /*
  901. * mat4.scale
  902. * Scales a matrix by the given vector
  903. *
  904. * Params:
  905. * mat - mat4 to scale
  906. * vec - vec3 specifying the scale for each axis
  907. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  908. *
  909. * Returns:
  910. * dest if specified, mat otherwise
  911. */
  912. mat4.scale = function(mat, vec, dest) {
  913. var x = vec[0], y = vec[1], z = vec[2];
  914. if(!dest || mat == dest) {
  915. mat[0] *= x;
  916. mat[1] *= x;
  917. mat[2] *= x;
  918. mat[3] *= x;
  919. mat[4] *= y;
  920. mat[5] *= y;
  921. mat[6] *= y;
  922. mat[7] *= y;
  923. mat[8] *= z;
  924. mat[9] *= z;
  925. mat[10] *= z;
  926. mat[11] *= z;
  927. return mat;
  928. }
  929. dest[0] = mat[0]*x;
  930. dest[1] = mat[1]*x;
  931. dest[2] = mat[2]*x;
  932. dest[3] = mat[3]*x;
  933. dest[4] = mat[4]*y;
  934. dest[5] = mat[5]*y;
  935. dest[6] = mat[6]*y;
  936. dest[7] = mat[7]*y;
  937. dest[8] = mat[8]*z;
  938. dest[9] = mat[9]*z;
  939. dest[10] = mat[10]*z;
  940. dest[11] = mat[11]*z;
  941. dest[12] = mat[12];
  942. dest[13] = mat[13];
  943. dest[14] = mat[14];
  944. dest[15] = mat[15];
  945. return dest;
  946. };
  947. /*
  948. * mat4.rotate
  949. * Rotates a matrix by the given angle around the specified axis
  950. * If rotating around a primary axis (X,Y,Z) one of the specialized rotation functions should be used instead for performance
  951. *
  952. * Params:
  953. * mat - mat4 to rotate
  954. * angle - angle (in radians) to rotate
  955. * axis - vec3 representing the axis to rotate around
  956. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  957. *
  958. * Returns:
  959. * dest if specified, mat otherwise
  960. */
  961. mat4.rotate = function(mat, angle, axis, dest) {
  962. var x = axis[0], y = axis[1], z = axis[2];
  963. var len = Math.sqrt(x*x + y*y + z*z);
  964. if (!len) { return null; }
  965. if (len != 1) {
  966. len = 1 / len;
  967. x *= len;
  968. y *= len;
  969. z *= len;
  970. }
  971. var s = Math.sin(angle);
  972. var c = Math.cos(angle);
  973. var t = 1-c;
  974. // Cache the matrix values (makes for huge speed increases!)
  975. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  976. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  977. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  978. // Construct the elements of the rotation matrix
  979. var b00 = x*x*t + c, b01 = y*x*t + z*s, b02 = z*x*t - y*s;
  980. var b10 = x*y*t - z*s, b11 = y*y*t + c, b12 = z*y*t + x*s;
  981. var b20 = x*z*t + y*s, b21 = y*z*t - x*s, b22 = z*z*t + c;
  982. if(!dest) {
  983. dest = mat
  984. } else if(mat != dest) { // If the source and destination differ, copy the unchanged last row
  985. dest[12] = mat[12];
  986. dest[13] = mat[13];
  987. dest[14] = mat[14];
  988. dest[15] = mat[15];
  989. }
  990. // Perform rotation-specific matrix multiplication
  991. dest[0] = a00*b00 + a10*b01 + a20*b02;
  992. dest[1] = a01*b00 + a11*b01 + a21*b02;
  993. dest[2] = a02*b00 + a12*b01 + a22*b02;
  994. dest[3] = a03*b00 + a13*b01 + a23*b02;
  995. dest[4] = a00*b10 + a10*b11 + a20*b12;
  996. dest[5] = a01*b10 + a11*b11 + a21*b12;
  997. dest[6] = a02*b10 + a12*b11 + a22*b12;
  998. dest[7] = a03*b10 + a13*b11 + a23*b12;
  999. dest[8] = a00*b20 + a10*b21 + a20*b22;
  1000. dest[9] = a01*b20 + a11*b21 + a21*b22;
  1001. dest[10] = a02*b20 + a12*b21 + a22*b22;
  1002. dest[11] = a03*b20 + a13*b21 + a23*b22;
  1003. return dest;
  1004. };
  1005. /*
  1006. * mat4.rotateX
  1007. * Rotates a matrix by the given angle around the X axis
  1008. *
  1009. * Params:
  1010. * mat - mat4 to rotate
  1011. * angle - angle (in radians) to rotate
  1012. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  1013. *
  1014. * Returns:
  1015. * dest if specified, mat otherwise
  1016. */
  1017. mat4.rotateX = function(mat, angle, dest) {
  1018. var s = Math.sin(angle);
  1019. var c = Math.cos(angle);
  1020. // Cache the matrix values (makes for huge speed increases!)
  1021. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  1022. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  1023. if(!dest) {
  1024. dest = mat
  1025. } else if(mat != dest) { // If the source and destination differ, copy the unchanged rows
  1026. dest[0] = mat[0];
  1027. dest[1] = mat[1];
  1028. dest[2] = mat[2];
  1029. dest[3] = mat[3];
  1030. dest[12] = mat[12];
  1031. dest[13] = mat[13];
  1032. dest[14] = mat[14];
  1033. dest[15] = mat[15];
  1034. }
  1035. // Perform axis-specific matrix multiplication
  1036. dest[4] = a10*c + a20*s;
  1037. dest[5] = a11*c + a21*s;
  1038. dest[6] = a12*c + a22*s;
  1039. dest[7] = a13*c + a23*s;
  1040. dest[8] = a10*-s + a20*c;
  1041. dest[9] = a11*-s + a21*c;
  1042. dest[10] = a12*-s + a22*c;
  1043. dest[11] = a13*-s + a23*c;
  1044. return dest;
  1045. };
  1046. /*
  1047. * mat4.rotateY
  1048. * Rotates a matrix by the given angle around the Y axis
  1049. *
  1050. * Params:
  1051. * mat - mat4 to rotate
  1052. * angle - angle (in radians) to rotate
  1053. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  1054. *
  1055. * Returns:
  1056. * dest if specified, mat otherwise
  1057. */
  1058. mat4.rotateY = function(mat, angle, dest) {
  1059. var s = Math.sin(angle);
  1060. var c = Math.cos(angle);
  1061. // Cache the matrix values (makes for huge speed increases!)
  1062. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  1063. var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
  1064. if(!dest) {
  1065. dest = mat
  1066. } else if(mat != dest) { // If the source and destination differ, copy the unchanged rows
  1067. dest[4] = mat[4];
  1068. dest[5] = mat[5];
  1069. dest[6] = mat[6];
  1070. dest[7] = mat[7];
  1071. dest[12] = mat[12];
  1072. dest[13] = mat[13];
  1073. dest[14] = mat[14];
  1074. dest[15] = mat[15];
  1075. }
  1076. // Perform axis-specific matrix multiplication
  1077. dest[0] = a00*c + a20*-s;
  1078. dest[1] = a01*c + a21*-s;
  1079. dest[2] = a02*c + a22*-s;
  1080. dest[3] = a03*c + a23*-s;
  1081. dest[8] = a00*s + a20*c;
  1082. dest[9] = a01*s + a21*c;
  1083. dest[10] = a02*s + a22*c;
  1084. dest[11] = a03*s + a23*c;
  1085. return dest;
  1086. };
  1087. /*
  1088. * mat4.rotateZ
  1089. * Rotates a matrix by the given angle around the Z axis
  1090. *
  1091. * Params:
  1092. * mat - mat4 to rotate
  1093. * angle - angle (in radians) to rotate
  1094. * dest - Optional, mat4 receiving operation result. If not specified result is written to mat
  1095. *
  1096. * Returns:
  1097. * dest if specified, mat otherwise
  1098. */
  1099. mat4.rotateZ = function(mat, angle, dest) {
  1100. var s = Math.sin(angle);
  1101. var c = Math.cos(angle);
  1102. // Cache the matrix values (makes for huge speed increases!)
  1103. var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
  1104. var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
  1105. if(!dest) {
  1106. dest = mat
  1107. } else if(mat != dest) { // If the source and destination differ, copy the unchanged last row
  1108. dest[8] = mat[8];
  1109. dest[9] = mat[9];
  1110. dest[10] = mat[10];
  1111. dest[11] = mat[11];
  1112. dest[12] = mat[12];
  1113. dest[13] = mat[13];
  1114. dest[14] = mat[14];
  1115. dest[15] = mat[15];
  1116. }
  1117. // Perform axis-specific matrix multiplication
  1118. dest[0] = a00*c + a10*s;
  1119. dest[1] = a01*c + a11*s;
  1120. dest[2] = a02*c + a12*s;
  1121. dest[3] = a03*c + a13*s;
  1122. dest[4] = a00*-s + a10*c;
  1123. dest[5] = a01*-s + a11*c;
  1124. dest[6] = a02*-s + a12*c;
  1125. dest[7] = a03*-s + a13*c;
  1126. return dest;
  1127. };
  1128. /*
  1129. * mat4.frustum
  1130. * Generates a frustum matrix with the given bounds
  1131. *
  1132. * Params:
  1133. * left, right - scalar, left and right bounds of the frustum
  1134. * bottom, top - scalar, bottom and top bounds of the frustum
  1135. * near, far - scalar, near and far bounds of the frustum
  1136. * dest - Optional, mat4 frustum matrix will be written into
  1137. *
  1138. * Returns:
  1139. * dest if specified, a new mat4 otherwise
  1140. */
  1141. mat4.frustum = function(left, right, bottom, top, near, far, dest) {
  1142. if(!dest) { dest = mat4.create(); }
  1143. var rl = (right - left);
  1144. var tb = (top - bottom);
  1145. var fn = (far - near);
  1146. dest[0] = (near*2) / rl;
  1147. dest[1] = 0;
  1148. dest[2] = 0;
  1149. dest[3] = 0;
  1150. dest[4] = 0;
  1151. dest[5] = (near*2) / tb;
  1152. dest[6] = 0;
  1153. dest[7] = 0;
  1154. dest[8] = (right + left) / rl;
  1155. dest[9] = (top + bottom) / tb;
  1156. dest[10] = -(far + near) / fn;
  1157. dest[11] = -1;
  1158. dest[12] = 0;
  1159. dest[13] = 0;
  1160. dest[14] = -(far*near*2) / fn;
  1161. dest[15] = 0;
  1162. return dest;
  1163. };
  1164. /*
  1165. * mat4.perspective
  1166. * Generates a perspective projection matrix with the given bounds
  1167. *
  1168. * Params:
  1169. * fovy - scalar, vertical field of view
  1170. * aspect - scalar, aspect ratio. typically viewport width/height
  1171. * near, far - scalar, near and far bounds of the frustum
  1172. * dest - Optional, mat4 frustum matrix will be written into
  1173. *
  1174. * Returns:
  1175. * dest if specified, a new mat4 otherwise
  1176. */
  1177. mat4.perspective = function(fovy, aspect, near, far, dest) {
  1178. var top = near*Math.tan(fovy*Math.PI / 360.0);
  1179. var right = top*aspect;
  1180. return mat4.frustum(-right, right, -top, top, near, far, dest);
  1181. };
  1182. /*
  1183. * mat4.ortho
  1184. * Generates a orthogonal projection matrix with the given bounds
  1185. *
  1186. * Params:
  1187. * left, right - scalar, left and right bounds of the frustum
  1188. * bottom, top - scalar, bottom and top bounds of the frustum
  1189. * near, far - scalar, near and far bounds of the frustum
  1190. * dest - Optional, mat4 frustum matrix will be written into
  1191. *
  1192. * Returns:
  1193. * dest if specified, a new mat4 otherwise
  1194. */
  1195. mat4.ortho = function(left, right, bottom, top, near, far, dest) {
  1196. if(!dest) { dest = mat4.create(); }
  1197. var rl = (right - left);
  1198. var tb = (top - bottom);
  1199. var fn = (far - near);
  1200. dest[0] = 2 / rl;
  1201. dest[1] = 0;
  1202. dest[2] = 0;
  1203. dest[3] = 0;
  1204. dest[4] = 0;
  1205. dest[5] = 2 / tb;
  1206. dest[6] = 0;
  1207. dest[7] = 0;
  1208. dest[8] = 0;
  1209. dest[9] = 0;
  1210. dest[10] = -2 / fn;
  1211. dest[11] = 0;
  1212. dest[12] = -(left + right) / rl;
  1213. dest[13] = -(top + bottom) / tb;
  1214. dest[14] = -(far + near) / fn;
  1215. dest[15] = 1;
  1216. return dest;
  1217. };
  1218. /*
  1219. * mat4.ortho
  1220. * Generates a look-at matrix with the given eye position, focal point, and up axis
  1221. *
  1222. * Params:
  1223. * eye - vec3, position of the viewer
  1224. * center - vec3, point the viewer is looking at
  1225. * up - vec3 pointing "up"
  1226. * dest - Optional, mat4 frustum matrix will be written into
  1227. *
  1228. * Returns:
  1229. * dest if specified, a new mat4 otherwise
  1230. */
  1231. mat4.lookAt = function(eye, center, up, dest) {
  1232. if(!dest) { dest = mat4.create(); }
  1233. var eyex = eye[0],
  1234. eyey = eye[1],
  1235. eyez = eye[2],
  1236. upx = up[0],
  1237. upy = up[1],
  1238. upz = up[2],
  1239. centerx = center[0],
  1240. centery = center[1],
  1241. centerz = center[2];
  1242. if (eyex == centerx && eyey == centery && eyez == centerz) {
  1243. return mat4.identity(dest);
  1244. }
  1245. var z0,z1,z2,x0,x1,x2,y0,y1,y2,len;
  1246. //vec3.direction(eye, center, z);
  1247. z0 = eyex - center[0];
  1248. z1 = eyey - center[1];
  1249. z2 = eyez - center[2];
  1250. // normalize (no check needed for 0 because of early return)
  1251. len = 1/Math.sqrt(z0*z0 + z1*z1 + z2*z2);
  1252. z0 *= len;
  1253. z1 *= len;
  1254. z2 *= len;
  1255. //vec3.normalize(vec3.cross(up, z, x));
  1256. x0 = upy*z2 - upz*z1;
  1257. x1 = upz*z0 - upx*z2;
  1258. x2 = upx*z1 - upy*z0;
  1259. len = Math.sqrt(x0*x0 + x1*x1 + x2*x2);
  1260. if (!len) {
  1261. x0 = 0;
  1262. x1 = 0;
  1263. x2 = 0;
  1264. } else {
  1265. len = 1/len;
  1266. x0 *= len;
  1267. x1 *= len;
  1268. x2 *= len;
  1269. };
  1270. //vec3.normalize(vec3.cross(z, x, y));
  1271. y0 = z1*x2 - z2*x1;
  1272. y1 = z2*x0 - z0*x2;
  1273. y2 = z0*x1 - z1*x0;
  1274. len = Math.sqrt(y0*y0 + y1*y1 + y2*y2);
  1275. if (!len) {
  1276. y0 = 0;
  1277. y1 = 0;
  1278. y2 = 0;
  1279. } else {
  1280. len = 1/len;
  1281. y0 *= len;
  1282. y1 *= len;
  1283. y2 *= len;
  1284. }
  1285. dest[0] = x0;
  1286. dest[1] = y0;
  1287. dest[2] = z0;
  1288. dest[3] = 0;
  1289. dest[4] = x1;
  1290. dest[5] = y1;
  1291. dest[6] = z1;
  1292. dest[7] = 0;
  1293. dest[8] = x2;
  1294. dest[9] = y2;
  1295. dest[10] = z2;
  1296. dest[11] = 0;
  1297. dest[12] = -(x0*eyex + x1*eyey + x2*eyez);
  1298. dest[13] = -(y0*eyex + y1*eyey + y2*eyez);
  1299. dest[14] = -(z0*eyex + z1*eyey + z2*eyez);
  1300. dest[15] = 1;
  1301. return dest;
  1302. };
  1303. /*
  1304. * mat4.str
  1305. * Returns a string representation of a mat4
  1306. *
  1307. * Params:
  1308. * mat - mat4 to represent as a string
  1309. *
  1310. * Returns:
  1311. * string representation of mat
  1312. */
  1313. mat4.str = function(mat) {
  1314. return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] +
  1315. ', '+ mat[4] + ', ' + mat[5] + ', ' + mat[6] + ', ' + mat[7] +
  1316. ', '+ mat[8] + ', ' + mat[9] + ', ' + mat[10] + ', ' + mat[11] +
  1317. ', '+ mat[12] + ', ' + mat[13] + ', ' + mat[14] + ', ' + mat[15] + ']';
  1318. };
  1319. /*
  1320. * quat4 - Quaternions
  1321. */
  1322. quat4 = {};
  1323. /*
  1324. * quat4.create
  1325. * Creates a new instance of a quat4 using the default array type
  1326. * Any javascript array containing at least 4 numeric elements can serve as a quat4
  1327. *
  1328. * Params:
  1329. * quat - Optional, quat4 containing values to initialize with
  1330. *
  1331. * Returns:
  1332. * New quat4
  1333. */
  1334. quat4.create = function(quat) {
  1335. var dest = new glMatrixArrayType(4);
  1336. if(quat) {
  1337. dest[0] = quat[0];
  1338. dest[1] = quat[1];
  1339. dest[2] = quat[2];
  1340. dest[3] = quat[3];
  1341. }
  1342. return dest;
  1343. };
  1344. /*
  1345. * quat4.set
  1346. * Copies the values of one quat4 to another
  1347. *
  1348. * Params:
  1349. * quat - quat4 containing values to copy
  1350. * dest - quat4 receiving copied values
  1351. *
  1352. * Returns:
  1353. * dest
  1354. */
  1355. quat4.set = function(quat, dest) {
  1356. dest[0] = quat[0];
  1357. dest[1] = quat[1];
  1358. dest[2] = quat[2];
  1359. dest[3] = quat[3];
  1360. return dest;
  1361. };
  1362. /*
  1363. * quat4.calculateW
  1364. * Calculates the W component of a quat4 from the X, Y, and Z components.
  1365. * Assumes that quaternion is 1 unit in length.
  1366. * Any existing W component will be ignored.
  1367. *
  1368. * Params:
  1369. * quat - quat4 to calculate W component of
  1370. * dest - Optional, quat4 receiving calculated values. If not specified result is written to quat
  1371. *
  1372. * Returns:
  1373. * dest if specified, quat otherwise
  1374. */
  1375. quat4.calculateW = function(quat, dest) {
  1376. var x = quat[0], y = quat[1], z = quat[2];
  1377. if(!dest || quat == dest) {
  1378. quat[3] = -Math.sqrt(Math.abs(1.0 - x*x - y*y - z*z));
  1379. return quat;
  1380. }
  1381. dest[0] = x;
  1382. dest[1] = y;
  1383. dest[2] = z;
  1384. dest[3] = -Math.sqrt(Math.abs(1.0 - x*x - y*y - z*z));
  1385. return dest;
  1386. }
  1387. /*
  1388. * quat4.inverse
  1389. * Calculates the inverse of a quat4
  1390. *
  1391. * Params:
  1392. * quat - quat4 to calculate inverse of
  1393. * dest - Optional, quat4 receiving inverse values. If not specified result is written to quat
  1394. *
  1395. * Returns:
  1396. * dest if specified, quat otherwise
  1397. */
  1398. quat4.inverse = function(quat, dest) {
  1399. if(!dest || quat == dest) {
  1400. quat[0] *= -1;
  1401. quat[1] *= -1;
  1402. quat[2] *= -1;
  1403. return quat;
  1404. }
  1405. dest[0] = -quat[0];
  1406. dest[1] = -quat[1];
  1407. dest[2] = -quat[2];
  1408. dest[3] = quat[3];
  1409. return dest;
  1410. }
  1411. /*
  1412. * quat4.length
  1413. * Calculates the length of a quat4
  1414. *
  1415. * Params:
  1416. * quat - quat4 to calculate length of
  1417. *
  1418. * Returns:
  1419. * Length of quat
  1420. */
  1421. quat4.length = function(quat) {
  1422. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  1423. return Math.sqrt(x*x + y*y + z*z + w*w);
  1424. }
  1425. /*
  1426. * quat4.normalize
  1427. * Generates a unit quaternion of the same direction as the provided quat4
  1428. * If quaternion length is 0, returns [0, 0, 0, 0]
  1429. *
  1430. * Params:
  1431. * quat - quat4 to normalize
  1432. * dest - Optional, quat4 receiving operation result. If not specified result is written to quat
  1433. *
  1434. * Returns:
  1435. * dest if specified, quat otherwise
  1436. */
  1437. quat4.normalize = function(quat, dest) {
  1438. if(!dest) { dest = quat; }
  1439. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  1440. var len = Math.sqrt(x*x + y*y + z*z + w*w);
  1441. if(len == 0) {
  1442. dest[0] = 0;
  1443. dest[1] = 0;
  1444. dest[2] = 0;
  1445. dest[3] = 0;
  1446. return dest;
  1447. }
  1448. len = 1/len;
  1449. dest[0] = x * len;
  1450. dest[1] = y * len;
  1451. dest[2] = z * len;
  1452. dest[3] = w * len;
  1453. return dest;
  1454. }
  1455. /*
  1456. * quat4.multiply
  1457. * Performs a quaternion multiplication
  1458. *
  1459. * Params:
  1460. * quat - quat4, first operand
  1461. * quat2 - quat4, second operand
  1462. * dest - Optional, quat4 receiving operation result. If not specified result is written to quat
  1463. *
  1464. * Returns:
  1465. * dest if specified, quat otherwise
  1466. */
  1467. quat4.multiply = function(quat, quat2, dest) {
  1468. if(!dest) { dest = quat; }
  1469. var qax = quat[0], qay = quat[1], qaz = quat[2], qaw = quat[3];
  1470. var qbx = quat2[0], qby = quat2[1], qbz = quat2[2], qbw = quat2[3];
  1471. dest[0] = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
  1472. dest[1] = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
  1473. dest[2] = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
  1474. dest[3] = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;
  1475. return dest;
  1476. }
  1477. /*
  1478. * quat4.multiplyVec3
  1479. * Transforms a vec3 with the given quaternion
  1480. *
  1481. * Params:
  1482. * quat - quat4 to transform the vector with
  1483. * vec - vec3 to transform
  1484. * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
  1485. *
  1486. * Returns:
  1487. * dest if specified, vec otherwise
  1488. */
  1489. quat4.multiplyVec3 = function(quat, vec, dest) {
  1490. if(!dest) { dest = vec; }
  1491. var x = vec[0], y = vec[1], z = vec[2];
  1492. var qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3];
  1493. // calculate quat * vec
  1494. var ix = qw*x + qy*z - qz*y;
  1495. var iy = qw*y + qz*x - qx*z;
  1496. var iz = qw*z + qx*y - qy*x;
  1497. var iw = -qx*x - qy*y - qz*z;
  1498. // calculate result * inverse quat
  1499. dest[0] = ix*qw + iw*-qx + iy*-qz - iz*-qy;
  1500. dest[1] = iy*qw + iw*-qy + iz*-qx - ix*-qz;
  1501. dest[2] = iz*qw + iw*-qz + ix*-qy - iy*-qx;
  1502. return dest;
  1503. }
  1504. /*
  1505. * quat4.toMat3
  1506. * Calculates a 3x3 matrix from the given quat4
  1507. *
  1508. * Params:
  1509. * quat - quat4 to create matrix from
  1510. * dest - Optional, mat3 receiving operation result
  1511. *
  1512. * Returns:
  1513. * dest if specified, a new mat3 otherwise
  1514. */
  1515. quat4.toMat3 = function(quat, dest) {
  1516. if(!dest) { dest = mat3.create(); }
  1517. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  1518. var x2 = x + x;
  1519. var y2 = y + y;
  1520. var z2 = z + z;
  1521. var xx = x*x2;
  1522. var xy = x*y2;
  1523. var xz = x*z2;
  1524. var yy = y*y2;
  1525. var yz = y*z2;
  1526. var zz = z*z2;
  1527. var wx = w*x2;
  1528. var wy = w*y2;
  1529. var wz = w*z2;
  1530. dest[0] = 1 - (yy + zz);
  1531. dest[1] = xy - wz;
  1532. dest[2] = xz + wy;
  1533. dest[3] = xy + wz;
  1534. dest[4] = 1 - (xx + zz);
  1535. dest[5] = yz - wx;
  1536. dest[6] = xz - wy;
  1537. dest[7] = yz + wx;
  1538. dest[8] = 1 - (xx + yy);
  1539. return dest;
  1540. }
  1541. /*
  1542. * quat4.toMat4
  1543. * Calculates a 4x4 matrix from the given quat4
  1544. *
  1545. * Params:
  1546. * quat - quat4 to create matrix from
  1547. * dest - Optional, mat4 receiving operation result
  1548. *
  1549. * Returns:
  1550. * dest if specified, a new mat4 otherwise
  1551. */
  1552. quat4.toMat4 = function(quat, dest) {
  1553. if(!dest) { dest = mat4.create(); }
  1554. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  1555. var x2 = x + x;
  1556. var y2 = y + y;
  1557. var z2 = z + z;
  1558. var xx = x*x2;
  1559. var xy = x*y2;
  1560. var xz = x*z2;
  1561. var yy = y*y2;
  1562. var yz = y*z2;
  1563. var zz = z*z2;
  1564. var wx = w*x2;
  1565. var wy = w*y2;
  1566. var wz = w*z2;
  1567. dest[0] = 1 - (yy + zz);
  1568. dest[1] = xy - wz;
  1569. dest[2] = xz + wy;
  1570. dest[3] = 0;
  1571. dest[4] = xy + wz;
  1572. dest[5] = 1 - (xx + zz);
  1573. dest[6] = yz - wx;
  1574. dest[7] = 0;
  1575. dest[8] = xz - wy;
  1576. dest[9] = yz + wx;
  1577. dest[10] = 1 - (xx + yy);
  1578. dest[11] = 0;
  1579. dest[12] = 0;
  1580. dest[13] = 0;
  1581. dest[14] = 0;
  1582. dest[15] = 1;
  1583. return dest;
  1584. }
  1585. /*
  1586. * quat4.slerp
  1587. * Performs a spherical linear interpolation between two quat4
  1588. *
  1589. * Params:
  1590. * quat - quat4, first quaternion
  1591. * quat2 - quat4, second quaternion
  1592. * slerp - interpolation amount between the two inputs
  1593. * dest - Optional, quat4 receiving operation result. If not specified result is written to quat
  1594. *
  1595. * Returns:
  1596. * dest if specified, quat otherwise
  1597. */
  1598. quat4.slerp = function(quat, quat2, slerp, dest) {
  1599. if(!dest) { dest = quat; }
  1600. var cosHalfTheta = quat[0]*quat2[0] + quat[1]*quat2[1] + quat[2]*quat2[2] + quat[3]*quat2[3];
  1601. if (Math.abs(cosHalfTheta) >= 1.0){
  1602. if(dest != quat) {
  1603. dest[0] = quat[0];
  1604. dest[1] = quat[1];
  1605. dest[2] = quat[2];
  1606. dest[3] = quat[3];
  1607. }
  1608. return dest;
  1609. }
  1610. var halfTheta = Math.acos(cosHalfTheta);
  1611. var sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta*cosHalfTheta);
  1612. if (Math.abs(sinHalfTheta) < 0.001){
  1613. dest[0] = (quat[0]*0.5 + quat2[0]*0.5);
  1614. dest[1] = (quat[1]*0.5 + quat2[1]*0.5);
  1615. dest[2] = (quat[2]*0.5 + quat2[2]*0.5);
  1616. dest[3] = (quat[3]*0.5 + quat2[3]*0.5);
  1617. return dest;
  1618. }
  1619. var ratioA = Math.sin((1 - slerp)*halfTheta) / sinHalfTheta;
  1620. var ratioB = Math.sin(slerp*halfTheta) / sinHalfTheta;
  1621. dest[0] = (quat[0]*ratioA + quat2[0]*ratioB);
  1622. dest[1] = (quat[1]*ratioA + quat2[1]*ratioB);
  1623. dest[2] = (quat[2]*ratioA + quat2[2]*ratioB);
  1624. dest[3] = (quat[3]*ratioA + quat2[3]*ratioB);
  1625. return dest;
  1626. }
  1627. /*
  1628. * quat4.str
  1629. * Returns a string representation of a quaternion
  1630. *
  1631. * Params:
  1632. * quat - quat4 to represent as a string
  1633. *
  1634. * Returns:
  1635. * string representation of quat
  1636. */
  1637. quat4.str = function(quat) {
  1638. return '[' + quat[0] + ', ' + quat[1] + ', ' + quat[2] + ', ' + quat[3] + ']';
  1639. }