Skip to content

Instantly share code, notes, and snippets.

@noboko
Created April 14, 2012 06:37
Show Gist options
  • Save noboko/2382494 to your computer and use it in GitHub Desktop.
Save noboko/2382494 to your computer and use it in GitHub Desktop.
//Mat3 for Plask
//Add your plask.js
////////Change your plask.js makeSetter and add exports.Mat3//////////////
function makeSetter(type, loc) {
switch (type) {
case gl.BOOL: // NOTE: bool could be set with 1i or 1f.
case gl.INT:
case gl.SAMPLER_2D:
case gl.SAMPLER_CUBE:
return function(value) {
gl.uniform1i(loc, value);
return this;
};
case gl.FLOAT:
return function(value) {
gl.uniform1f(loc, value);
return this;
};
case gl.FLOAT_VEC2:
return function(v) {
gl.uniform2f(loc, v.x, v.y);
};
case gl.FLOAT_VEC3:
return function(v) {
gl.uniform3f(loc, v.x, v.y, v.z);
};
case gl.FLOAT_VEC4:
return function(v) {
gl.uniform4f(loc, v.x, v.y, v.z, v.w);
};
case gl.FLOAT_MAT3:///add
return function(mat3) {
gl.uniformMatrix3fv(loc, false, mat3.toFloat32Array());
};
case gl.FLOAT_MAT4:
return function(mat4) {
gl.uniformMatrix4fv(loc, false, mat4.toFloat32Array());
};
default:
break;
}
///////////////////////////////////////////////////////////////////
exports.Mat3 = Mat3;
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// 3x3 matrix
// a11 a12 a13
// a21 a22 a23
// a31 a32 a33
///////////////////////////////////////////////////////////////////
function Mat3() {
this.reset();
}
// Set the full 9 elements of the 3x3 matrix, arguments in row major order.
// The elements are specified in row major order.
Mat3.prototype.set3x3r = function(a11, a12, a13, a21, a22, a23,a31, a32, a33) {
this.a11 = a11; this.a12 = a12; this.a13 = a13;
this.a21 = a21; this.a22 = a22; this.a23 = a23;
this.a31 = a31; this.a32 = a32; this.a33 = a33;
return this;
};
// Reset the transform to the identity matrix.
Mat3.prototype.reset = function() {
this.set3x3r(1, 0, 0,
0, 1, 0,
0, 0, 1 );
return this;
};
// Transpose the matrix, rows become columns and columns become rows.
Mat3.prototype.transpose = function() {
var a11 = this.a11, a12 = this.a12, a13 = this.a13,
a21 = this.a21, a22 = this.a22, a23 = this.a23,
a31 = this.a31, a32 = this.a32, a33 = this.a33;
this.a11 = a11; this.a12 = a21; this.a13 = a31;
this.a21 = a12; this.a22 = a22; this.a23 = a32;
this.a31 = a13; this.a32 = a23; this.a33 = a33;
return this;
};
Mat3.prototype.dup = function() {
var m = new Mat3(); // TODO(deanm): This could be better.
m.set3x3r(this.a11, this.a12, this.a13,
this.a21, this.a22, this.a23,
this.a31, this.a32, this.a33);
return m;
};
Mat3.prototype.toFloat32Array = function() {
return new Float32Array([this.a11, this.a21, this.a31,
this.a12, this.a22, this.a32,
this.a13, this.a23, this.a33]);
};
Mat3.prototype.toMat4 = function(){
var m = new Mat4;
m.set4x4r(this.a11, this.a12, this.a13, 0,
this.a21, this.a22, this.a23, 0,
this.a31, this.a32, this.a33, 0,
0, 0, 0, 1);
return m;
};
Mat3.prototype.debugString = function() {
var s = [this.a11, this.a12, this.a13,
this.a21, this.a22, this.a23,
this.a31, this.a32, this.a33,];
var row_lengths = [0, 0, 0];
for (var i = 0; i < 9; ++i) {
s[i] += ''; // Stringify.
var len = s[i].length;
var row = i & 2;
if (row_lengths[row] < len)
row_lengths[row] = len;
}
var out = '';
for (var i = 0; i < 9; ++i) {
var len = s[i].length;
var row_len = row_lengths[i & 2];
var num_spaces = row_len - len;
while (num_spaces--) out += ' ';
out += s[i] + ((i & 2) === 2 ? '\n' : ' ');
}
return out;
};
///////////////////////////////////////////////////////////////////
//add Mat4 methods
///////////////////////////////////////////////////////////////////
////toMat3
Mat4.prototype.toMat3 = function(){
var m = new Mat3;
m.set3x3r(this.a11, this.a12, this.a13,
this.a21, this.a22, this.a23,
this.a31, this.a32, this.a33);
return m;
};
////toInverseMat3
Mat4.prototype.toInverseMat3 = function () {
var m = new Mat3;
var x11 = this.a11, x12 = this.a12, x13 = this.a13,
x21 = this.a21, x22 = this.a22, x23 = this.a23,
x31 = this.a31, x32 = this.a32, x33 = this.a33;
var b11 = x22 * x33 - x23 * x32,
b12 = x32 * x13 - x33 * x12,
b13 = x12 * x23 - x13 * x22,
b21 = x23 * x31 - x21 * x33,
b22 = x33 * x11 - x31 * x13,
b23 = x13 * x21 - x11 * x23,
b31 = x21 * x32 - x22 * x31,
b32 = x31 * x12 - x32 * x11,
b33 = x11 * x22 - x12 * x21;
var det = x11 * b11 + x21 * b12 + x31 * b13;
var invdet;
if (!det){
return console.log('det is zero');
}
else{
invdet = 1 / det;
m.set3x3r(b11 * invdet,
b12 * invdet,
b13 * invdet,
b21 * invdet,
b22 * invdet,
b23 * invdet,
b31 * invdet,
b32 * invdet,
b33 * invdet);
return m;
}
};
//////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment