29#import "MyOpenGLView.h"
37 "ooliteModelViewProjection",
39 "ooliteModelViewInverse",
40 "ooliteProjectionInverse",
41 "ooliteModelViewProjectionInverse",
42 "ooliteModelViewTranspose",
43 "ooliteProjectionTranspose",
44 "ooliteModelViewProjectionTranspose",
45 "ooliteModelViewInverseTranspose",
46 "ooliteProjectionInverseTranspose",
47 "ooliteModelViewProjectionInverseTranspose"
54 if ((
self = [super
init]))
56 stack = [[NSMutableArray alloc] init];
67- (void) push: (OOMatrix) matrix
69 [stack addObject: [NSValue valueWithBytes: &matrix objCType: @encode(OOMatrix)]];
79 [[stack lastObject] getValue: &matrix];
80 [stack removeLastObject];
103 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
104 valid[OOLITE_GL_MATRIX_NORMAL] = NO;
105 valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE] = NO;
106 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
107 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
108 valid[OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE] = NO;
109 valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
110 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
111 valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE] = NO;
112 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
113 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
118 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
119 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
120 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
121 valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
122 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
123 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
124 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
133 if ((
self = [super init]))
159 [modelViewStack release];
160 [projectionStack release];
164- (void) loadModelView: (OOMatrix) matrix
166 matrices[OOLITE_GL_MATRIX_MODELVIEW] = matrix;
167 [
self updateModelView];
171- (void) resetModelView
174 [
self updateModelView];
177- (void) multModelView: (OOMatrix) matrix
180 [
self updateModelView];
183- (void) translateModelView: (Vector) vector
186 matrix.m[3][0] = vector.x;
187 matrix.m[3][1] = vector.y;
188 matrix.m[3][2] = vector.z;
189 [
self multModelView: matrix];
192- (void) rotateModelView: (GLfloat) angle axis: (Vector) axis
194 [
self multModelView: OOMatrixForRotation(axis, angle)];
197- (void) scaleModelView: (Vector) scale
199 [
self multModelView: OOMatrixForScale(scale.x, scale.y, scale.z)];
202- (void) lookAtWithEye: (Vector) eye center: (Vector) center up: (Vector) up
204 Vector k = vector_normal(vector_subtract(eye, center));
205 Vector i = cross_product(up, k);
206 Vector j = cross_product(k, i);
207 OOMatrix m1 = OOMatrixConstruct
212 -eye.x, -eye.y, -eye.z, 1.0
214 [
self multModelView: m1];
219- (void) pushModelView
221 [modelViewStack push: matrices[OOLITE_GL_MATRIX_MODELVIEW]];
224- (OOMatrix) popModelView
226 matrices[OOLITE_GL_MATRIX_MODELVIEW] = [modelViewStack pop];
227 [
self updateModelView];
228 return matrices[OOLITE_GL_MATRIX_MODELVIEW];
231- (OOMatrix) getModelView
233 return matrices[OOLITE_GL_MATRIX_MODELVIEW];
236- (NSUInteger) countModelView
238 return [modelViewStack stackCount];
241- (void) syncModelView
244 OOGL(glMatrixMode(GL_MODELVIEW));
245 GLLoadOOMatrix([
self getModelView]);
249- (void) loadProjection: (OOMatrix) matrix
251 matrices[OOLITE_GL_MATRIX_PROJECTION] = matrix;
252 [
self updateProjection];
256- (void) multProjection: (OOMatrix) matrix
259 [
self updateProjection];
262- (void) translateProjection: (Vector) vector
265 matrix.m[0][3] = vector.x;
266 matrix.m[1][3] = vector.y;
267 matrix.m[2][3] = vector.z;
268 [
self multProjection: matrix];
271- (void) rotateProjection: (GLfloat) angle axis: (Vector) axis
273 [
self multProjection: OOMatrixForRotation(axis, angle)];
276- (void) scaleProjection: (Vector) scale
278 [
self multProjection: OOMatrixForScale(scale.x, scale.y, scale.z)];
281- (void) frustumLeft: (
double) l right: (
double) r bottom: (
double) b top: (
double) t near: (
double) n far: (
double) f
283 if (l == r || t == b || n == f || n <= 0 || f <= 0)
return;
284 [
self multProjection: OOMatrixConstruct
286 2*n/(r-l), 0.0, 0.0, 0.0,
287 0.0, 2*n/(t-b), 0.0, 0.0,
288 (r+l)/(r-l), (t+b)/(t-b), -(f+n)/(f-n), -1.0,
289 0.0, 0.0, -2*f*n/(f-n), 0.0
293- (void) orthoLeft: (
double) l right: (
double) r bottom: (
double) b top: (
double) t near: (
double) n far: (
double) f
295 if (l == r || t == b || n == f)
return;
296 [
self multProjection: OOMatrixConstruct
298 2/(r-l), 0.0, 0.0, 0.0,
299 0.0, 2/(t-b), 0.0, 0.0,
300 0.0, 0.0, 2/(n-f), 0.0,
301 (l+r)/(l-r), (b+t)/(b-t), (n+f)/(n-f), 1.0
305- (void) perspectiveFovy: (
double) fovy aspect: (
double) aspect zNear: (
double) zNear zFar: (
double) zFar
307 if (aspect == 0.0 || zNear == zFar)
return;
308 double f = 1.0/tan(
M_PI * fovy / 360);
309 [
self multProjection: OOMatrixConstruct
311 f/aspect, 0.0, 0.0, 0.0,
313 0.0, 0.0, (zFar + zNear)/(zNear - zFar), -1.0,
314 0.0, 0.0, 2*zFar*zNear/(zNear - zFar), 0.0
318- (void) resetProjection
321 [
self updateProjection];
324- (void) pushProjection
326 [projectionStack push: matrices[OOLITE_GL_MATRIX_PROJECTION]];
329- (OOMatrix) popProjection
331 matrices[OOLITE_GL_MATRIX_PROJECTION] = [projectionStack pop];
332 [
self updateProjection];
333 return matrices[OOLITE_GL_MATRIX_PROJECTION];
336- (OOMatrix) getProjection
338 return matrices[OOLITE_GL_MATRIX_PROJECTION];
341- (void) syncProjection
344 OOGL(glMatrixMode(GL_PROJECTION));
345 GLLoadOOMatrix([
self getProjection]);
349- (OOMatrix) getMatrix: (
int) which
352 if (valid[which])
return matrices[which];
360 matrices[which] = matrices[OOLITE_GL_MATRIX_MODELVIEW];
361 matrices[which].m[3][0] = 0.0;
362 matrices[which].m[3][1] = 0.0;
363 matrices[which].m[3][2] = 0.0;
364 matrices[which].m[0][3] = 0.0;
365 matrices[which].m[1][3] = 0.0;
366 matrices[which].m[2][3] = 0.0;
367 matrices[which].m[3][3] = 1.0;
371 d = powf(fabsf(d), 1.0/3);
372 for (
int i = 0; i < 3; i++)
374 for (
int j = 0; j < 3; j++)
376 matrices[which].m[i][j] /= d;
410 return matrices[which];
413- (NSArray*) standardMatrixUniformLocations: (GLhandleARB) program
417 NSMutableArray *locationSet = [[[NSMutableArray alloc] init] autorelease];
426 [locationSet addObject:
427 [NSArray arrayWithObjects:
428 [NSNumber numberWithInt: location],
429 [NSNumber numberWithInteger: i],
435 [locationSet addObject:
436 [NSArray arrayWithObjects:
437 [NSNumber numberWithInt: location],
438 [NSNumber numberWithInteger: i],
444 return [[NSArray arrayWithArray: locationSet] retain];
584void OOGLFrustum(
double frleft,
double frright,
double frbottom,
double frtop,
double frnear,
double frfar)
591void OOGLOrtho(
double orleft,
double orright,
double orbottom,
double ortop,
double ornear,
double orfar)
608 return [matrixManager
getMatrix: OOLITE_GL_MATRIX_MODELVIEW_PROJECTION];
#define OO_ENTER_OPENGL()
OOMatrix OOMatrixMultiply(OOMatrix a, OOMatrix b)
OOMatrix OOMatrixInverseWithDeterminant(OOMatrix M, OOScalar *d)
const OOMatrix kIdentityMatrix
OOMatrix OOMatrixInverse(OOMatrix M)
@ OOLITE_GL_MATRIX_MODELVIEW
@ OOLITE_GL_MATRIX_MODELVIEW_PROJECTION
@ OOLITE_GL_MATRIX_MODELVIEW_INVERSE
@ OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE
@ OOLITE_GL_MATRIX_PROJECTION
@ OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE
@ OOLITE_GL_MATRIX_PROJECTION_INVERSE
@ OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE
@ OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE
@ OOLITE_GL_MATRIX_NORMAL
@ OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE
@ OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE
@ OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE
const char * ooliteStandardMatrixUniforms[]
void OOGLLoadModelView(OOMatrix matrix)
OOMatrix OOGLGetModelView()
OOMatrix OOGLPopProjection()
void OOGLRotateProjection(GLfloat angle, Vector axis)
void OOGLTranslateProjection(Vector vector)
void OOGLRotateModelView(GLfloat angle, Vector axis)
void OOGLLookAt(Vector eye, Vector center, Vector up)
void OOGLLoadProjection(OOMatrix matrix)
void OOGLScaleProjection(Vector scale)
void OOGLScaleModelView(Vector scale)
void OOGLResetProjection()
void OOGLTranslateModelView(Vector vector)
OOMatrix OOGLGetProjection()
void OOGLMultProjection(OOMatrix matrix)
OOMatrix OOGLPopModelView()
void OOGLPushProjection()
void OOGLPerspective(double fovy, double aspect, double zNear, double zFar)
void OOGLMultModelView(OOMatrix matrix)
void OOGLOrtho(double orleft, double orright, double orbottom, double ortop, double ornear, double orfar)
void OOGLFrustum(double frleft, double frright, double frbottom, double frtop, double frnear, double frfar)
OOMatrix OOGLGetModelViewProjection()
void OOGLResetModelView()
const char * ooliteStandardMatrixUniforms[]
void frustumLeft:right:bottom:top:near:far:(double l,[right] double r,[bottom] double b,[top] double t,[near] double n,[far] double f)
void perspectiveFovy:aspect:zNear:zFar:(double fovy,[aspect] double aspect,[zNear] double zNear,[zFar] double zFar)
void multModelView:(OOMatrix matrix)
void loadProjection:(OOMatrix matrix)
void scaleProjection:(Vector scale)
void translateProjection:(Vector vector)
void rotateProjection:axis:(GLfloat angle,[axis] Vector axis)
void rotateModelView:axis:(GLfloat angle,[axis] Vector axis)
void translateModelView:(Vector vector)
void lookAtWithEye:center:up:(Vector eye,[center] Vector center,[up] Vector up)
void loadModelView:(OOMatrix matrix)
void orthoLeft:right:bottom:top:near:far:(double l,[right] double r,[bottom] double b,[top] double t,[near] double n,[far] double f)
OOMatrix getMatrix:(int which)
void scaleModelView:(Vector scale)
void multProjection:(OOMatrix matrix)