/OJGL/GLU.j

http://github.com/tartiflop/ojgl · Unknown · 120 lines · 93 code · 27 blank · 0 comment · 0 complexity · 7abcc376669318868dab5eda379a9dc3 MD5 · raw file

  1. @import <Foundation/CPObject.j>
  2. @implementation GLU : CPObject {
  3. }
  4. - (id)init {
  5. self = [super init];
  6. if (self) {
  7. }
  8. return self;
  9. }
  10. + (Matrix4D)lookAt:(float)eyex eyey:(float)eyey eyez:(float)eyez centerx:(float)centerx centery:(float)centery centerz:(float)centerz upx:(float)upx upy:(float)upy upz:(float)upz {
  11. // remember: z out of screen
  12. var zx = eyex - centerx;
  13. var zy = eyey - centery;
  14. var zz = eyez - centerz;
  15. // normalise z
  16. var zlen = Math.sqrt(zx * zx + zy * zy + zz * zz);
  17. zx /= zlen;
  18. zy /= zlen;
  19. zz /= zlen;
  20. // Calculate cross product of up and z to get x
  21. // (x coming out of plane containing up and z: up not necessarily perpendicular to z)
  22. var xx = upy * zz - upz * zy;
  23. var xy = -upx * zz + upz * zx;
  24. var xz = upx * zy - upy * zx;
  25. // up not necessarily a unit vector so normalise x
  26. var xlen = Math.sqrt(xx * xx + xy * xy + xz * xz);
  27. xx /= xlen;
  28. xy /= xlen;
  29. xz /= xlen;
  30. // calculate y: cross product of z and x (x and z unit vector so no need to normalise after)
  31. var yx = zy * xz - zz * xy;
  32. var yy = -zx * xz + zz * xx;
  33. var yz = zx * xy - zy * xx;
  34. // Create rotation matrix from new coorinate system
  35. var lookatMatrix = new Matrix4D([xx, xy, xz, 0, yx, yy, yz, 0, zx, zy, zz, 0, 0, 0, 0, 1]);
  36. // create translation matrix
  37. var translationMatrix = new Matrix4D([1, 0, 0, -eyex, 0, 1, 0, -eyey, 0, 0, 1, -eyez, 0, 0, 0, 1]);
  38. // calculate final lookat (projection) matrix from combination of both rotation and translation
  39. lookatMatrix.multiply(translationMatrix);
  40. return lookatMatrix;
  41. }
  42. + (Matrix4D)perspective:(float)fovy aspect:(float)aspect near:(float)near far:(float)far {
  43. var top = Math.tan(fovy * Math.PI / 360) * near;
  44. var bottom = -top;
  45. var left = aspect * bottom;
  46. var right = aspect * top;
  47. var A = (right + left) / (right - left);
  48. var B = (top + bottom) / (top - bottom);
  49. var C = -(far + near) / (far - near);
  50. var D = -(2 * far * near) / (far - near);
  51. var matrix = new Matrix4D();
  52. matrix.sxx = (2 * near) / (right - left);
  53. matrix.syx = 0;
  54. matrix.szx = 0;
  55. matrix.swx = 0;
  56. matrix.sxy = 0;
  57. matrix.syy = 2 * near / (top - bottom);
  58. matrix.szy = 0;
  59. matrix.swy = 0;
  60. matrix.sxz = A;
  61. matrix.syz = B;
  62. matrix.szz = C;
  63. matrix.swz = -1;
  64. matrix.tx = 0;
  65. matrix.ty = 0;
  66. matrix.tz = D;
  67. matrix.tw = 0;
  68. return matrix;
  69. }
  70. + (Matrix4D)ortho:(float)left right:(float)right bottom:(float)bottom top:(float)top near:(float)near far:(float)far {
  71. var tx = (left + right) / (right - left);
  72. var ty = (top + bottom) / (top - bottom);
  73. var tz = (far + near) / (far - near);
  74. var matrix = new Matrix4D();
  75. matrix.sxx = 2 / (right - left);
  76. matrix.sxy = 0;
  77. matrix.sxz = 0;
  78. matrix.tx = tx;
  79. matrix.syx = 0;
  80. matrix.syy = 2 / (top - bottom);
  81. matrix.syz = 0;
  82. matrix.ty = ty;
  83. matrix.szx = 0;
  84. matrix.szy = 0;
  85. matrix.szz = -2 / (far - near);
  86. matrix.tz = -tz;
  87. matrix.swx = 0;
  88. matrix.swy = 0;
  89. matrix.swz = 0;
  90. matrix.tw = 1;
  91. return matrix;
  92. }
  93. @end