diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/math | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/math')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/mat4.js | 754 | ||||
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/quat.js | 225 | ||||
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/vec2.js | 191 | ||||
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/vec3.js | 351 | ||||
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/vec4.js | 258 |
5 files changed, 1779 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/math/mat4.js b/js/helper-classes/RDGE/src/core/script/math/mat4.js new file mode 100644 index 00000000..fab57732 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/math/mat4.js | |||
@@ -0,0 +1,754 @@ | |||
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 | |||
8 | float3 = function() { | ||
9 | data = [3]; | ||
10 | } | ||
11 | |||
12 | float4 = function() { | ||
13 | data = [4]; | ||
14 | } | ||
15 | |||
16 | /** | ||
17 | * mat4 = {} | ||
18 | * This library contains functions for operating on 4x4 matrices. Any JS array | ||
19 | * containing at least 16 numeric components can represent a 4x4 matrix. | ||
20 | * | ||
21 | * For example, all of these are valid matrix construction methods: | ||
22 | * ... | ||
23 | * var a = mat4.identity(); | ||
24 | * var b = mat4.perspective(90, aspectRatio, 0.1, 100.00); | ||
25 | * var c = mat4.lookAt( [0, 0, 0], [1, 0, 0], [ 0, 1, 0 ] ); | ||
26 | * var d = mat4.basis( [1, 0, 0], [0, 1, 0], [ 0, 0, 1 ] ); | ||
27 | * | ||
28 | * This library is implemented assuming components are arranged | ||
29 | * contiguously in memory as such: | ||
30 | * M = [ x0, x1, x2, x3, | ||
31 | * y0, y1, y2, y3, | ||
32 | * z0, z1, z2, z3, | ||
33 | * w0, w1, w2, w3 ]; | ||
34 | * The translation components of a tranformation matrix would be stored in | ||
35 | * w0, w1, w2, or at indices 12, 13, and 14 of the array, as is consistent | ||
36 | * with opengl. | ||
37 | */ | ||
38 | mat4 = {} | ||
39 | |||
40 | /** | ||
41 | * mat4.string | ||
42 | */ | ||
43 | mat4.string = function(m) { | ||
44 | var out = "{ "; | ||
45 | out += m[0] + ", " + m[1] + ", " + m[2] + ", " + m[3] + ", "; | ||
46 | out += m[4] + ", " + m[5] + ", " + m[6] + ", " + m[7] + ", "; | ||
47 | out += m[8] + ", " + m[9] + ", " + m[10] + ", " + m[11] + ", "; | ||
48 | out += m[12] + ", " + m[13] + ", " + m[14] + ", " + m[15] + " }"; | ||
49 | return out; | ||
50 | } | ||
51 | |||
52 | mat4.toCSSString = function(m, conversionConstant) | ||
53 | { | ||
54 | var cc = 10.0; | ||
55 | |||
56 | if (conversionConstant) | ||
57 | cc = conversionConstant; | ||
58 | |||
59 | var out = "matrix3d("; | ||
60 | out += m[0].toFixed(10) + ", " + m[1].toFixed(10) + ", " + m[2].toFixed(10) + ", " + m[3].toFixed(10) + ", "; | ||
61 | out += m[4].toFixed(10) + ", " + m[5].toFixed(10) + ", " + m[6].toFixed(10) + ", " + m[7].toFixed(10) + ", "; | ||
62 | out += m[8].toFixed(10) + ", " + m[9].toFixed(10) + ", " + m[10].toFixed(10) + ", " + m[11].toFixed(10) + ", "; | ||
63 | out += m[12].toFixed(10)*cc + ", " + (600 - m[13].toFixed(10)*cc) + ", " + m[14].toFixed(10)*cc + ", " + m[15].toFixed(10) + ")"; | ||
64 | return out; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * mat4.verify | ||
69 | * This function is provided for debugging purposes only. It is not recommended | ||
70 | * to be used in performance critical areas of the code. | ||
71 | */ | ||
72 | mat4.verify = function(m) { | ||
73 | if (m == undefined || m.length == undefined || m.length < 16) { | ||
74 | return false; | ||
75 | } | ||
76 | var i = 16; | ||
77 | while (i--) { | ||
78 | if (typeof (m[i]) != "number") { | ||
79 | return false; | ||
80 | } | ||
81 | } | ||
82 | return true; | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * mat4.copy | ||
87 | */ | ||
88 | mat4.copy = function(m) { | ||
89 | return [ m[0], m[1], m[2], m[3], | ||
90 | m[4], m[5], m[6], m[7], | ||
91 | m[8], m[9], m[10], m[11], | ||
92 | m[12], m[13], m[14], m[15] ]; | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * mat4.inplace_copy | ||
97 | */ | ||
98 | mat4.inplace_copy = function(dst, src) { | ||
99 | dst[0] = src[0]; | ||
100 | dst[1] = src[1]; | ||
101 | dst[2] = src[2]; | ||
102 | dst[3] = src[3]; | ||
103 | dst[4] = src[4]; | ||
104 | dst[5] = src[5]; | ||
105 | dst[6] = src[6]; | ||
106 | dst[7] = src[7]; | ||
107 | dst[8] = src[8]; | ||
108 | dst[9] = src[9]; | ||
109 | dst[10] = src[10]; | ||
110 | dst[11] = src[11]; | ||
111 | dst[12] = src[12]; | ||
112 | dst[13] = src[13]; | ||
113 | dst[14] = src[14]; | ||
114 | dst[15] = src[15]; | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * mat4.identity | ||
119 | */ | ||
120 | mat4.identity = function() { | ||
121 | return [ 1.0, 0.0, 0.0, 0.0, | ||
122 | 0.0, 1.0, 0.0, 0.0, | ||
123 | 0.0, 0.0, 1.0, 0.0, | ||
124 | 0.0, 0.0, 0.0, 1.0 ]; | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * mat4.zero | ||
129 | */ | ||
130 | mat4.zero = function() { | ||
131 | return [ 0.0, 0.0, 0.0, 0.0, | ||
132 | 0.0, 0.0, 0.0, 0.0, | ||
133 | 0.0, 0.0, 0.0, 0.0, | ||
134 | 0.0, 0.0, 0.0, 0.0 ]; | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * mat4.basis | ||
139 | * description - construct a matrix with the given basis vectors. | ||
140 | */ | ||
141 | mat4.basis = function( rowx, rowy, rowz, roww ) { | ||
142 | if( roww == null || roww == undefined ) { | ||
143 | return [ rowx[0], rowx[1], rowx[2], 0.0, | ||
144 | rowy[0], rowy[1], rowy[2], 0.0, | ||
145 | rowz[0], rowz[1], rowz[2], 0.0, | ||
146 | 0, 0, 0, 1.0 ]; | ||
147 | } else { | ||
148 | return [ rowx[0], rowx[1], rowx[2], rowx.length == 4 ? rowx[3] : 0.0, | ||
149 | rowy[0], rowy[1], rowy[2], rowy.length == 4 ? rowy[3] : 0.0, | ||
150 | rowz[0], rowz[1], rowz[2], rowz.length == 4 ? rowz[3] : 0.0, | ||
151 | roww[0], roww[1], roww[2], roww.length == 4 ? roww[3] : 1.0 ]; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * mat4.angleAxis | ||
157 | */ | ||
158 | mat4.angleAxis = function(angle, axis) { | ||
159 | // angles are in degrees. Switch to radians | ||
160 | angle *= (Math.PI / 180.0); | ||
161 | |||
162 | angle /= 2; | ||
163 | var sinA = Math.sin(angle); | ||
164 | var cosA = Math.cos(angle); | ||
165 | var sinA2 = sinA * sinA; | ||
166 | |||
167 | // normalize | ||
168 | vec3.normalize(axis); | ||
169 | if (vec3.lengthSq(axis) <= 0.0) { | ||
170 | axis = [0, 0, 0, 1]; | ||
171 | } | ||
172 | |||
173 | var matR = mat4.identity(); | ||
174 | |||
175 | // optimize case where axis is along major axis | ||
176 | if (axis[0] == 1 && axis[1] == 0 && axis[2] == 0) { | ||
177 | matR[5] = 1 - 2 * sinA2; | ||
178 | matR[6] = 2 * sinA * cosA; | ||
179 | matR[9] = -2 * sinA * cosA; | ||
180 | matR[10] = 1 - 2 * sinA2; | ||
181 | } else if (axis[0] == 0 && axis[1] == 1 && axis[2] == 0) { | ||
182 | matR[0] = 1 - 2 * sinA2; | ||
183 | matR[2] = -2 * sinA * cosA; | ||
184 | matR[8] = 2 * sinA * cosA; | ||
185 | matR[10] = 1 - 2 * sinA2; | ||
186 | } else if (axis[0] == 0 && axis[1] == 0 && axis[2] == 1) { | ||
187 | matR[0] = 1 - 2 * sinA2; | ||
188 | matR[1] = 2 * sinA * cosA; | ||
189 | matR[4] = -2 * sinA * cosA; | ||
190 | matR[5] = 1 - 2 * sinA2; | ||
191 | } else { | ||
192 | var x = axis[0]; | ||
193 | var y = axis[1]; | ||
194 | var z = axis[2]; | ||
195 | var x2 = x * x; | ||
196 | var y2 = y * y; | ||
197 | var z2 = z * z; | ||
198 | |||
199 | matR[0] = 1 - 2 * (y2 + z2) * sinA2; | ||
200 | matR[1] = 2 * (x * y * sinA2 + z * sinA * cosA); | ||
201 | matR[2] = 2 * (x * z * sinA2 - y * sinA * cosA); | ||
202 | matR[4] = 2 * (y * x * sinA2 - z * sinA * cosA); | ||
203 | matR[5] = 1 - 2 * (z2 + x2) * sinA2; | ||
204 | matR[6] = 2 * (y * z * sinA2 + x * sinA * cosA); | ||
205 | matR[8] = 2 * (z * x * sinA2 + y * sinA * cosA); | ||
206 | matR[9] = 2 * (z * y * sinA2 - x * sinA * cosA); | ||
207 | matR[10] = 1 - 2 * (x2 + y2) * sinA2; | ||