aboutsummaryrefslogtreecommitdiff
path: root/js/lib/math/matrix.js
blob: 77a0966da162afbd1e6d8baef7ba39d9d92e3c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Matrix = function Matrix() {

};

Matrix.create = function( rowArray ) {
	var mat = Matrix.I(4);
	var index = 0;
	for(var j=0;  j<4;  j++) {
		for (var i=0;  i<4;  i++) {
			mat[index] = rowArray[i][j];
			index++;
		}
	}

	return mat;
};

Matrix.I = function(dimen) {
	var mat = [];

	for (var i = 0; i<dimen*dimen; i++)  {
        mat.push(0);
    }

	var index = 0;
	for (var j = 0; j<dimen; j++) {
		mat[index] = 1.0;
		index += dimen + 1;
	}
	
	return mat;	
};

Matrix.Translation = function (vec) {
	var mat = Matrix.I(4);
	glmat4.translate(mat, vec);
	return mat;
};

Matrix.RotationX = function( angle ) {
	var mat = Matrix.I(4);
	glmat4.rotateX(mat, angle);
	return mat;
};

Matrix.RotationY = function( angle ) {
	var mat = Matrix.I(4);
	glmat4.rotateY(mat, angle);
	return mat;
};

Matrix.RotationZ = function( angle ) {
	var mat = Matrix.I(4);
	glmat4.rotateZ(mat, angle);
	return mat;
};

Matrix.Rotation = function(angle, axis) {
    var mat = Matrix.I(4);
    glmat4.rotate(mat, angle, axis);
    return mat;
};

Matrix.flatten = function (mat) {
    var result = [];
    if (this.elements.length == 0) {
        return [];
    }

	for (var i=0; i<16; i++) {
        result.push(mat[i]);
    }

    return result;
};

Matrix.makePerspective = function (fovy, aspect, znear, zfar) {
    var ymax = znear * Math.tan(fovy * Math.PI / 360.0);
    var ymin = -ymax;
    var xmin = ymin * aspect;
    var xmax = ymax * aspect;

    //return makeFrustum(xmin, xmax, ymin, ymax, znear, zfar);

    var X = 2*znear/(xmax-xmin);
    var Y = 2*znear/(ymax-ymin);
    var A = (xmax+xmin)/(xmax-xmin);
    var B = (ymax+ymin)/(ymax-ymin);
    var C = -(zfar+znear)/(zfar-znear);
    var D = -2*zfar*znear/(zfar-znear);

    return Matrix.create([[X, 0, A, 0], [0, Y, B, 0], [0, 0, C, D], [0, 0, -1, 0]]);

};

// Namepace this class
// TODO
window.Matrix = Matrix;