Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some new functions #119

Merged
merged 7 commits into from
Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions src/gl-matrix/mat4.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,128 @@ mat4.fromRotationTranslation = function (out, q, v) {
return out;
};

/**
* Creates a matrix from a quaternion rotation, vector translation and vector scale
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, vec);
* var quatMat = mat4.create();
* quat4.toMat4(quat, quatMat);
* mat4.multiply(dest, quatMat);
* mat4.scale(dest, scale)
*
* @param {mat4} out mat4 receiving operation result
* @param {quat4} q Rotation quaternion
* @param {vec3} v Translation vector
* @param {vec3} s Scaling vector
* @returns {mat4} out
*/
mat4.fromRotationTranslationScale = function (out, q, v, s) {
// Quaternion math
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,
yy = y * y2,
yz = y * z2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2,
sx = s[0],
sy = s[1],
sz = s[2];

out[0] = (1 - (yy + zz)) * sx;
out[1] = (xy + wz) * sx;
out[2] = (xz - wy) * sx;
out[3] = 0;
out[4] = (xy - wz) * sy;
out[5] = (1 - (xx + zz)) * sy;
out[6] = (yz + wx) * sy;
out[7] = 0;
out[8] = (xz + wy) * sz;
out[9] = (yz - wx) * sz;
out[10] = (1 - (xx + yy)) * sz;
out[11] = 0;
out[12] = v[0];
out[13] = v[1];
out[14] = v[2];
out[15] = 1;

return out;
};

/**
* Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, vec);
* mat4.translate(dest, origin);
* var quatMat = mat4.create();
* quat4.toMat4(quat, quatMat);
* mat4.multiply(dest, quatMat);
* mat4.scale(dest, scale)
* mat4.translate(dest, negativeOrigin);
*
* @param {mat4} out mat4 receiving operation result
* @param {quat4} q Rotation quaternion
* @param {vec3} v Translation vector
* @param {vec3} s Scaling vector
* @param {vec3} o The origin vector around which to scale and rotate
* @returns {mat4} out
*/
mat4.fromRotationTranslationScaleOrigin = function (out, q, v, s, o) {
// Quaternion math
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,
yy = y * y2,
yz = y * z2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2,

sx = s[0],
sy = s[1],
sz = s[2],

ox = o[0],
oy = o[1],
oz = o[2];

out[0] = (1 - (yy + zz)) * sx;
out[1] = (xy + wz) * sx;
out[2] = (xz - wy) * sx;
out[3] = 0;
out[4] = (xy - wz) * sy;
out[5] = (1 - (xx + zz)) * sy;
out[6] = (yz + wx) * sy;
out[7] = 0;
out[8] = (xz + wy) * sz;
out[9] = (yz - wx) * sz;
out[10] = (1 - (xx + yy)) * sz;
out[11] = 0;
out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz);
out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz);
out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz);
out[15] = 1;

return out;
};

mat4.fromQuat = function (out, q) {
var x = q[0], y = q[1], z = q[2], w = q[3],
x2 = x + x,
Expand Down
24 changes: 24 additions & 0 deletions src/gl-matrix/quat.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,30 @@ quat.slerp = function (out, a, b, t) {
return out;
};

/**
* Performs a spherical linear interpolation with two control points
*
* @param {quat} out the receiving quaternion
* @param {quat} a the first operand
* @param {quat} b the second operand
* @param {quat} c the third operand
* @param {quat} d the fourth operand
* @param {Number} t interpolation amount
* @returns {quat} out
*/
quat.sqlerp = (function () {
var temp1 = quat.create();
var temp2 = quat.create();

return function (out, a, b, c, d, t) {
quat.slerp(temp1, a, d, t);
quat.slerp(temp2, b, c, t);
quat.slerp(out, temp1, temp2, 2 * t * (1 - t));

return out;
};
}());

/**
* Calculates the inverse of a quat
*
Expand Down
52 changes: 52 additions & 0 deletions src/gl-matrix/vec3.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,58 @@ vec3.lerp = function (out, a, b, t) {
return out;
};

/**
* Performs a hermite interpolation with two control points
*
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @param {vec3} c the third operand
* @param {vec3} d the fourth operand
* @param {Number} t interpolation amount between the two inputs
* @returns {vec3} out
*/
vec3.hermite = function (out, a, b, c, d, t) {
var factorTimes2 = t * t,
factor1 = factorTimes2 * (2 * t - 3) + 1,
factor2 = factorTimes2 * (t - 2) + t,
factor3 = factorTimes2 * (t - 1),
factor4 = factorTimes2 * (3 - 2 * t);

out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;

return out;
};

/**
* Performs a bezier interpolation with two control points
*
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @param {vec3} c the third operand
* @param {vec3} d the fourth operand
* @param {Number} t interpolation amount between the two inputs
* @returns {vec3} out
*/
vec3.bezier = function (out, a, b, c, d, t) {
var inverseFactor = 1 - t,
inverseFactorTimesTwo = inverseFactor * inverseFactor,
factorTimes2 = t * t,
factor1 = inverseFactorTimesTwo * inverseFactor,
factor2 = 3 * t * inverseFactorTimesTwo,
factor3 = 3 * factorTimes2 * inverseFactor,
factor4 = factorTimes2 * t;

out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;

return out;
};

/**
* Generates a random vector with the given scale
*
Expand Down