shader-web-test/algebra.mjs

36 lines
811 B
JavaScript

export function calcRotationMatrix(vec, rad) {
// Equation used from:
// https://people.eecs.berkeley.edu/~ug/slide/pipeline/assignments/as5/rotation.html
let A = glMatrix.mat3.fromValues(
0,
-vec[2],
vec[1],
vec[2],
0,
-vec[0],
-vec[1],
vec[0],
0,
);
let I = glMatrix.mat3.create();
// Matrix Exponential
let Mpow = glMatrix.mat3.create();
glMatrix.mat3.mul(Mpow, A, A);
let M1 = glMatrix.mat3.create();
let M2 = glMatrix.mat3.create();
// Scalar Multiplications
glMatrix.mat3.multiplyScalar(M1, A, Math.sin(rad));
glMatrix.mat3.multiplyScalar(M2, Mpow, 1 - Math.cos(rad));
// Matrix Addition
let M3 = glMatrix.mat3.create();
let Q = glMatrix.mat3.create();
glMatrix.mat3.add(M3, I, M1);
glMatrix.mat3.add(Q, M3, M2);
return Q;
}