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.
18Matrix = function ( ) {
29 this . m = Array . prototype . concat . apply ( [ ] , arguments ) ;
310 if ( ! this . m . length ) {
@@ -10,8 +17,12 @@ Matrix = function() {
1017 }
1118} ;
1219
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`.
1325Matrix . prototype . inverse = function ( ) {
14- // Implementation from the Mesa OpenGL function __gluInvertMatrixd()
1526 var m = this . m , inv = new Matrix (
1627 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 ] ,
1728 - 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() {
3950 return inv ;
4051} ;
4152
53+ // ### .multiply(matrix)
54+ //
55+ // Concatenates the transforms for this matrix and `matrix`.
56+ // This emulates the OpenGL function `glMultMatrix()`.
4257Matrix . prototype . multiply = function ( matrix ) {
4358 var a = this . m , b = matrix . m ;
4459 return new Matrix (
@@ -64,6 +79,10 @@ Matrix.prototype.multiply = function(matrix) {
6479 ) ;
6580} ;
6681
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.
6786Matrix . prototype . transformPoint = function ( v ) {
6887 var m = this . m ;
6988 return new Vector (
@@ -73,6 +92,10 @@ Matrix.prototype.transformPoint = function(v) {
7392 ) . divide ( m [ 12 ] * v . x + m [ 13 ] * v . y + m [ 14 ] * v . z + m [ 15 ] ) ;
7493} ;
7594
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.
7699Matrix . prototype . transformVector = function ( v ) {
77100 var m = this . m ;
78101 return new Vector (
@@ -82,12 +105,23 @@ Matrix.prototype.transformVector = function(v) {
82105 ) ;
83106} ;
84107
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()`.
85114Matrix . perspective = function ( fov , aspect , near , far ) {
86115 var y = Math . tan ( fov * Math . PI / 360 ) * near ;
87116 var x = y * aspect ;
88117 return Matrix . frustum ( - x , x , - y , y , near , far ) ;
89118} ;
90119
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()`.
91125Matrix . frustum = function ( l , r , b , t , n , f ) {
92126 return new Matrix (
93127 2 * n / ( r - l ) , 0 , ( r + l ) / ( r - l ) , 0 ,
@@ -97,6 +131,11 @@ Matrix.frustum = function(l, r, b, t, n, f) {
97131 ) ;
98132} ;
99133
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()`.
100139Matrix . ortho = function ( l , r , b , t , n , f ) {
101140 return new Matrix (
102141 2 / ( r - l ) , 0 , 0 , ( r + l ) / ( r - l ) ,
@@ -106,6 +145,9 @@ Matrix.ortho = function(l, r, b, t, n, f) {
106145 ) ;
107146} ;
108147
148+ // ### .scale(x, y, z)
149+ //
150+ // This emulates the OpenGL function `glScale()`.
109151Matrix . scale = function ( x , y , z ) {
110152 return new Matrix (
111153 x , 0 , 0 , 0 ,
@@ -115,6 +157,9 @@ Matrix.scale = function(x, y, z) {
115157 ) ;
116158} ;
117159
160+ // ### .translate(x, y, z)
161+ //
162+ // This emulates the OpenGL function `glTranslate()`.
118163Matrix . translate = function ( x , y , z ) {
119164 return new Matrix (
120165 1 , 0 , 0 , x ,
@@ -124,6 +169,10 @@ Matrix.translate = function(x, y, z) {
124169 ) ;
125170} ;
126171
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()`.
127176Matrix . rotate = function ( a , x , y , z ) {
128177 if ( a && ( x || y || z ) ) {
129178 var d = Math . sqrt ( x * x + y * y + z * z ) ;
@@ -140,6 +189,11 @@ Matrix.rotate = function(a, x, y, z) {
140189 }
141190} ;
142191
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()`.
143197Matrix . lookAt = function ( ex , ey , ez , cx , cy , cz , ux , uy , uz ) {
144198 var e = new Vector ( ex , ey , ez ) ;
145199 var c = new Vector ( cx , cy , cz ) ;
0 commit comments