Skip to content

Commit

Permalink
Upgrade to latest gl-matrix r235 - NOTE translation is now always alo…
Browse files Browse the repository at this point in the history
…ng global axis - not object local. Fixed up tests to reflect changes.
  • Loading branch information
kevinroast committed Feb 11, 2014
1 parent 574b097 commit 6d0452d
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 121 deletions.
2 changes: 1 addition & 1 deletion scripts/gl-matrix-min.js

Large diffs are not rendered by default.

202 changes: 106 additions & 96 deletions scripts/gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ glMatrix.setMatrixArrayType = function(type) {
if(typeof(exports) !== 'undefined') {
exports.glMatrix = glMatrix;
}
;

var degree = Math.PI / 180;

/**
* Convert Degree To Radian
*
* @param {Number} Angle in Degrees
*/
glMatrix.toRadian = function(a){
return a * degree;
};

/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -2564,26 +2575,26 @@ mat3.fromQuat = function (out, q) {
z2 = z + z,

xx = x * x2,
xy = x * y2,
xz = x * z2,
yx = y * x2,
yy = y * y2,
yz = y * z2,
zx = z * x2,
zy = z * y2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2;

out[0] = 1 - (yy + zz);
out[3] = xy + wz;
out[6] = xz - wy;
out[0] = 1 - yy - zz;
out[3] = yx - wz;
out[6] = zx + wy;

out[1] = xy - wz;
out[4] = 1 - (xx + zz);
out[7] = yz + wx;
out[1] = yx + wz;
out[4] = 1 - xx - zz;
out[7] = zy - wx;

out[2] = xz + wy;
out[5] = yz - wx;
out[8] = 1 - (xx + yy);
out[2] = zx - wy;
out[5] = zy + wx;
out[8] = 1 - xx - yy;

return out;
};
Expand Down Expand Up @@ -3011,31 +3022,35 @@ mat4.translate = function (out, a, v) {
var x = v[0], y = v[1], z = v[2],
a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23;
a20, a21, a22, a23,
a30, a31, a32, a33;

if (a === out) {
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
} else {
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];

out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;

out[12] = a00 * x + a10 * y + a20 * z + a[12];
out[13] = a01 * x + a11 * y + a21 * z + a[13];
out[14] = a02 * x + a12 * y + a22 * z + a[14];
out[15] = a03 * x + a13 * y + a23 * z + a[15];
}
a30 = a[12]; a31 = a[13]; a32 = a[14]; a33 = a[15];

out[0] = a00 + a03*x;
out[1] = a01 + a03*y;
out[2] = a02 + a03*z;
out[3] = a03;

out[4] = a10 + a13*x;
out[5] = a11 + a13*y;
out[6] = a12 + a13*z;
out[7] = a13;

out[8] = a20 + a23*x;
out[9] = a21 + a23*y;
out[10] = a22 + a23*z;
out[11] = a23;
out[12] = a30 + a33*x;
out[13] = a31 + a33*y;
out[14] = a32 + a33*z;
out[15] = a33;

return out;
};

/**
* Scales the mat4 by the dimensions in the given vec3
*
Expand Down Expand Up @@ -3310,43 +3325,35 @@ mat4.fromRotationTranslation = function (out, q, v) {
return out;
};

/**
* Calculates a 4x4 matrix from the given quaternion
*
* @param {mat4} out mat4 receiving operation result
* @param {quat} q Quaternion to create matrix from
*
* @returns {mat4} out
*/
mat4.fromQuat = function (out, q) {
var x = q[0], y = q[1], z = q[2], w = q[3],
x2 = x + x,
y2 = y + y,
z2 = z + z,

xx = x * x2,
xy = x * y2,
xz = x * z2,
yx = y * x2,
yy = y * y2,
yz = y * z2,
zx = z * x2,
zy = z * y2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2;

out[0] = 1 - (yy + zz);
out[1] = xy + wz;
out[2] = xz - wy;
out[0] = 1 - yy - zz;
out[1] = yx + wz;
out[2] = zx - wy;
out[3] = 0;

out[4] = xy - wz;
out[5] = 1 - (xx + zz);
out[6] = yz + wx;
out[4] = yx - wz;
out[5] = 1 - xx - zz;
out[6] = zy + wx;
out[7] = 0;

out[8] = xz + wy;
out[9] = yz - wx;
out[10] = 1 - (xx + yy);
out[8] = zx + wy;
out[9] = zy - wx;
out[10] = 1 - xx - yy;
out[11] = 0;

out[12] = 0;
Expand Down Expand Up @@ -3670,9 +3677,9 @@ quat.setAxes = (function() {
matr[4] = up[1];
matr[7] = up[2];

matr[2] = view[0];
matr[5] = view[1];
matr[8] = view[2];
matr[2] = -view[0];
matr[5] = -view[1];
matr[8] = -view[2];

return quat.normalize(out, quat.fromMat3(out, matr));
};
Expand Down Expand Up @@ -4044,48 +4051,40 @@ quat.normalize = vec4.normalize;
* @returns {quat} out
* @function
*/
quat.fromMat3 = (function() {
// benchmarks:
// http://jsperf.com/typed-array-access-speed
// http://jsperf.com/conversion-of-3x3-matrix-to-quaternion

var s_iNext = (typeof(Int8Array) !== 'undefined' ? new Int8Array([1,2,0]) : [1,2,0]);

return function(out, m) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var fTrace = m[0] + m[4] + m[8];
var fRoot;

if ( fTrace > 0.0 ) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
out[3] = 0.5 * fRoot;
fRoot = 0.5/fRoot; // 1/(4w)
out[0] = (m[7]-m[5])*fRoot;
out[1] = (m[2]-m[6])*fRoot;
out[2] = (m[3]-m[1])*fRoot;
} else {
// |w| <= 1/2
var i = 0;
if ( m[4] > m[0] )
i = 1;
if ( m[8] > m[i*3+i] )
i = 2;
var j = s_iNext[i];
var k = s_iNext[j];

fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
out[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
out[3] = (m[k*3+j] - m[j*3+k]) * fRoot;
out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
}
quat.fromMat3 = function(out, m) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var fTrace = m[0] + m[4] + m[8];
var fRoot;

if ( fTrace > 0.0 ) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
out[3] = 0.5 * fRoot;
fRoot = 0.5/fRoot; // 1/(4w)
out[0] = (m[7]-m[5])*fRoot;
out[1] = (m[2]-m[6])*fRoot;
out[2] = (m[3]-m[1])*fRoot;
} else {
// |w| <= 1/2
var i = 0;
if ( m[4] > m[0] )
i = 1;
if ( m[8] > m[i*3+i] )
i = 2;
var j = (i+1)%3;
var k = (i+2)%3;

return out;
};
})();
fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
out[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
out[3] = (m[k*3+j] - m[j*3+k]) * fRoot;
out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
}

return out;
};

