Advertisement
Delta

OpenGL full mat4 lib

Sep 10th, 2011
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Vec.js required for some methods (pastebin.com/Hdxg8dxn)
  3.  
  4.     Please, if you're going to use it, could you put a link to my pastebin account and my e-mail (tutmes@gmail.com)?
  5.     Thanks ;)
  6. */
  7.  
  8. window.Matrix = {};
  9.  
  10. Matrix.CreateScale = function(sX, sY, sZ)
  11. {
  12.     arguments.length == 1 && (sY = sZ = sX);
  13.    
  14.     return [
  15.         sX,  0,  0, 0,
  16.          0, sY,  0, 0,
  17.          0,  0, sZ, 0,
  18.          0,  0,  0, 1
  19.     ];
  20. };
  21.  
  22. Matrix.CreateTranslation = function(dX, dY, dZ)
  23. {
  24.     arguments.length == 1 && (dY = dZ = dX);
  25.    
  26.     return [
  27.          1,  0,  0, 0,
  28.          0,  1,  0, 0,
  29.          0,  0,  1, 0,
  30.         dX, dY, dZ, 1
  31.     ];
  32. };
  33.  
  34. Matrix.CreateRotationX = function(Ang)
  35. {
  36.     var Cos = Math.cos(Ang);
  37.     var Sin = Math.sin(Ang);
  38.    
  39.     return [
  40.         1,    0,   0, 0,
  41.         0,  Cos, Sin, 0,
  42.         0, -Sin, Cos, 0,
  43.         0,    0,   0, 1
  44.     ];
  45. };
  46.  
  47. Matrix.CreateRotationY = function(Ang)
  48. {
  49.     var Cos = Math.cos(Ang);
  50.     var Sin = Math.sin(Ang);
  51.    
  52.     return [
  53.         Cos, 0, -Sin, 0,
  54.           0, 1,    0, 0,
  55.         Sin, 0,  Cos, 0,
  56.           0, 0,    0, 1
  57.     ];
  58. };
  59.  
  60. Matrix.CreateRotationZ = function(Ang)
  61. {
  62.     var Cos = Math.cos(Ang);
  63.     var Sin = Math.sin(Ang);
  64.    
  65.     return [
  66.          Cos, Sin, 0, 0,
  67.         -Sin, Cos, 0, 0,
  68.            0,   0, 1, 0,
  69.            0,   0, 0, 1
  70.     ];
  71. };
  72.  
  73. Matrix.Mul = function(M2, M1)
  74. {
  75.     var M3 = [];
  76.    
  77.     M3[0] = M1[0] * M2[0] + M1[1] * M2[4] + M1[2] *  M2[8] + M1[3] * M2[12];
  78.     M3[1] = M1[0] * M2[1] + M1[1] * M2[5] + M1[2] *  M2[9] + M1[3] * M2[13];
  79.     M3[2] = M1[0] * M2[2] + M1[1] * M2[6] + M1[2] * M2[10] + M1[3] * M2[14];
  80.     M3[3] = M1[0] * M2[3] + M1[1] * M2[7] + M1[2] * M2[11] + M1[3] * M2[15];
  81.    
  82.     M3[4] = M1[4] * M2[0] + M1[5] * M2[4] + M1[6] *  M2[8] + M1[7] * M2[12];
  83.     M3[5] = M1[4] * M2[1] + M1[5] * M2[5] + M1[6] *  M2[9] + M1[7] * M2[13];
  84.     M3[6] = M1[4] * M2[2] + M1[5] * M2[6] + M1[6] * M2[10] + M1[7] * M2[14];
  85.     M3[7] = M1[4] * M2[3] + M1[5] * M2[7] + M1[6] * M2[11] + M1[7] * M2[15];
  86.    
  87.     M3[8]  = M1[8] * M2[0] + M1[9] * M2[4] + M1[10] *  M2[8] + M1[11] * M2[12];
  88.     M3[9]  = M1[8] * M2[1] + M1[9] * M2[5] + M1[10] *  M2[9] + M1[11] * M2[13];
  89.     M3[10] = M1[8] * M2[2] + M1[9] * M2[6] + M1[10] * M2[10] + M1[11] * M2[14];
  90.     M3[11] = M1[8] * M2[3] + M1[9] * M2[7] + M1[10] * M2[11] + M1[11] * M2[15];
  91.    
  92.     M3[12] = M1[12] * M2[0] + M1[13] * M2[4] + M1[14] *  M2[8] + M1[15] * M2[12];
  93.     M3[13] = M1[12] * M2[1] + M1[13] * M2[5] + M1[14] *  M2[9] + M1[15] * M2[13];
  94.     M3[14] = M1[12] * M2[2] + M1[13] * M2[6] + M1[14] * M2[10] + M1[15] * M2[14];
  95.     M3[15] = M1[12] * M2[3] + M1[13] * M2[7] + M1[14] * M2[11] + M1[15] * M2[15];
  96.    
  97.     return M3;
  98. };
  99.  
  100. Matrix.__defineGetter__("Identity", function()
  101. {
  102.     return [
  103.         1, 0, 0, 0,
  104.         0, 1, 0, 0,
  105.         0, 0, 1, 0,
  106.         0, 0, 0, 1
  107.     ];
  108. });
  109.  
  110. Matrix.Transpose = function(M)
  111. {
  112.     return [
  113.         M[0], M[4],  M[8], M[12],
  114.         M[1], M[5],  M[9], M[13],
  115.         M[2], M[6], M[10], M[14],
  116.         M[3], M[7], M[11], M[15]
  117.     ];
  118. };
  119.  
  120. Matrix.Transpose3 = function(M)
  121. {
  122.     return [
  123.         M[0],  M[4],  M[8],  M[3],
  124.         M[1],  M[5],  M[9],  M[7],
  125.         M[2],  M[6],  M[10], M[11],
  126.         M[12], M[13], M[14], M[15]
  127.     ];
  128. };
  129.  
  130. Matrix.CreateLookAt = function(Position, Target, Up) //Input params are all of Vector3 type.
  131. {
  132.     if(Position.Equals(Target))
  133.         return Matrix.Identity;
  134.  
  135.     var zaxis = Position.Clone().Subtract(Target).Normalize();
  136.     var xaxis = Up.Clone().Cross(zaxis).Normalize();
  137.     var yaxis = zaxis.Clone().Cross(xaxis);
  138.    
  139.     var xdp = xaxis.Dot(Position);
  140.     var ydp = yaxis.Dot(Position);
  141.     var zdp = zaxis.Dot(Position);
  142.    
  143.     return [
  144.         xaxis.X, yaxis.X, zaxis.X, 0,
  145.         xaxis.Y, yaxis.Y, zaxis.Y, 0,
  146.         xaxis.Z, yaxis.Z, zaxis.Z, 0,
  147.            -xdp,    -ydp,    -zdp, 1
  148.     ];
  149. };
  150.  
  151. Matrix.CreatePerspectiveFieldOfView = function(Fov, AspectRatio, Near, Far)
  152. {
  153.     var yScale = 1 / Math.tan(Fov / 2);
  154.     var xScale = yScale / AspectRatio;
  155.    
  156.     return [
  157.         xScale,      0,                   0,  0,
  158.              0, yScale,                   0,  0,
  159.              0,      0,      Far/(Near-Far), -1,
  160.              0,      0, Near*Far/(Near-Far),  0
  161.     ];
  162. };
  163.  
  164. Matrix.CreateOrthographic = function(Width, Height, Near, Far)
  165. {
  166.     return [
  167.         2/Width,        0,            0,                0,
  168.               0, 2/Height,            0,                0,
  169.               0,        0, 1/(Far-Near), -Near/(Far-Near),
  170.               0,        0,            0,                1
  171.     ];
  172. };
  173.  
  174. Matrix.Scale = function(M, S)
  175. {
  176.     var L = M.length;
  177.     var Out = [];
  178.     for(var I = 0; I < L; I++)
  179.         Out[I] = M[I] * S;
  180.        
  181.     return Out;
  182. };
  183.  
  184. Matrix.Determinant3 = function(M)
  185. {  
  186.     var Forward = M[0] * M[4] * M[8] + M[1] * M[5] * M[6] + M[2] * M[3] * M[7];
  187.     var Backwards = M[0] * M[5] * M[7] + M[1] * M[3] * M[8] + M[2] * M[4] * M[6];
  188.    
  189.     return Forward - Backwards;
  190. };
  191.  
  192. Matrix.Reduce = function(M, x, y)
  193. {
  194.     var Out = [];
  195.     for(var Y = 0; Y < 4; Y++)
  196.     {
  197.         if(Y == y)
  198.             continue;
  199.                
  200.         for(var X = 0; X < 4; X++)
  201.         {
  202.             if(X == x)
  203.                 continue
  204.                
  205.             Out.push(M[Y * 4 + X]);
  206.         }
  207.     }
  208.        
  209.     return Out;
  210. };
  211.  
  212. Matrix.Minor = function(M, X, Y)
  213. {  
  214.     return Matrix.Determinant3(Matrix.Reduce(M, X, Y));
  215. };
  216.  
  217. Matrix.Minors = function(M)
  218. {  
  219.     var Minors = [];
  220.     for(var Y = 0; Y < 4; Y++)
  221.         for(var X = 0; X < 4; X++)
  222.             Minors.push(Matrix.Minor(M, X, Y));
  223.    
  224.     return Minors;
  225. };
  226.  
  227. Matrix.MinorsCofactors = function(M)
  228. {  
  229.     var CoMinors = [];
  230.     for(var Y = 0; Y < 4; Y++)
  231.         for(var X = 0; X < 4; X++)
  232.             CoMinors.push((Y + X) % 2 ? -Matrix.Minor(M, X, Y) : Matrix.Minor(M, X, Y));
  233.    
  234.     return CoMinors;
  235. };
  236.  
  237. Matrix.Adjugate = function(M)
  238. {
  239.     return Matrix.Transpose(Matrix.MinorsCofactors(M));
  240. };
  241.  
  242. Matrix.Determinant = function(M)
  243. {
  244.     var CoMinors = [
  245.         Matrix.Minor(M, 0, 0),
  246.         -Matrix.Minor(M, 1, 0),
  247.         Matrix.Minor(M, 2, 0),
  248.         -Matrix.Minor(M, 3, 0),
  249.     ];
  250.    
  251.     return M[0] * CoMinors[0] + M[1] * CoMinors[1] + M[2] * CoMinors[2] + M[3] * CoMinors[3];
  252. };
  253.  
  254. Matrix.Inverse = function(M)
  255. {
  256.     var CoMinors = Matrix.MinorsCofactors(M);
  257.     var Det = M[0] * CoMinors[0] + M[1] * CoMinors[1] + M[2] * CoMinors[2] + M[3] * CoMinors[3];
  258.     var Adj = Matrix.Transpose(CoMinors);
  259.    
  260.     return Matrix.Scale(Adj, 1 / Det);
  261. };
  262.  
  263. Matrix.Forward = function(M)
  264. {
  265.     return new Vector3(-M[2], -M[6], -M[10]);
  266. };
  267.  
  268. Matrix.Up = function(M)
  269. {
  270.     return new Vector3(M[1], M[5], M[9]);
  271. };
  272.  
  273. Matrix.Right = function(M)
  274. {
  275.     return new Vector3(M[0], M[4], M[8]);
  276. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement