6
|
1 /*
|
|
2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
|
|
3 *
|
|
4 * Redistribution and use in source and binary forms, with or without
|
|
5 * modification, are permitted provided that the following conditions
|
|
6 * are met:
|
|
7 * 1. Redistributions of source code must retain the above copyright
|
|
8 * notice, this list of conditions and the following disclaimer.
|
|
9 * 2. Redistributions in binary form must reproduce the above copyright
|
|
10 * notice, this list of conditions and the following disclaimer in the
|
|
11 * documentation and/or other materials provided with the distribution.
|
|
12 *
|
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
24 */
|
|
25
|
|
26 // J3DI (Jedi) - A support library for WebGL.
|
|
27
|
|
28 /*
|
|
29 J3DI Math Classes. Currently includes:
|
|
30
|
|
31 J3DIMatrix4 - A 4x4 Matrix
|
|
32 */
|
|
33
|
|
34 /*
|
|
35 J3DIMatrix4 class
|
|
36
|
|
37 This class implements a 4x4 matrix. It has functions which duplicate the
|
|
38 functionality of the OpenGL matrix stack and glut functions. On browsers
|
|
39 that support it, CSSMatrix is used to accelerate operations.
|
|
40
|
|
41 IDL:
|
|
42
|
|
43 [
|
|
44 Constructor(in J3DIMatrix4 matrix), // copy passed matrix into new J3DIMatrix4
|
|
45 Constructor(in sequence<float> array) // create new J3DIMatrix4 with 16 floats (row major)
|
|
46 Constructor() // create new J3DIMatrix4 with identity matrix
|
|
47 ]
|
|
48 interface J3DIMatrix4 {
|
|
49 void load(in J3DIMatrix4 matrix); // copy the values from the passed matrix
|
|
50 void load(in sequence<float> array); // copy 16 floats into the matrix
|
|
51 sequence<float> getAsArray(); // return the matrix as an array of 16 floats
|
|
52 Float32Array getAsFloat32Array(); // return the matrix as a Float32Array with 16 values
|
|
53 void setUniform(in WebGLRenderingContext ctx, // Send the matrix to the passed uniform location in the passed context
|
|
54 in WebGLUniformLocation loc,
|
|
55 in boolean transpose);
|
|
56 void makeIdentity(); // replace the matrix with identity
|
|
57 void transpose(); // replace the matrix with its transpose
|
|
58 void invert(); // replace the matrix with its inverse
|
|
59
|
|
60 void translate(in float x, in float y, in float z); // multiply the matrix by passed translation values on the right
|
|
61 void translate(in J3DVector3 v); // multiply the matrix by passed translation values on the right
|
|
62 void scale(in float x, in float y, in float z); // multiply the matrix by passed scale values on the right
|
|
63 void scale(in J3DVector3 v); // multiply the matrix by passed scale values on the right
|
|
64 void rotate(in float angle, // multiply the matrix by passed rotation values on the right
|
|
65 in float x, in float y, in float z); // (angle is in degrees)
|
|
66 void rotate(in float angle, in J3DVector3 v); // multiply the matrix by passed rotation values on the right
|
|
67 // (angle is in degrees)
|
|
68 void multiply(in CanvasMatrix matrix); // multiply the matrix by the passed matrix on the right
|
|
69 void divide(in float divisor); // divide the matrix by the passed divisor
|
|
70 void ortho(in float left, in float right, // multiply the matrix by the passed ortho values on the right
|
|
71 in float bottom, in float top,
|
|
72 in float near, in float far);
|
|
73 void frustum(in float left, in float right, // multiply the matrix by the passed frustum values on the right
|
|
74 in float bottom, in float top,
|
|
75 in float near, in float far);
|
|
76 void perspective(in float fovy, in float aspect, // multiply the matrix by the passed perspective values on the right
|
|
77 in float zNear, in float zFar);
|
|
78 void lookat(in J3DVector3 eye, // multiply the matrix by the passed lookat
|
|
79 in J3DVector3 center, in J3DVector3 up); // values on the right
|
|
80 bool decompose(in J3DVector3 translate, // decompose the matrix into the passed vector
|
|
81 in J3DVector3 rotate,
|
|
82 in J3DVector3 scale,
|
|
83 in J3DVector3 skew,
|
|
84 in sequence<float> perspective);
|
|
85 }
|
|
86
|
|
87 [
|
|
88 Constructor(in J3DVector3 vector), // copy passed vector into new J3DVector3
|
|
89 Constructor(in sequence<float> array) // create new J3DVector3 with 3 floats from array
|
|
90 Constructor(in float x, in float y, in float z) // create new J3DVector3 with 3 floats
|
|
91 Constructor() // create new J3DVector3 with (0,0,0)
|
|
92 ]
|
|
93 interface J3DVector3 {
|
|
94 void load(in J3DVector3 vector); // copy the values from the passed vector
|
|
95 void load(in sequence<float> array); // copy 3 floats into the vector from array
|
|
96 void load(in float x, in float y, in float z); // copy 3 floats into the vector
|
|
97 sequence<float> getAsArray(); // return the vector as an array of 3 floats
|
|
98 Float32Array getAsFloat32Array(); // return the matrix as a Float32Array with 16 values
|
|
99 void multMatrix(in J3DIMatrix4 matrix); // multiply the vector by the passed matrix (on the right)
|
|
100 float vectorLength(); // return the length of the vector
|
|
101 float dot(); // return the dot product of the vector
|
|
102 void cross(in J3DVector3 v); // replace the vector with vector x v
|
|
103 void divide(in float divisor); // divide the vector by the passed divisor
|
|
104 }
|
|
105 */
|
|
106
|
|
107 J3DIHasCSSMatrix = false;
|
|
108 J3DIHasCSSMatrixCopy = false;
|
|
109 /*
|
|
110 if ("WebKitCSSMatrix" in window && ("media" in window && window.media.matchMedium("(-webkit-transform-3d)")) ||
|
|
111 ("styleMedia" in window && window.styleMedia.matchMedium("(-webkit-transform-3d)"))) {
|
|
112 J3DIHasCSSMatrix = true;
|
|
113 if ("copy" in WebKitCSSMatrix.prototype)
|
|
114 J3DIHasCSSMatrixCopy = true;
|
|
115 }
|
|
116 */
|
|
117
|
|
118 // console.log("J3DIHasCSSMatrix="+J3DIHasCSSMatrix);
|
|
119 // console.log("J3DIHasCSSMatrixCopy="+J3DIHasCSSMatrixCopy);
|
|
120
|
|
121 //
|
|
122 // J3DIMatrix4
|
|
123 //
|
|
124 J3DIMatrix4 = function(m)
|
|
125 {
|
|
126 if (J3DIHasCSSMatrix)
|
|
127 this.$matrix = new WebKitCSSMatrix;
|
|
128 else
|
|
129 this.$matrix = new Object;
|
|
130
|
|
131 if (typeof m == 'object') {
|
|
132 if ("length" in m && m.length >= 16) {
|
|
133 this.load(m);
|
|
134 return;
|
|
135 }
|
|
136 else if (m instanceof J3DIMatrix4) {
|
|
137 this.load(m);
|
|
138 return;
|
|
139 }
|
|
140 }
|
|
141 this.makeIdentity();
|
|
142 }
|
|
143
|
|
144 J3DIMatrix4.prototype.load = function()
|
|
145 {
|
|
146 if (arguments.length == 1 && typeof arguments[0] == 'object') {
|
|
147 var matrix;
|
|
148
|
|
149 if (arguments[0] instanceof J3DIMatrix4) {
|
|
150 matrix = arguments[0].$matrix;
|
|
151
|
|
152 this.$matrix.m11 = matrix.m11;
|
|
153 this.$matrix.m12 = matrix.m12;
|
|
154 this.$matrix.m13 = matrix.m13;
|
|
155 this.$matrix.m14 = matrix.m14;
|
|
156
|
|
157 this.$matrix.m21 = matrix.m21;
|
|
158 this.$matrix.m22 = matrix.m22;
|
|
159 this.$matrix.m23 = matrix.m23;
|
|
160 this.$matrix.m24 = matrix.m24;
|
|
161
|
|
162 this.$matrix.m31 = matrix.m31;
|
|
163 this.$matrix.m32 = matrix.m32;
|
|
164 this.$matrix.m33 = matrix.m33;
|
|
165 this.$matrix.m34 = matrix.m34;
|
|
166
|
|
167 this.$matrix.m41 = matrix.m41;
|
|
168 this.$matrix.m42 = matrix.m42;
|
|
169 this.$matrix.m43 = matrix.m43;
|
|
170 this.$matrix.m44 = matrix.m44;
|
|
171 return;
|
|
172 }
|
|
173 else
|
|
174 matrix = arguments[0];
|
|
175
|
|
176 if ("length" in matrix && matrix.length >= 16) {
|
|
177 this.$matrix.m11 = matrix[0];
|
|
178 this.$matrix.m12 = matrix[1];
|
|
179 this.$matrix.m13 = matrix[2];
|
|
180 this.$matrix.m14 = matrix[3];
|
|
181
|
|
182 this.$matrix.m21 = matrix[4];
|
|
183 this.$matrix.m22 = matrix[5];
|
|
184 this.$matrix.m23 = matrix[6];
|
|
185 this.$matrix.m24 = matrix[7];
|
|
186
|
|
187 this.$matrix.m31 = matrix[8];
|
|
188 this.$matrix.m32 = matrix[9];
|
|
189 this.$matrix.m33 = matrix[10];
|
|
190 this.$matrix.m34 = matrix[11];
|
|
191
|
|
192 this.$matrix.m41 = matrix[12];
|
|
193 this.$matrix.m42 = matrix[13];
|
|
194 this.$matrix.m43 = matrix[14];
|
|
195 this.$matrix.m44 = matrix[15];
|
|
196 return;
|
|
197 }
|
|
198 }
|
|
199
|
|
200 this.makeIdentity();
|
|
201 }
|
|
202
|
|
203 J3DIMatrix4.prototype.getAsArray = function()
|
|
204 {
|
|
205 return [
|
|
206 this.$matrix.m11, this.$matrix.m12, this.$matrix.m13, this.$matrix.m14,
|
|
207 this.$matrix.m21, this.$matrix.m22, this.$matrix.m23, this.$matrix.m24,
|
|
208 this.$matrix.m31, this.$matrix.m32, this.$matrix.m33, this.$matrix.m34,
|
|
209 this.$matrix.m41, this.$matrix.m42, this.$matrix.m43, this.$matrix.m44
|
|
210 ];
|
|
211 }
|
|
212
|
|
213 J3DIMatrix4.prototype.getAsArrayMatrix = function(mat)
|
|
214 {
|
|
215 mat.m11 = this.$matrix.m11; mat.m12 = this.$matrix.m12;
|
|
216 mat.m13 = this.$matrix.m13; mat.m14 = this.$matrix.m14,
|
|
217 mat.m21 = this.$matrix.m21; mat.m22 = this.$matrix.m22;
|
|
218 mat.m23 = this.$matrix.m23; mat.m25 = this.$matrix.m24;
|
|
219 mat.m31 = this.$matrix.m31; mat.m32 = this.$matrix.m32;
|
|
220 mat.m33 = this.$matrix.m33; mat.m34 = this.$matrix.m34;
|
|
221 mat.m41 = this.$matrix.m41; mat.m42 = this.$matrix.m42;
|
|
222 mat.m43 = this.$matrix.m43; mat.m44 = this.$matrix.m44;
|
|
223 }
|
|
224
|
|
225 J3DIMatrix4.prototype.getAsFloat32Array = function()
|
|
226 {
|
|
227 if (J3DIHasCSSMatrixCopy) {
|
|
228 var array = new Float32Array(16);
|
|
229 this.$matrix.copy(array);
|
|
230 return array;
|
|
231 }
|
|
232 return new Float32Array(this.getAsArray());
|
|
233 }
|
|
234
|
|
235 J3DIMatrix4.prototype.setUniform = function(ctx, loc, transpose)
|
|
236 {
|
|
237 if (J3DIMatrix4.setUniformArray == undefined) {
|
|
238 J3DIMatrix4.setUniformWebGLArray = new Float32Array(16);
|
|
239 J3DIMatrix4.setUniformArray = new Array(16);
|
|
240 }
|
|
241
|
|
242 if (J3DIHasCSSMatrixCopy)
|
|
243 this.$matrix.copy(J3DIMatrix4.setUniformWebGLArray);
|
|
244 else {
|
|
245 J3DIMatrix4.setUniformArray[0] = this.$matrix.m11;
|
|
246 J3DIMatrix4.setUniformArray[1] = this.$matrix.m12;
|
|
247 J3DIMatrix4.setUniformArray[2] = this.$matrix.m13;
|
|
248 J3DIMatrix4.setUniformArray[3] = this.$matrix.m14;
|
|
249 J3DIMatrix4.setUniformArray[4] = this.$matrix.m21;
|
|
250 J3DIMatrix4.setUniformArray[5] = this.$matrix.m22;
|
|
251 J3DIMatrix4.setUniformArray[6] = this.$matrix.m23;
|
|
252 J3DIMatrix4.setUniformArray[7] = this.$matrix.m24;
|
|
253 J3DIMatrix4.setUniformArray[8] = this.$matrix.m31;
|
|
254 J3DIMatrix4.setUniformArray[9] = this.$matrix.m32;
|
|
255 J3DIMatrix4.setUniformArray[10] = this.$matrix.m33;
|
|
256 J3DIMatrix4.setUniformArray[11] = this.$matrix.m34;
|
|
257 J3DIMatrix4.setUniformArray[12] = this.$matrix.m41;
|
|
258 J3DIMatrix4.setUniformArray[13] = this.$matrix.m42;
|
|
259 J3DIMatrix4.setUniformArray[14] = this.$matrix.m43;
|
|
260 J3DIMatrix4.setUniformArray[15] = this.$matrix.m44;
|
|
261
|
|
262 J3DIMatrix4.setUniformWebGLArray.set(J3DIMatrix4.setUniformArray);
|
|
263 }
|
|
264
|
|
265 ctx.uniformMatrix4fv(loc, transpose, J3DIMatrix4.setUniformWebGLArray);
|
|
266 }
|
|
267
|
|
268 J3DIMatrix4.prototype.makeIdentity = function()
|
|
269 {
|
|
270 this.$matrix.m11 = 1;
|
|
271 this.$matrix.m12 = 0;
|
|
272 this.$matrix.m13 = 0;
|
|
273 this.$matrix.m14 = 0;
|
|
274
|
|
275 this.$matrix.m21 = 0;
|
|
276 this.$matrix.m22 = 1;
|
|
277 this.$matrix.m23 = 0;
|
|
278 this.$matrix.m24 = 0;
|
|
279
|
|
280 this.$matrix.m31 = 0;
|
|
281 this.$matrix.m32 = 0;
|
|
282 this.$matrix.m33 = 1;
|
|
283 this.$matrix.m34 = 0;
|
|
284
|
|
285 this.$matrix.m41 = 0;
|
|
286 this.$matrix.m42 = 0;
|
|
287 this.$matrix.m43 = 0;
|
|
288 this.$matrix.m44 = 1;
|
|
289 }
|
|
290
|
|
291 J3DIMatrix4.prototype.transpose = function()
|
|
292 {
|
|
293 var tmp = this.$matrix.m12;
|
|
294 this.$matrix.m12 = this.$matrix.m21;
|
|
295 this.$matrix.m21 = tmp;
|
|
296
|
|
297 tmp = this.$matrix.m13;
|
|
298 this.$matrix.m13 = this.$matrix.m31;
|
|
299 this.$matrix.m31 = tmp;
|
|
300
|
|
301 tmp = this.$matrix.m14;
|
|
302 this.$matrix.m14 = this.$matrix.m41;
|
|
303 this.$matrix.m41 = tmp;
|
|
304
|
|
305 tmp = this.$matrix.m23;
|
|
306 this.$matrix.m23 = this.$matrix.m32;
|
|
307 this.$matrix.m32 = tmp;
|
|
308
|
|
309 tmp = this.$matrix.m24;
|
|
310 this.$matrix.m24 = this.$matrix.m42;
|
|
311 this.$matrix.m42 = tmp;
|
|
312
|
|
313 tmp = this.$matrix.m34;
|
|
314 this.$matrix.m34 = this.$matrix.m43;
|
|
315 this.$matrix.m43 = tmp;
|
|
316 }
|
|
317
|
|
318 J3DIMatrix4.prototype.invert = function()
|
|
319 {
|
|
320 if (J3DIHasCSSMatrix) {
|
|
321 this.$matrix = this.$matrix.inverse();
|
|
322 return;
|
|
323 }
|
|
324
|
|
325 // Calculate the 4x4 determinant
|
|
326 // If the determinant is zero,
|
|
327 // then the inverse matrix is not unique.
|
|
328 var det = this._determinant4x4();
|
|
329
|
|
330 if (Math.abs(det) < 1e-8)
|
|
331 return null;
|
|
332
|
|
333 this._makeAdjoint();
|
|
334
|
|
335 // Scale the adjoint matrix to get the inverse
|
|
336 this.$matrix.m11 /= det;
|
|
337 this.$matrix.m12 /= det;
|
|
338 this.$matrix.m13 /= det;
|
|
339 this.$matrix.m14 /= det;
|
|
340
|
|
341 this.$matrix.m21 /= det;
|
|
342 this.$matrix.m22 /= det;
|
|
343 this.$matrix.m23 /= det;
|
|
344 this.$matrix.m24 /= det;
|
|
345
|
|
346 this.$matrix.m31 /= det;
|
|
347 this.$matrix.m32 /= det;
|
|
348 this.$matrix.m33 /= det;
|
|
349 this.$matrix.m34 /= det;
|
|
350
|
|
351 this.$matrix.m41 /= det;
|
|
352 this.$matrix.m42 /= det;
|
|
353 this.$matrix.m43 /= det;
|
|
354 this.$matrix.m44 /= det;
|
|
355 }
|
|
356
|
|
357 J3DIMatrix4.prototype.translate = function(x,y,z)
|
|
358 {
|
|
359 if (typeof x == 'object' && "length" in x) {
|
|
360 var t = x;
|
|
361 x = t[0];
|
|
362 y = t[1];
|
|
363 z = t[2];
|
|
364 }
|
|
365 else {
|
|
366 if (x == undefined)
|
|
367 x = 0;
|
|
368 if (y == undefined)
|
|
369 y = 0;
|
|
370 if (z == undefined)
|
|
371 z = 0;
|
|
372 }
|
|
373
|
|
374 if (J3DIHasCSSMatrix) {
|
|
375 this.$matrix = this.$matrix.translate(x, y, z);
|
|
376 return;
|
|
377 }
|
|
378
|
|
379 var matrix = new J3DIMatrix4();
|
|
380 matrix.$matrix.m41 = x;
|
|
381 matrix.$matrix.m42 = y;
|
|
382 matrix.$matrix.m43 = z;
|
|
383
|
|
384 this.multiply(matrix);
|
|
385 }
|
|
386
|
|
387 J3DIMatrix4.prototype.scale = function(x,y,z)
|
|
388 {
|
|
389 if (typeof x == 'object' && "length" in x) {
|
|
390 var t = x;
|
|
391 x = t[0];
|
|
392 y = t[1];
|
|
393 z = t[2];
|
|
394 }
|
|
395 else {
|
|
396 if (x == undefined)
|
|
397 x = 1;
|
|
398 if (z == undefined) {
|
|
399 if (y == undefined) {
|
|
400 y = x;
|
|
401 z = x;
|
|
402 }
|
|
403 else
|
|
404 z = 1;
|
|
405 }
|
|
406 else if (y == undefined)
|
|
407 y = x;
|
|
408 }
|
|
409
|
|
410 if (J3DIHasCSSMatrix) {
|
|
411 this.$matrix = this.$matrix.scale(x, y, z);
|
|
412 return;
|
|
413 }
|
|
414
|
|
415 var matrix = new J3DIMatrix4();
|
|
416 matrix.$matrix.m11 = x;
|
|
417 matrix.$matrix.m22 = y;
|
|
418 matrix.$matrix.m33 = z;
|
|
419
|
|
420 this.multiply(matrix);
|
|
421 }
|
|
422
|
|
423 J3DIMatrix4.prototype.rotate = function(angle,x,y,z)
|
|
424 {
|
|
425 // Forms are (angle, x,y,z), (angle,vector), (angleX, angleY, angleZ), (angle)
|
|
426 if (typeof x == 'object' && "length" in x) {
|
|
427 var t = x;
|
|
428 x = t[0];
|
|
429 y = t[1];
|
|
430 z = t[2];
|
|
431 }
|
|
432 else {
|
|
433 if (arguments.length == 1) {
|
|
434 x = 0;
|
|
435 y = 0;
|
|
436 z = 1;
|
|
437 }
|
|
438 else if (arguments.length == 3) {
|
|
439 this.rotate(angle, 1,0,0); // about X axis
|
|
440 this.rotate(x, 0,1,0); // about Y axis
|
|
441 this.rotate(y, 0,0,1); // about Z axis
|
|
442 return;
|
|
443 }
|
|
444 }
|
|
445
|
|
446 if (J3DIHasCSSMatrix) {
|
|
447 this.$matrix = this.$matrix.rotateAxisAngle(x, y, z, angle);
|
|
448 return;
|
|
449 }
|
|
450
|
|
451 // angles are in degrees. Switch to radians
|
|
452 angle = angle / 180 * Math.PI;
|
|
453
|
|
454 angle /= 2;
|
|
455 var sinA = Math.sin(angle);
|
|
456 var cosA = Math.cos(angle);
|
|
457 var sinA2 = sinA * sinA;
|
|
458
|
|
459 // normalize
|
|
460 var len = Math.sqrt(x * x + y * y + z * z);
|
|
461 if (len == 0) {
|
|
462 // bad vector, just use something reasonable
|
|
463 x = 0;
|
|
464 y = 0;
|
|
465 z = 1;
|
|
466 } else if (len != 1) {
|
|
467 x /= len;
|
|
468 y /= len;
|
|
469 z /= len;
|
|
470 }
|
|
471
|
|
472 var mat = new J3DIMatrix4();
|
|
473
|
|
474 // optimize case where axis is along major axis
|
|
475 if (x == 1 && y == 0 && z == 0) {
|
|
476 mat.$matrix.m11 = 1;
|
|
477 mat.$matrix.m12 = 0;
|
|
478 mat.$matrix.m13 = 0;
|
|
479 mat.$matrix.m21 = 0;
|
|
480 mat.$matrix.m22 = 1 - 2 * sinA2;
|
|
481 mat.$matrix.m23 = 2 * sinA * cosA;
|
|
482 mat.$matrix.m31 = 0;
|
|
483 mat.$matrix.m32 = -2 * sinA * cosA;
|
|
484 mat.$matrix.m33 = 1 - 2 * sinA2;
|
|
485 mat.$matrix.m14 = mat.$matrix.m24 = mat.$matrix.m34 = 0;
|
|
486 mat.$matrix.m41 = mat.$matrix.m42 = mat.$matrix.m43 = 0;
|
|
487 mat.$matrix.m44 = 1;
|
|
488 } else if (x == 0 && y == 1 && z == 0) {
|
|
489 mat.$matrix.m11 = 1 - 2 * sinA2;
|
|
490 mat.$matrix.m12 = 0;
|
|
491 mat.$matrix.m13 = -2 * sinA * cosA;
|
|
492 mat.$matrix.m21 = 0;
|
|
493 mat.$matrix.m22 = 1;
|
|
494 mat.$matrix.m23 = 0;
|
|
495 mat.$matrix.m31 = 2 * sinA * cosA;
|
|
496 mat.$matrix.m32 = 0;
|
|
497 mat.$matrix.m33 = 1 - 2 * sinA2;
|
|
498 mat.$matrix.m14 = mat.$matrix.m24 = mat.$matrix.m34 = 0;
|
|
499 mat.$matrix.m41 = mat.$matrix.m42 = mat.$matrix.m43 = 0;
|
|
500 mat.$matrix.m44 = 1;
|
|
501 } else if (x == 0 && y == 0 && z == 1) {
|
|
502 mat.$matrix.m11 = 1 - 2 * sinA2;
|
|
503 mat.$matrix.m12 = 2 * sinA * cosA;
|
|
504 mat.$matrix.m13 = 0;
|
|
505 mat.$matrix.m21 = -2 * sinA * cosA;
|
|
506 mat.$matrix.m22 = 1 - 2 * sinA2;
|
|
507 mat.$matrix.m23 = 0;
|
|
508 mat.$matrix.m31 = 0;
|
|
509 mat.$matrix.m32 = 0;
|
|
510 mat.$matrix.m33 = 1;
|
|
511 mat.$matrix.m14 = mat.$matrix.m24 = mat.$matrix.m34 = 0;
|
|
512 mat.$matrix.m41 = mat.$matrix.m42 = mat.$matrix.m43 = 0;
|
|
513 mat.$matrix.m44 = 1;
|
|
514 } else {
|
|
515 var x2 = x*x;
|
|
516 var y2 = y*y;
|
|
517 var z2 = z*z;
|
|
518
|
|
519 mat.$matrix.m11 = 1 - 2 * (y2 + z2) * sinA2;
|
|
520 mat.$matrix.m12 = 2 * (x * y * sinA2 + z * sinA * cosA);
|
|
521 mat.$matrix.m13 = 2 * (x * z * sinA2 - y * sinA * cosA);
|
|
522 mat.$matrix.m21 = 2 * (y * x * sinA2 - z * sinA * cosA);
|
|
523 mat.$matrix.m22 = 1 - 2 * (z2 + x2) * sinA2;
|
|
524 mat.$matrix.m23 = 2 * (y * z * sinA2 + x * sinA * cosA);
|
|
525 mat.$matrix.m31 = 2 * (z * x * sinA2 + y * sinA * cosA);
|
|
526 mat.$matrix.m32 = 2 * (z * y * sinA2 - x * sinA * cosA);
|
|
527 mat.$matrix.m33 = 1 - 2 * (x2 + y2) * sinA2;
|
|
528 mat.$matrix.m14 = mat.$matrix.m24 = mat.$matrix.m34 = 0;
|
|
529 mat.$matrix.m41 = mat.$matrix.m42 = mat.$matrix.m43 = 0;
|
|
530 mat.$matrix.m44 = 1;
|
|
531 }
|
|
532 this.multiply(mat);
|
|
533 }
|
|
534
|
|
535 J3DIMatrix4.prototype.multiply = function(mat)
|
|
536 {
|
|
537 if (J3DIHasCSSMatrix) {
|
|
538 this.$matrix = this.$matrix.multiply(mat.$matrix);
|
|
539 return;
|
|
540 }
|
|
541
|
|
542 var m11 = (mat.$matrix.m11 * this.$matrix.m11 + mat.$matrix.m12 * this.$matrix.m21
|
|
543 + mat.$matrix.m13 * this.$matrix.m31 + mat.$matrix.m14 * this.$matrix.m41);
|
|
544 var m12 = (mat.$matrix.m11 * this.$matrix.m12 + mat.$matrix.m12 * this.$matrix.m22
|
|
545 + mat.$matrix.m13 * this.$matrix.m32 + mat.$matrix.m14 * this.$matrix.m42);
|
|
546 var m13 = (mat.$matrix.m11 * this.$matrix.m13 + mat.$matrix.m12 * this.$matrix.m23
|
|
547 + mat.$matrix.m13 * this.$matrix.m33 + mat.$matrix.m14 * this.$matrix.m43);
|
|
548 var m14 = (mat.$matrix.m11 * this.$matrix.m14 + mat.$matrix.m12 * this.$matrix.m24
|
|
549 + mat.$matrix.m13 * this.$matrix.m34 + mat.$matrix.m14 * this.$matrix.m44);
|
|
550
|
|
551 var m21 = (mat.$matrix.m21 * this.$matrix.m11 + mat.$matrix.m22 * this.$matrix.m21
|
|
552 + mat.$matrix.m23 * this.$matrix.m31 + mat.$matrix.m24 * this.$matrix.m41);
|
|
553 var m22 = (mat.$matrix.m21 * this.$matrix.m12 + mat.$matrix.m22 * this.$matrix.m22
|
|
554 + mat.$matrix.m23 * this.$matrix.m32 + mat.$matrix.m24 * this.$matrix.m42);
|
|
555 var m23 = (mat.$matrix.m21 * this.$matrix.m13 + mat.$matrix.m22 * this.$matrix.m23
|
|
556 + mat.$matrix.m23 * this.$matrix.m33 + mat.$matrix.m24 * this.$matrix.m43);
|
|
557 var m24 = (mat.$matrix.m21 * this.$matrix.m14 + mat.$matrix.m22 * this.$matrix.m24
|
|
558 + mat.$matrix.m23 * this.$matrix.m34 + mat.$matrix.m24 * this.$matrix.m44);
|
|
559
|
|
560 var m31 = (mat.$matrix.m31 * this.$matrix.m11 + mat.$matrix.m32 * this.$matrix.m21
|
|
561 + mat.$matrix.m33 * this.$matrix.m31 + mat.$matrix.m34 * this.$matrix.m41);
|
|
562 var m32 = (mat.$matrix.m31 * this.$matrix.m12 + mat.$matrix.m32 * this.$matrix.m22
|
|
563 + mat.$matrix.m33 * this.$matrix.m32 + mat.$matrix.m34 * this.$matrix.m42);
|
|
564 var m33 = (mat.$matrix.m31 * this.$matrix.m13 + mat.$matrix.m32 * this.$matrix.m23
|
|
565 + mat.$matrix.m33 * this.$matrix.m33 + mat.$matrix.m34 * this.$matrix.m43);
|
|
566 var m34 = (mat.$matrix.m31 * this.$matrix.m14 + mat.$matrix.m32 * this.$matrix.m24
|
|
567 + mat.$matrix.m33 * this.$matrix.m34 + mat.$matrix.m34 * this.$matrix.m44);
|
|
568
|
|
569 var m41 = (mat.$matrix.m41 * this.$matrix.m11 + mat.$matrix.m42 * this.$matrix.m21
|
|
570 + mat.$matrix.m43 * this.$matrix.m31 + mat.$matrix.m44 * this.$matrix.m41);
|
|
571 var m42 = (mat.$matrix.m41 * this.$matrix.m12 + mat.$matrix.m42 * this.$matrix.m22
|
|
572 + mat.$matrix.m43 * this.$matrix.m32 + mat.$matrix.m44 * this.$matrix.m42);
|
|
573 var m43 = (mat.$matrix.m41 * this.$matrix.m13 + mat.$matrix.m42 * this.$matrix.m23
|
|
574 + mat.$matrix.m43 * this.$matrix.m33 + mat.$matrix.m44 * this.$matrix.m43);
|
|
575 var m44 = (mat.$matrix.m41 * this.$matrix.m14 + mat.$matrix.m42 * this.$matrix.m24
|
|
576 + mat.$matrix.m43 * this.$matrix.m34 + mat.$matrix.m44 * this.$matrix.m44);
|
|
577
|
|
578 this.$matrix.m11 = m11;
|
|
579 this.$matrix.m12 = m12;
|
|
580 this.$matrix.m13 = m13;
|
|
581 this.$matrix.m14 = m14;
|
|
582
|
|
583 this.$matrix.m21 = m21;
|
|
584 this.$matrix.m22 = m22;
|
|
585 this.$matrix.m23 = m23;
|
|
586 this.$matrix.m24 = m24;
|
|
587
|
|
588 this.$matrix.m31 = m31;
|
|
589 this.$matrix.m32 = m32;
|
|
590 this.$matrix.m33 = m33;
|
|
591 this.$matrix.m34 = m34;
|
|
592
|
|
593 this.$matrix.m41 = m41;
|
|
594 this.$matrix.m42 = m42;
|
|
595 this.$matrix.m43 = m43;
|
|
596 this.$matrix.m44 = m44;
|
|
597 }
|
|
598
|
|
599 J3DIMatrix4.prototype.divide = function(divisor)
|
|
600 {
|
|
601 this.$matrix.m11 /= divisor;
|
|
602 this.$matrix.m12 /= divisor;
|
|
603 this.$matrix.m13 /= divisor;
|
|
604 this.$matrix.m14 /= divisor;
|
|
605
|
|
606 this.$matrix.m21 /= divisor;
|
|
607 this.$matrix.m22 /= divisor;
|
|
608 this.$matrix.m23 /= divisor;
|
|
609 this.$matrix.m24 /= divisor;
|
|
610
|
|
611 this.$matrix.m31 /= divisor;
|
|
612 this.$matrix.m32 /= divisor;
|
|
613 this.$matrix.m33 /= divisor;
|
|
614 this.$matrix.m34 /= divisor;
|
|
615
|
|
616 this.$matrix.m41 /= divisor;
|
|
617 this.$matrix.m42 /= divisor;
|
|
618 this.$matrix.m43 /= divisor;
|
|
619 this.$matrix.m44 /= divisor;
|
|
620
|
|
621 }
|
|
622
|
|
623 J3DIMatrix4.prototype.ortho = function(left, right, bottom, top, near, far)
|
|
624 {
|
|
625 var tx = (left + right) / (left - right);
|
|
626 var ty = (top + bottom) / (top - bottom);
|
|
627 var tz = (far + near) / (far - near);
|
|
628
|
|
629 var matrix = new J3DIMatrix4();
|
|
630 matrix.$matrix.m11 = 2 / (left - right);
|
|
631 matrix.$matrix.m12 = 0;
|
|
632 matrix.$matrix.m13 = 0;
|
|
633 matrix.$matrix.m14 = 0;
|
|
634 matrix.$matrix.m21 = 0;
|
|
635 matrix.$matrix.m22 = 2 / (top - bottom);
|
|
636 matrix.$matrix.m23 = 0;
|
|
637 matrix.$matrix.m24 = 0;
|
|
638 matrix.$matrix.m31 = 0;
|
|
639 matrix.$matrix.m32 = 0;
|
|
640 matrix.$matrix.m33 = -2 / (far - near);
|
|
641 matrix.$matrix.m34 = 0;
|
|
642 matrix.$matrix.m41 = tx;
|
|
643 matrix.$matrix.m42 = ty;
|
|
644 matrix.$matrix.m43 = tz;
|
|
645 matrix.$matrix.m44 = 1;
|
|
646
|
|
647 this.multiply(matrix);
|
|
648 }
|
|
649
|
|
650 J3DIMatrix4.prototype.frustum = function(left, right, bottom, top, near, far)
|
|
651 {
|
|
652 var matrix = new J3DIMatrix4();
|
|
653 var A = (right + left) / (right - left);
|
|
654 var B = (top + bottom) / (top - bottom);
|
|
655 var C = -(far + near) / (far - near);
|
|
656 var D = -(2 * far * near) / (far - near);
|
|
657
|
|
658 matrix.$matrix.m11 = (2 * near) / (right - left);
|
|
659 matrix.$matrix.m12 = 0;
|
|
660 matrix.$matrix.m13 = 0;
|
|
661 matrix.$matrix.m14 = 0;
|
|
662
|
|
663 matrix.$matrix.m21 = 0;
|
|
664 matrix.$matrix.m22 = 2 * near / (top - bottom);
|
|
665 matrix.$matrix.m23 = 0;
|
|
666 matrix.$matrix.m24 = 0;
|
|
667
|
|
668 matrix.$matrix.m31 = A;
|
|
669 matrix.$matrix.m32 = B;
|
|
670 matrix.$matrix.m33 = C;
|
|
671 matrix.$matrix.m34 = -1;
|
|
672
|
|
673 matrix.$matrix.m41 = 0;
|
|
674 matrix.$matrix.m42 = 0;
|
|
675 matrix.$matrix.m43 = D;
|
|
676 matrix.$matrix.m44 = 0;
|
|
677
|
|
678 this.multiply(matrix);
|
|
679 }
|
|
680
|
|
681 J3DIMatrix4.prototype.perspective = function(fovy, aspect, zNear, zFar)
|
|
682 {
|
|
683 var top = Math.tan(fovy * Math.PI / 360) * zNear;
|
|
684 var bottom = -top;
|
|
685 var left = aspect * bottom;
|
|
686 var right = aspect * top;
|
|
687 this.frustum(left, right, bottom, top, zNear, zFar);
|
|
688 }
|
|
689
|
|
690 J3DIMatrix4.prototype.lookat = function(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)
|
|
691 {
|
|
692 if (typeof eyez == 'object' && "length" in eyez) {
|
|
693 var t = eyez;
|
|
694 upx = t[0];
|
|
695 upy = t[1];
|
|
696 upz = t[2];
|
|
697
|
|
698 t = eyey;
|
|
699 centerx = t[0];
|
|
700 centery = t[1];
|
|
701 centerz = t[2];
|
|
702
|
|
703 t = eyex;
|
|
704 eyex = t[0];
|
|
705 eyey = t[1];
|
|
706 eyez = t[2];
|
|
707 }
|
|
708
|
|
709 var matrix = new J3DIMatrix4();
|
|
710
|
|
711 // Make rotation matrix
|
|
712
|
|
713 // Z vector
|
|
714 var zx = eyex - centerx;
|
|
715 var zy = eyey - centery;
|
|
716 var zz = eyez - centerz;
|
|
717 var mag = Math.sqrt(zx * zx + zy * zy + zz * zz);
|
|
718 if (mag) {
|
|
719 zx /= mag;
|
|
720 zy /= mag;
|
|
721 zz /= mag;
|
|
722 }
|
|
723
|
|
724 // Y vector
|
|
725 var yx = upx;
|
|
726 var yy = upy;
|
|
727 var yz = upz;
|
|
728
|
|
729 // X vector = Y cross Z
|
|
730 xx = yy * zz - yz * zy;
|
|
731 xy = -yx * zz + yz * zx;
|
|
732 xz = yx * zy - yy * zx;
|
|
733
|
|
734 // Recompute Y = Z cross X
|
|
735 yx = zy * xz - zz * xy;
|
|
736 yy = -zx * xz + zz * xx;
|
|
737 yx = zx * xy - zy * xx;
|
|
738
|
|
739 // cross product gives area of parallelogram, which is < 1.0 for
|
|
740 // non-perpendicular unit-length vectors; so normalize x, y here
|
|
741
|
|
742 mag = Math.sqrt(xx * xx + xy * xy + xz * xz);
|
|
743 if (mag) {
|
|
744 xx /= mag;
|
|
745 xy /= mag;
|
|
746 xz /= mag;
|
|
747 }
|
|
748
|
|
749 mag = Math.sqrt(yx * yx + yy * yy + yz * yz);
|
|
750 if (mag) {
|
|
751 yx /= mag;
|
|
752 yy /= mag;
|
|
753 yz /= mag;
|
|
754 }
|
|
755
|
|
756 matrix.$matrix.m11 = xx;
|
|
757 matrix.$matrix.m12 = xy;
|
|
758 matrix.$matrix.m13 = xz;
|
|
759 matrix.$matrix.m14 = 0;
|
|
760
|
|
761 matrix.$matrix.m21 = yx;
|
|
762 matrix.$matrix.m22 = yy;
|
|
763 matrix.$matrix.m23 = yz;
|
|
764 matrix.$matrix.m24 = 0;
|
|
765
|
|
766 matrix.$matrix.m31 = zx;
|
|
767 matrix.$matrix.m32 = zy;
|
|
768 matrix.$matrix.m33 = zz;
|
|
769 matrix.$matrix.m34 = 0;
|
|
770
|
|
771 matrix.$matrix.m41 = 0;
|
|
772 matrix.$matrix.m42 = 0;
|
|
773 matrix.$matrix.m43 = 0;
|
|
774 matrix.$matrix.m44 = 1;
|
|
775 matrix.translate(-eyex, -eyey, -eyez);
|
|
776
|
|
777 this.multiply(matrix);
|
|
778 }
|
|
779
|
|
780 // Returns true on success, false otherwise. All params are Array objects
|
|
781 J3DIMatrix4.prototype.decompose = function(_translate, _rotate, _scale, _skew, _perspective)
|
|
782 {
|
|
783 // Normalize the matrix.
|
|
784 if (this.$matrix.m44 == 0)
|
|
785 return false;
|
|
786
|
|
787 // Gather the params
|
|
788 var translate, rotate, scale, skew, perspective;
|
|
789
|
|
790 var translate = (_translate == undefined || !("length" in _translate)) ? new J3DIVector3 : _translate;
|
|
791 var rotate = (_rotate == undefined || !("length" in _rotate)) ? new J3DIVector3 : _rotate;
|
|
792 var scale = (_scale == undefined || !("length" in _scale)) ? new J3DIVector3 : _scale;
|
|
793 var skew = (_skew == undefined || !("length" in _skew)) ? new J3DIVector3 : _skew;
|
|
794 var perspective = (_perspective == undefined || !("length" in _perspective)) ? new Array(4) : _perspective;
|
|
795
|
|
796 var matrix = new J3DIMatrix4(this);
|
|
797
|
|
798 matrix.divide(matrix.$matrix.m44);
|
|
799
|
|
800 // perspectiveMatrix is used to solve for perspective, but it also provides
|
|
801 // an easy way to test for singularity of the upper 3x3 component.
|
|
802 var perspectiveMatrix = new J3DIMatrix4(matrix);
|
|
803
|
|
804 perspectiveMatrix.$matrix.m14 = 0;
|
|
805 perspectiveMatrix.$matrix.m24 = 0;
|
|
806 perspectiveMatrix.$matrix.m34 = 0;
|
|
807 perspectiveMatrix.$matrix.m44 = 1;
|
|
808
|
|
809 if (perspectiveMatrix._determinant4x4() == 0)
|
|
810 return false;
|
|
811
|
|
812 // First, isolate perspective.
|
|
813 if (matrix.$matrix.m14 != 0 || matrix.$matrix.m24 != 0 || matrix.$matrix.m34 != 0) {
|
|
814 // rightHandSide is the right hand side of the equation.
|
|
815 var rightHandSide = [ matrix.$matrix.m14, matrix.$matrix.m24, matrix.$matrix.m34, matrix.$matrix.m44 ];
|
|
816
|
|
817 // Solve the equation by inverting perspectiveMatrix and multiplying
|
|
818 // rightHandSide by the inverse.
|
|
819 var inversePerspectiveMatrix = new J3DIMatrix4(perspectiveMatrix);
|
|
820 inversePerspectiveMatrix.invert();
|
|
821 var transposedInversePerspectiveMatrix = new J3DIMatrix4(inversePerspectiveMatrix);
|
|
822 transposedInversePerspectiveMatrix.transpose();
|
|
823 transposedInversePerspectiveMatrix.multVecMatrix(perspective, rightHandSide);
|
|
824
|
|
825 // Clear the perspective partition
|
|
826 matrix.$matrix.m14 = matrix.$matrix.m24 = matrix.$matrix.m34 = 0
|
|
827 matrix.$matrix.m44 = 1;
|
|
828 }
|
|
829 else {
|
|
830 // No perspective.
|
|
831 perspective[0] = perspective[1] = perspective[2] = 0;
|
|
832 perspective[3] = 1;
|
|
833 }
|
|
834
|
|
835 // Next take care of translation
|
|
836 translate[0] = matrix.$matrix.m41
|
|
837 matrix.$matrix.m41 = 0
|
|
838 translate[1] = matrix.$matrix.m42
|
|
839 matrix.$matrix.m42 = 0
|
|
840 translate[2] = matrix.$matrix.m43
|
|
841 matrix.$matrix.m43 = 0
|
|
842
|
|
843 // Now get scale and shear. 'row' is a 3 element array of 3 component vectors
|
|
844 var row0 = new J3DIVector3(matrix.$matrix.m11, matrix.$matrix.m12, matrix.$matrix.m13);
|
|
845 var row1 = new J3DIVector3(matrix.$matrix.m21, matrix.$matrix.m22, matrix.$matrix.m23);
|
|
846 var row2 = new J3DIVector3(matrix.$matrix.m31, matrix.$matrix.m32, matrix.$matrix.m33);
|
|
847
|
|
848 // Compute X scale factor and normalize first row.
|
|
849 scale[0] = row0.vectorLength();
|
|
850 row0.divide(scale[0]);
|
|
851
|
|
852 // Compute XY shear factor and make 2nd row orthogonal to 1st.
|
|
853 skew[0] = row0.dot(row1);
|
|
854 row1.combine(row0, 1.0, -skew[0]);
|
|
855
|
|
856 // Now, compute Y scale and normalize 2nd row.
|
|
857 scale[1] = row1.vectorLength();
|
|
858 row1.divide(scale[1]);
|
|
859 skew[0] /= scale[1];
|
|
860
|
|
861 // Compute XZ and YZ shears, orthogonalize 3rd row
|
|
862 skew[1] = row1.dot(row2);
|
|
863 row2.combine(row0, 1.0, -skew[1]);
|
|
864 skew[2] = row1.dot(row2);
|
|
865 row2.combine(row1, 1.0, -skew[2]);
|
|
866
|
|
867 // Next, get Z scale and normalize 3rd row.
|
|
868 scale[2] = row2.vectorLength();
|
|
869 row2.divide(scale[2]);
|
|
870 skew[1] /= scale[2];
|
|
871 skew[2] /= scale[2];
|
|
872
|
|
873 // At this point, the matrix (in rows) is orthonormal.
|
|
874 // Check for a coordinate system flip. If the determinant
|
|
875 // is -1, then negate the matrix and the scaling factors.
|
|
876 var pdum3 = new J3DIVector3(row1);
|
|
877 pdum3.cross(row2);
|
|
878 if (row0.dot(pdum3) < 0) {
|
|
879 for (i = 0; i < 3; i++) {
|
|
880 scale[i] *= -1;
|
|
881 row[0][i] *= -1;
|
|
882 row[1][i] *= -1;
|
|
883 row[2][i] *= -1;
|
|
884 }
|
|
885 }
|
|
886
|
|
887 // Now, get the rotations out
|
|
888 rotate[1] = Math.asin(-row0[2]);
|
|
889 if (Math.cos(rotate[1]) != 0) {
|
|
890 rotate[0] = Math.atan2(row1[2], row2[2]);
|
|
891 rotate[2] = Math.atan2(row0[1], row0[0]);
|
|
892 }
|
|
893 else {
|
|
894 rotate[0] = Math.atan2(-row2[0], row1[1]);
|
|
895 rotate[2] = 0;
|
|
896 }
|
|
897
|
|
898 // Convert rotations to degrees
|
|
899 var rad2deg = 180 / Math.PI;
|
|
900 rotate[0] *= rad2deg;
|
|
901 rotate[1] *= rad2deg;
|
|
902 rotate[2] *= rad2deg;
|
|
903
|
|
904 return true;
|
|
905 }
|
|
906
|
|
907 J3DIMatrix4.prototype._determinant2x2 = function(a, b, c, d)
|
|
908 {
|
|
909 return a * d - b * c;
|
|
910 }
|
|
911
|
|
912 J3DIMatrix4.prototype._determinant3x3 = function(a1, a2, a3, b1, b2, b3, c1, c2, c3)
|
|
913 {
|
|
914 return a1 * this._determinant2x2(b2, b3, c2, c3)
|
|
915 - b1 * this._determinant2x2(a2, a3, c2, c3)
|
|
916 + c1 * this._determinant2x2(a2, a3, b2, b3);
|
|
917 }
|
|
918
|
|
919 J3DIMatrix4.prototype._determinant4x4 = function()
|
|
920 {
|
|
921 var a1 = this.$matrix.m11;
|
|
922 var b1 = this.$matrix.m12;
|
|
923 var c1 = this.$matrix.m13;
|
|
924 var d1 = this.$matrix.m14;
|
|
925
|
|
926 var a2 = this.$matrix.m21;
|
|
927 var b2 = this.$matrix.m22;
|
|
928 var c2 = this.$matrix.m23;
|
|
929 var d2 = this.$matrix.m24;
|
|
930
|
|
931 var a3 = this.$matrix.m31;
|
|
932 var b3 = this.$matrix.m32;
|
|
933 var c3 = this.$matrix.m33;
|
|
934 var d3 = this.$matrix.m34;
|
|
935
|
|
936 var a4 = this.$matrix.m41;
|
|
937 var b4 = this.$matrix.m42;
|
|
938 var c4 = this.$matrix.m43;
|
|
939 var d4 = this.$matrix.m44;
|
|
940
|
|
941 return a1 * this._determinant3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4)
|
|
942 - b1 * this._determinant3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4)
|
|
943 + c1 * this._determinant3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4)
|
|
944 - d1 * this._determinant3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4);
|
|
945 }
|
|
946
|
|
947 J3DIMatrix4.prototype._makeAdjoint = function()
|
|
948 {
|
|
949 var a1 = this.$matrix.m11;
|
|
950 var b1 = this.$matrix.m12;
|
|
951 var c1 = this.$matrix.m13;
|
|
952 var d1 = this.$matrix.m14;
|
|
953
|
|
954 var a2 = this.$matrix.m21;
|
|
955 var b2 = this.$matrix.m22;
|
|
956 var c2 = this.$matrix.m23;
|
|
957 var d2 = this.$matrix.m24;
|
|
958
|
|
959 var a3 = this.$matrix.m31;
|
|
960 var b3 = this.$matrix.m32;
|
|
961 var c3 = this.$matrix.m33;
|
|
962 var d3 = this.$matrix.m34;
|
|
963
|
|
964 var a4 = this.$matrix.m41;
|
|
965 var b4 = this.$matrix.m42;
|
|
966 var c4 = this.$matrix.m43;
|
|
967 var d4 = this.$matrix.m44;
|
|
968
|
|
969 // Row column labeling reversed since we transpose rows & columns
|
|
970 this.$matrix.m11 = this._determinant3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4);
|
|
971 this.$matrix.m21 = - this._determinant3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4);
|
|
972 this.$matrix.m31 = this._determinant3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4);
|
|
973 this.$matrix.m41 = - this._determinant3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4);
|
|
974
|
|
975 this.$matrix.m12 = - this._determinant3x3(b1, b3, b4, c1, c3, c4, d1, d3, d4);
|
|
976 this.$matrix.m22 = this._determinant3x3(a1, a3, a4, c1, c3, c4, d1, d3, d4);
|
|
977 this.$matrix.m32 = - this._determinant3x3(a1, a3, a4, b1, b3, b4, d1, d3, d4);
|
|
978 this.$matrix.m42 = this._determinant3x3(a1, a3, a4, b1, b3, b4, c1, c3, c4);
|
|
979
|
|
980 this.$matrix.m13 = this._determinant3x3(b1, b2, b4, c1, c2, c4, d1, d2, d4);
|
|
981 this.$matrix.m23 = - this._determinant3x3(a1, a2, a4, c1, c2, c4, d1, d2, d4);
|
|
982 this.$matrix.m33 = this._determinant3x3(a1, a2, a4, b1, b2, b4, d1, d2, d4);
|
|
983 this.$matrix.m43 = - this._determinant3x3(a1, a2, a4, b1, b2, b4, c1, c2, c4);
|
|
984
|
|
985 this.$matrix.m14 = - this._determinant3x3(b1, b2, b3, c1, c2, c3, d1, d2, d3);
|
|
986 this.$matrix.m24 = this._determinant3x3(a1, a2, a3, c1, c2, c3, d1, d2, d3);
|
|
987 this.$matrix.m34 = - this._determinant3x3(a1, a2, a3, b1, b2, b3, d1, d2, d3);
|
|
988 this.$matrix.m44 = this._determinant3x3(a1, a2, a3, b1, b2, b3, c1, c2, c3);
|
|
989 }
|
|
990
|
|
991 //
|
|
992 // J3DIVector3
|
|
993 //
|
|
994 J3DIVector3 = function(x,y,z)
|
|
995 {
|
|
996 this.load(x,y,z);
|
|
997 }
|
|
998
|
|
999 J3DIVector3.prototype.load = function(x,y,z)
|
|
1000 {
|
|
1001 if (typeof x == 'object' && "length" in x) {
|
|
1002 this[0] = x[0];
|
|
1003 this[1] = x[1];
|
|
1004 this[2] = x[2];
|
|
1005 }
|
|
1006 else if (typeof x == 'number') {
|
|
1007 this[0] = x;
|
|
1008 this[1] = y;
|
|
1009 this[2] = z;
|
|
1010 }
|
|
1011 else {
|
|
1012 this[0] = 0;
|
|
1013 this[1] = 0;
|
|
1014 this[2] = 0;
|
|
1015 }
|
|
1016 }
|
|
1017
|
|
1018 J3DIVector3.prototype.getAsArray = function()
|
|
1019 {
|
|
1020 return [ this[0], this[1], this[2] ];
|
|
1021 }
|
|
1022
|
|
1023 J3DIVector3.prototype.getAsFloat32Array = function()
|
|
1024 {
|
|
1025 return new Float32Array(this.getAsArray());
|
|
1026 }
|
|
1027
|
|
1028 J3DIVector3.prototype.vectorLength = function()
|
|
1029 {
|
|
1030 return Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]);
|
|
1031 }
|
|
1032
|
|
1033 J3DIVector3.prototype.divide = function(divisor)
|
|
1034 {
|
|
1035 this[0] /= divisor; this[1] /= divisor; this[2] /= divisor;
|
|
1036 }
|
|
1037
|
|
1038 J3DIVector3.prototype.cross = function(v)
|
|
1039 {
|
|
1040 this[0] = this[1] * v[2] - this[2] * v[1];
|
|
1041 this[1] = -this[0] * v[2] + this[2] * v[0];
|
|
1042 this[2] = this[0] * v[1] - this[1] * v[0];
|
|
1043 }
|
|
1044
|
|
1045 J3DIVector3.prototype.dot = function(v)
|
|
1046 {
|
|
1047 return this[0] * v[0] + this[1] * v[1] + this[2] * v[2];
|
|
1048 }
|
|
1049
|
|
1050 J3DIVector3.prototype.combine = function(v, ascl, bscl)
|
|
1051 {
|
|
1052 this[0] = (ascl * this[0]) + (bscl * v[0]);
|
|
1053 this[1] = (ascl * this[1]) + (bscl * v[1]);
|
|
1054 this[2] = (ascl * this[2]) + (bscl * v[2]);
|
|
1055 }
|
|
1056
|
|
1057 J3DIVector3.prototype.multVecMatrix = function(matrix)
|
|
1058 {
|
|
1059 var x = this[0];
|
|
1060 var y = this[1];
|
|
1061 var z = this[2];
|
|
1062
|
|
1063 this[0] = matrix.$matrix.m41 + x * matrix.$matrix.m11 + y * matrix.$matrix.m21 + z * matrix.$matrix.m31;
|
|
1064 this[1] = matrix.$matrix.m42 + x * matrix.$matrix.m12 + y * matrix.$matrix.m22 + z * matrix.$matrix.m32;
|
|
1065 this[2] = matrix.$matrix.m43 + x * matrix.$matrix.m13 + y * matrix.$matrix.m23 + z * matrix.$matrix.m33;
|
|
1066 var w = matrix.$matrix.m44 + x * matrix.$matrix.m14 + y * matrix.$matrix.m24 + z * matrix.$matrix.m34;
|
|
1067 if (w != 1 && w != 0) {
|
|
1068 this[0] /= w;
|
|
1069 this[1] /= w;
|
|
1070 this[2] /= w;
|
|
1071 }
|
|
1072 }
|
|
1073
|
|
1074 J3DIVector3.prototype.toString = function()
|
|
1075 {
|
|
1076 return "["+this[0]+","+this[1]+","+this[2]+"]";
|
|
1077 }
|