/**
* Returns a string representation of a quatenion
Expand All @@ -4102,6 +4101,17 @@ if(typeof(exports) !== 'undefined') {
}
;













})(shim.exports);
})(this);

2 changes: 1 addition & 1 deletion scripts/phoria-min.js

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions scripts/phoria-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ mat4.fromYPR = function(yaw, pitch, roll) {
return out;
};

quat.fromYPR = function(yaw, pitch, roll) {
var num9 = roll * 0.5;
var num6 = Math.sin(num9);
var num5 = Math.cos(num9);
var num8 = pitch * 0.5;
var num4 = Math.sin(num8);
var num3 = Math.cos(num8);
var num7 = yaw * 0.5;
var num2 = Math.sin(num7);
var num = Math.cos(num7);
var out = new Array(4);
out[0] = ((num * num4) * num5) + ((num2 * num3) * num6);
out[1] = ((num2 * num3) * num5) - ((num * num4) * num6);
out[2] = ((num * num3) * num6) - ((num2 * num4) * num5);
out[3] = ((num * num3) * num5) + ((num2 * num4) * num6);
return out;
};


/**
* Phoria root namespace.
Expand All @@ -95,18 +113,17 @@ mat4.fromYPR = function(yaw, pitch, roll) {
if (typeof Phoria === "undefined" || !Phoria)
{
var Phoria = {};

// Global static Phoria constants
Phoria.RADIANS = Math.PI/180.0;
Phoria.TWOPI = Math.PI*2;
Phoria.ONEOPI = 1.0/Math.PI;
Phoria.PIO2 = Math.PI/2;
Phoria.PIO4 = Math.PI/4;
Phoria.EPSILON = 0.000001;
}


// Global Phoria constants
Phoria.RADIANS = Math.PI/180.0;
Phoria.TWOPI = Math.PI*2;
Phoria.ONEOPI = 1.0/Math.PI;
Phoria.PIO2 = Math.PI/2;
Phoria.PIO4 = Math.PI/4;
Phoria.EPSILON = 0.000001;


(function() {
"use strict";

Expand Down
6 changes: 3 additions & 3 deletions test0p.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
color: [120, 0, 0]
}
});
cube1.scaleN(1.25).rotateY(0.5).translateX(3);
cube1.scaleN(1.25).translateZ(-2).translateX(3);
scene.graph.push(cube1);

var c2 = Phoria.Util.generateUnitCube();
Expand All @@ -73,7 +73,7 @@
color: [0, 0, 120]
}
});
cube2.rotateY(1.5).translateZ(-4);
cube2.translateX(-4).rotateY(1.5);
scene.graph.push(cube2);

var c3 = Phoria.Util.generateUnitCube();
Expand All @@ -86,7 +86,7 @@
color: [0, 120, 0]
}
});
cube3.scaleN(0.7).rotateX(0.8).translateY(3);
cube3.scaleN(0.7).translateY(2).rotateX(0.8);
scene.graph.push(cube3);

// add a light
Expand Down
3 changes: 2 additions & 1 deletion test1t.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
// rotate local matrix of an object
testCube.rotateY(0.5*Phoria.RADIANS);
// translate local matrix of child object - will receive rotation from parent
childObj.identity().rotateX(Math.sin(Date.now() / 1000)*1).translateY(Math.sin(Date.now() / 1000) + 3);
var r = Math.sin(Date.now() / 1000);
childObj.identity().translate(vec3.fromValues(0, r + 3, r * 3)).rotateX(r);
// translate visible light objects around the origin - will rotate child Light emitters
var sine = Math.sin(Date.now() / 500);
blueLightObj.identity().rotateY(rotates[0]+=1*Phoria.RADIANS).translateY(sine);
Expand Down
4 changes: 2 additions & 2 deletions test5.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
position: {x:0.0, y:0.0, z:0.0},
rate: 20,
velocity: {x:0.12, y:0.2, z:0.0},
velocityRnd: {x:0.01, y:0.06, z:0.06},
lifetime: 3000,
velocityRnd: {x:0.02, y:0.07, z:0.07},
lifetime: 5000,
style: {
objectsortmode: "sorted",
shademode: "sprite",
Expand Down
Loading

0 comments on commit 6d0452d

Please sign in to comment.