1
+ // Represents a 4x4 matrix.
2
+
3
+ // ### new Matrix([elements])
4
+ //
5
+ // This constructor takes 16 arguments, which can be passed individually, as
6
+ // a list, or even as four lists, one for each row. If the arguments are
7
+ // omitted then the identity matrix is constructed instead.
1
8
Matrix = function ( ) {
2
9
this . m = Array . prototype . concat . apply ( [ ] , arguments ) ;
3
10
if ( ! this . m . length ) {
@@ -10,8 +17,12 @@ Matrix = function() {
10
17
}
11
18
} ;
12
19
20
+ // ### .inverse()
21
+ //
22
+ // Returns the matrix that when multiplied with this matrix results in the
23
+ // identity matrix. This implementation is from the Mesa OpenGL function
24
+ // `__gluInvertMatrixd()` found in `project.c`.
13
25
Matrix . prototype . inverse = function ( ) {
14
- // Implementation from the Mesa OpenGL function __gluInvertMatrixd()
15
26
var m = this . m , inv = new Matrix (
16
27
m [ 5 ] * m [ 10 ] * m [ 15 ] - m [ 5 ] * m [ 14 ] * m [ 11 ] - m [ 6 ] * m [ 9 ] * m [ 15 ] + m [ 6 ] * m [ 13 ] * m [ 11 ] + m [ 7 ] * m [ 9 ] * m [ 14 ] - m [ 7 ] * m [ 13 ] * m [ 10 ] ,
17
28
- m [ 1 ] * m [ 10 ] * m [ 15 ] + m [ 1 ] * m [ 14 ] * m [ 11 ] + m [ 2 ] * m [ 9 ] * m [ 15 ] - m [ 2 ] * m [ 13 ] * m [ 11 ] - m [ 3 ] * m [ 9 ] * m [ 14 ] + m [ 3 ] * m [ 13 ] * m [ 10 ] ,
@@ -39,6 +50,10 @@ Matrix.prototype.inverse = function() {
39
50
return inv ;
40
51
} ;
41
52
53
+ // ### .multiply(matrix)
54
+ //
55
+ // Concatenates the transforms for this matrix and `matrix`.
56
+ // This emulates the OpenGL function `glMultMatrix()`.
42
57
Matrix . prototype . multiply = function ( matrix ) {
43
58
var a = this . m , b = matrix . m ;
44
59
return new Matrix (
@@ -64,6 +79,10 @@ Matrix.prototype.multiply = function(matrix) {
64
79
) ;
65
80
} ;
66
81
82
+ // ### .transformPoint(point)
83
+ //
84
+ // Transforms the vector as a point with a `w` coordinate of `1`. This
85
+ // means translations will have an effect, for example.
67
86
Matrix . prototype . transformPoint = function ( v ) {
68
87
var m = this . m ;
69
88
return new Vector (
@@ -73,6 +92,10 @@ Matrix.prototype.transformPoint = function(v) {
73
92
) . divide ( m [ 12 ] * v . x + m [ 13 ] * v . y + m [ 14 ] * v . z + m [ 15 ] ) ;
74
93
} ;
75
94
95
+ // ### .transformPoint(vector)
96
+ //
97
+ // Transforms the vector as a vector with a `w` coordinate of `0`. This
98
+ // means translations will have no effect, for example.
76
99
Matrix . prototype . transformVector = function ( v ) {
77
100
var m = this . m ;
78
101
return new Vector (
@@ -82,12 +105,23 @@ Matrix.prototype.transformVector = function(v) {
82
105
) ;
83
106
} ;
84
107
108
+ // ### .perspective(fov, aspect, near, far)
109
+ //
110
+ // Sets up a perspective transform, which makes far away objects appear smaller
111
+ // than nearby objects. The `aspect` argument is the width divided by the height
112
+ // of your viewport and `fov` is the top-to-bottom angle of the field of view in
113
+ // degrees. This emulates the OpenGL function `gluPerspective()`.
85
114
Matrix . perspective = function ( fov , aspect , near , far ) {
86
115
var y = Math . tan ( fov * Math . PI / 360 ) * near ;
87
116
var x = y * aspect ;
88
117
return Matrix . frustum ( - x , x , - y , y , near , far ) ;
89
118
} ;
90
119
120
+ // ### .frustum(left, right, bottom, top, near, far)
121
+ //
122
+ // Sets up a viewing frustum, which is shaped like a truncated pyramid with the
123
+ // camera where the point of the pyramid would be. This emulates the OpenGL
124
+ // function `glFrustum()`.
91
125
Matrix . frustum = function ( l , r , b , t , n , f ) {
92
126
return new Matrix (
93
127
2 * n / ( r - l ) , 0 , ( r + l ) / ( r - l ) , 0 ,
@@ -97,6 +131,11 @@ Matrix.frustum = function(l, r, b, t, n, f) {
97
131
) ;
98
132
} ;
99
133
134
+ // ### .ortho(left, right, bottom, top, near, far)
135
+ //
136
+ // Creates an orthographic projection, in which objects are the same size no
137
+ // matter how far away or nearby they are. This emulates the OpenGL function
138
+ // `glOrtho()`.
100
139
Matrix . ortho = function ( l , r , b , t , n , f ) {
101
140
return new Matrix (
102
141
2 / ( r - l ) , 0 , 0 , ( r + l ) / ( r - l ) ,
@@ -106,6 +145,9 @@ Matrix.ortho = function(l, r, b, t, n, f) {
106
145
) ;
107
146
} ;
108
147
148
+ // ### .scale(x, y, z)
149
+ //
150
+ // This emulates the OpenGL function `glScale()`.
109
151
Matrix . scale = function ( x , y , z ) {
110
152
return new Matrix (
111
153
x , 0 , 0 , 0 ,
@@ -115,6 +157,9 @@ Matrix.scale = function(x, y, z) {
115
157
) ;
116
158
} ;
117
159
160
+ // ### .translate(x, y, z)
161
+ //
162
+ // This emulates the OpenGL function `glTranslate()`.
118
163
Matrix . translate = function ( x , y , z ) {
119
164
return new Matrix (
120
165
1 , 0 , 0 , x ,
@@ -124,6 +169,10 @@ Matrix.translate = function(x, y, z) {
124
169
) ;
125
170
} ;
126
171
172
+ // ### .rotate(a, x, y, z)
173
+ //
174
+ // Creates a matrix that rotates by `a` degrees around the vector `x, y, z`.
175
+ // This emulates the OpenGL function `glRotate()`.
127
176
Matrix . rotate = function ( a , x , y , z ) {
128
177
if ( a && ( x || y || z ) ) {
129
178
var d = Math . sqrt ( x * x + y * y + z * z ) ;
@@ -140,6 +189,11 @@ Matrix.rotate = function(a, x, y, z) {
140
189
}
141
190
} ;
142
191
192
+ // ### .lookAt(ex, ey, ez, cx, cy, cz, ux, uy, uz)
193
+ //
194
+ // Create a matrix that puts the camera at the eye point `ex, ey, ez` looking
195
+ // toward the center point `cx, cy, cz` with an up direction of `ux, uy, uz`.
196
+ // This emulates the OpenGL function `gluLookAt()`.
143
197
Matrix . lookAt = function ( ex , ey , ez , cx , cy , cz , ux , uy , uz ) {
144
198
var e = new Vector ( ex , ey , ez ) ;
145
199
var c = new Vector ( cx , cy , cz ) ;
0 commit comments