PageRenderTime 263ms CodeModel.GetById 151ms app.highlight 1ms RepoModel.GetById 109ms app.codeStats 0ms

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