diff options
author | Jon Reid | 2012-03-06 14:15:18 -0800 |
---|---|---|
committer | Jon Reid | 2012-03-06 14:15:18 -0800 |
commit | 78d42c7cbf9205ed2e4faee1d8ddf02a4725edd3 (patch) | |
tree | c395b703f21431dcb4432c725f4c860431add61b /js/lib/math | |
parent | fa8b81d4f436c0b8564c8dcdd22d8dd6d5bccfaa (diff) | |
parent | 60bba95eaffa8b5c741c6c85fb84b327cd75d6c3 (diff) | |
download | ninja-78d42c7cbf9205ed2e4faee1d8ddf02a4725edd3.tar.gz |
Merge branch 'Timeline-uber' into timeline-serialized
Diffstat (limited to 'js/lib/math')
-rwxr-xr-x | js/lib/math/matrix.js | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/js/lib/math/matrix.js b/js/lib/math/matrix.js new file mode 100755 index 00000000..77a0966d --- /dev/null +++ b/js/lib/math/matrix.js | |||
@@ -0,0 +1,104 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Matrix = function Matrix() { | ||
8 | |||
9 | }; | ||
10 | |||
11 | Matrix.create = function( rowArray ) { | ||
12 | var mat = Matrix.I(4); | ||
13 | var index = 0; | ||
14 | for(var j=0; j<4; j++) { | ||
15 | for (var i=0; i<4; i++) { | ||
16 | mat[index] = rowArray[i][j]; | ||
17 | index++; | ||
18 | } | ||
19 | } | ||
20 | |||
21 | return mat; | ||
22 | }; | ||
23 | |||
24 | Matrix.I = function(dimen) { | ||
25 | var mat = []; | ||
26 | |||
27 | for (var i = 0; i<dimen*dimen; i++) { | ||
28 | mat.push(0); | ||
29 | } | ||
30 | |||
31 | var index = 0; | ||
32 | for (var j = 0; j<dimen; j++) { | ||
33 | mat[index] = 1.0; | ||
34 | index += dimen + 1; | ||
35 | } | ||
36 | |||
37 | return mat; | ||
38 | }; | ||
39 | |||
40 | Matrix.Translation = function (vec) { | ||
41 | var mat = Matrix.I(4); | ||
42 | glmat4.translate(mat, vec); | ||
43 | return mat; | ||
44 | }; | ||
45 | |||
46 | Matrix.RotationX = function( angle ) { | ||
47 | var mat = Matrix.I(4); | ||
48 | glmat4.rotateX(mat, angle); | ||
49 | return mat; | ||
50 | }; | ||
51 | |||
52 | Matrix.RotationY = function( angle ) { | ||
53 | var mat = Matrix.I(4); | ||
54 | glmat4.rotateY(mat, angle); | ||
55 | return mat; | ||
56 | }; | ||
57 | |||
58 | Matrix.RotationZ = function( angle ) { | ||
59 | var mat = Matrix.I(4); | ||
60 | glmat4.rotateZ(mat, angle); | ||
61 | return mat; | ||
62 | }; | ||
63 | |||
64 | Matrix.Rotation = function(angle, axis) { | ||
65 | var mat = Matrix.I(4); | ||
66 | glmat4.rotate(mat, angle, axis); | ||
67 | return mat; | ||
68 | }; | ||
69 | |||
70 | Matrix.flatten = function (mat) { | ||
71 | var result = []; | ||
72 | if (this.elements.length == 0) { | ||
73 | return []; | ||
74 | } | ||
75 | |||
76 | for (var i=0; i<16; i++) { | ||
77 | result.push(mat[i]); | ||
78 | } | ||
79 | |||
80 | return result; | ||
81 | }; | ||
82 | |||
83 | Matrix.makePerspective = function (fovy, aspect, znear, zfar) { | ||
84 | var ymax = znear * Math.tan(fovy * Math.PI / 360.0); | ||
85 | var ymin = -ymax; | ||
86 | var xmin = ymin * aspect; | ||
87 | var xmax = ymax * aspect; | ||
88 | |||
89 | //return makeFrustum(xmin, xmax, ymin, ymax, znear, zfar); | ||
90 | |||
91 | var X = 2*znear/(xmax-xmin); | ||
92 | var Y = 2*znear/(ymax-ymin); | ||
93 | var A = (xmax+xmin)/(xmax-xmin); | ||
94 | var B = (ymax+ymin)/(ymax-ymin); | ||
95 | var C = -(zfar+znear)/(zfar-znear); | ||
96 | var D = -2*zfar*znear/(zfar-znear); | ||
97 | |||
98 | return Matrix.create([[X, 0, A, 0], [0, Y, B, 0], [0, 0, C, D], [0, 0, -1, 0]]); | ||
99 | |||
100 | }; | ||
101 | |||
102 | // Namepace this class | ||
103 | // TODO | ||
104 | window.Matrix = Matrix; | ||