Oolite
Loading...
Searching...
No Matches
OOOpenGLMatrixManager.m
Go to the documentation of this file.
1/*
2
3OOOpenGLMatrixManager.m
4
5Manages OpenGL Model, View, etc. matrices.
6
7Oolite
8Copyright (C) 2004-2014 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
28#import "MyOpenGLView.h"
29#import "OOMacroOpenGL.h"
31#import "Universe.h"
32
34 "ooliteModelView",
35 "ooliteProjection",
36 "ooliteModelViewProjection",
37 "ooliteNormalMatrix",
38 "ooliteModelViewInverse",
39 "ooliteProjectionInverse",
40 "ooliteModelViewProjectionInverse",
41 "ooliteModelViewTranspose",
42 "ooliteProjectionTranspose",
43 "ooliteModelViewProjectionTranspose",
44 "ooliteModelViewInverseTranspose",
45 "ooliteProjectionInverseTranspose",
46 "ooliteModelViewProjectionInverseTranspose"
47};
48
49@implementation OOOpenGLMatrixStack
50
51- (id)init
52{
53 if ((self = [super init])) {
54 stack = [[NSMutableArray alloc] init];
55 }
56 return self;
57}
58
59- (void)dealloc
60{
61 [stack release];
62 [super dealloc];
63}
64
65- (void)push:(OOMatrix)matrix
66{
67 [stack addObject:[NSValue valueWithBytes:&matrix objCType:@encode(OOMatrix)]];
68}
69
71{
72 if ([stack count] == 0) {
73 return kIdentityMatrix;
74 }
75 OOMatrix matrix;
76 [[stack lastObject] getValue:&matrix];
77 [stack removeLastObject];
78 return matrix;
79}
80
81- (NSUInteger)stackCount
82{
83 return [stack count];
84}
85
86@end
87
88@interface OOOpenGLMatrixManager (Private)
89
90- (void)updateModelView;
91- (void)updateProjection;
92
93@end
94
95@implementation OOOpenGLMatrixManager (Private)
96
98{
99 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
100 valid[OOLITE_GL_MATRIX_NORMAL] = NO;
101 valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE] = NO;
102 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
103 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
104 valid[OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE] = NO;
105 valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
106 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
107 valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE] = NO;
108 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
109 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
110}
111
113{
114 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
115 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
116 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
117 valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
118 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
119 valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
120 valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
121}
122
123@end
124
125@implementation OOOpenGLMatrixManager
126
127- (id)init
128{
129 if ((self = [super init])) {
130 int i;
131 for (i = 0; i < OOLITE_GL_MATRIX_END; i++) {
132 switch (i) {
136 valid[i] = YES;
137 break;
138
139 default:
140 valid[i] = NO;
141 break;
142 }
143 }
144 modelViewStack = [[OOOpenGLMatrixStack alloc] init];
145 projectionStack = [[OOOpenGLMatrixStack alloc] init];
146 }
147 return self;
148}
149
150- (void)dealloc
151{
152 [modelViewStack release];
153 [projectionStack release];
154 [super dealloc];
155}
156
157- (void)loadModelView:(OOMatrix)matrix
158{
159 matrices[OOLITE_GL_MATRIX_MODELVIEW] = matrix;
160 [self updateModelView];
161 return;
162}
163
165{
166 matrices[OOLITE_GL_MATRIX_MODELVIEW] = kIdentityMatrix;
167 [self updateModelView];
168}
169
170- (void)multModelView:(OOMatrix)matrix
171{
172 matrices[OOLITE_GL_MATRIX_MODELVIEW] = OOMatrixMultiply(matrix, matrices[OOLITE_GL_MATRIX_MODELVIEW]);
173 [self updateModelView];
174}
175
176- (void)translateModelView:(Vector)vector
177{
178 OOMatrix matrix = kIdentityMatrix;
179 matrix.m[3][0] = vector.x;
180 matrix.m[3][1] = vector.y;
181 matrix.m[3][2] = vector.z;
182 [self multModelView:matrix];
183}
184
185- (void)rotateModelView:(GLfloat)angle axis:(Vector)axis
186{
187 [self multModelView:OOMatrixForRotation(axis, angle)];
188}
189
190- (void)scaleModelView:(Vector)scale
191{
192 [self multModelView:OOMatrixForScale(scale.x, scale.y, scale.z)];
193}
194
195- (void)lookAtWithEye:(Vector)eye center:(Vector)center up:(Vector)up
196{
197 Vector k = vector_normal(vector_subtract(eye, center));
198 Vector i = cross_product(up, k);
199 Vector j = cross_product(k, i);
201 i.x, j.x, k.x, 0.0,
202 i.y, j.y, k.y, 0.0,
203 i.z, j.z, k.z, 0.0,
204 -eye.x, -eye.y, -eye.z, 1.0);
205 [self multModelView:m1];
206 return;
207}
208
210{
211 [modelViewStack push:matrices[OOLITE_GL_MATRIX_MODELVIEW]];
212}
213
215{
216 matrices[OOLITE_GL_MATRIX_MODELVIEW] = [modelViewStack pop];
217 [self updateModelView];
218 return matrices[OOLITE_GL_MATRIX_MODELVIEW];
219}
220
222{
223 return matrices[OOLITE_GL_MATRIX_MODELVIEW];
224}
225
226- (NSUInteger)countModelView
227{
228 return [modelViewStack stackCount];
229}
230
232{
234 OOGL(glMatrixMode(GL_MODELVIEW));
236 return;
237}
238
239- (void)loadProjection:(OOMatrix)matrix
240{
241 matrices[OOLITE_GL_MATRIX_PROJECTION] = matrix;
242 [self updateProjection];
243 return;
244}
245
246- (void)multProjection:(OOMatrix)matrix
247{
248 matrices[OOLITE_GL_MATRIX_PROJECTION] = OOMatrixMultiply(matrix, matrices[OOLITE_GL_MATRIX_PROJECTION]);
249 [self updateProjection];
250}
251
252- (void)translateProjection:(Vector)vector
253{
254 OOMatrix matrix = kIdentityMatrix;
255 matrix.m[0][3] = vector.x;
256 matrix.m[1][3] = vector.y;
257 matrix.m[2][3] = vector.z;
258 [self multProjection:matrix];
259}
260
261- (void)rotateProjection:(GLfloat)angle axis:(Vector)axis
262{
263 [self multProjection:OOMatrixForRotation(axis, angle)];
264}
265
266- (void)scaleProjection:(Vector)scale
267{
268 [self multProjection:OOMatrixForScale(scale.x, scale.y, scale.z)];
269}
270
271- (void)frustumLeft:(double)l right:(double)r bottom:(double)b top:(double)t near:(double)n far:(double)f
272{
273 if (l == r || t == b || n == f || n <= 0 || f <= 0)
274 return;
275 [self multProjection:OOMatrixConstruct(
276 2 * n / (r - l), 0.0, 0.0, 0.0,
277 0.0, 2 * n / (t - b), 0.0, 0.0,
278 (r + l) / (r - l), (t + b) / (t - b), -(f + n) / (f - n), -1.0,
279 0.0, 0.0, -2 * f * n / (f - n), 0.0)];
280}
281
282- (void)orthoLeft:(double)l right:(double)r bottom:(double)b top:(double)t near:(double)n far:(double)f
283{
284 if (l == r || t == b || n == f)
285 return;
286 [self multProjection:OOMatrixConstruct(
287 2 / (r - l), 0.0, 0.0, 0.0,
288 0.0, 2 / (t - b), 0.0, 0.0,
289 0.0, 0.0, 2 / (n - f), 0.0,
290 (l + r) / (l - r), (b + t) / (b - t), (n + f) / (n - f), 1.0)];
291}
292
293- (void)perspectiveFovy:(double)fovy aspect:(double)aspect zNear:(double)zNear zFar:(double)zFar
294{
295 if (aspect == 0.0 || zNear == zFar)
296 return;
297 double f = 1.0 / tan(M_PI * fovy / 360);
298 [self multProjection:OOMatrixConstruct(
299 f / aspect, 0.0, 0.0, 0.0,
300 0.0, f, 0.0, 0.0,
301 0.0, 0.0, (zFar + zNear) / (zNear - zFar), -1.0,
302 0.0, 0.0, 2 * zFar * zNear / (zNear - zFar), 0.0)];
303}
304
306{
307 matrices[OOLITE_GL_MATRIX_PROJECTION] = kIdentityMatrix;
308 [self updateProjection];
309}
310
312{
313 [projectionStack push:matrices[OOLITE_GL_MATRIX_PROJECTION]];
314}
315
317{
318 matrices[OOLITE_GL_MATRIX_PROJECTION] = [projectionStack pop];
319 [self updateProjection];
320 return matrices[OOLITE_GL_MATRIX_PROJECTION];
321}
322
324{
325 return matrices[OOLITE_GL_MATRIX_PROJECTION];
326}
327
329{
331 OOGL(glMatrixMode(GL_PROJECTION));
333 return;
334}
335
336- (OOMatrix)getMatrix:(int)which
337{
338 if (which < 0 || which >= OOLITE_GL_MATRIX_END)
339 return kIdentityMatrix;
340 if (valid[which])
341 return matrices[which];
342 OOScalar d;
343 switch (which) {
346 break;
348 matrices[which] = matrices[OOLITE_GL_MATRIX_MODELVIEW];
349 matrices[which].m[3][0] = 0.0;
350 matrices[which].m[3][1] = 0.0;
351 matrices[which].m[3][2] = 0.0;
352 matrices[which].m[0][3] = 0.0;
353 matrices[which].m[1][3] = 0.0;
354 matrices[which].m[2][3] = 0.0;
355 matrices[which].m[3][3] = 1.0;
357 if (d != 0.0) {
358 d = powf(fabsf(d), 1.0 / 3);
359 for (int i = 0; i < 3; i++) {
360 for (int j = 0; j < 3; j++) {
361 matrices[which].m[i][j] /= d;
362 }
363 }
364 }
365 break;
368 break;
371 break;
374 break;
377 break;
380 break;
383 break;
386 break;
389 break;
392 break;
393 }
394 valid[which] = YES;
395 return matrices[which];
396}
397
398- (NSArray*)standardMatrixUniformLocations:(GLhandleARB)program
399{
400 GLint location;
401 NSUInteger i;
402 NSMutableArray* locationSet = [[[NSMutableArray alloc] init] autorelease];
403
405
406 for (i = 0; i < OOLITE_GL_MATRIX_END; i++) {
407 location = glGetUniformLocationARB(program, ooliteStandardMatrixUniforms[i]);
408 if (location >= 0) {
409 if (i == OOLITE_GL_MATRIX_NORMAL) {
410 [locationSet addObject:
411 [NSArray arrayWithObjects:
412 [NSNumber numberWithInt:location],
413 [NSNumber numberWithInteger:i],
414 @"mat3",
415 nil]];
416 } else {
417 [locationSet addObject:
418 [NSArray arrayWithObjects:
419 [NSNumber numberWithInt:location],
420 [NSNumber numberWithInteger:i],
421 @"mat4",
422 nil]];
423 }
424 }
425 }
426 return [[NSArray arrayWithArray:locationSet] retain];
427}
428
429@end
430
432{
433 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
434 [matrixManager pushModelView];
435 [matrixManager syncModelView];
436}
437
439{
440 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
441 OOMatrix matrix = [matrixManager popModelView];
442 [matrixManager syncModelView];
443 return matrix;
444}
445
447{
448 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
449 OOMatrix matrix = [matrixManager getModelView];
450 return matrix;
451}
452
454{
455 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
456 [matrixManager resetModelView];
457 [matrixManager syncModelView];
458}
459
461{
462 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
463 [matrixManager loadModelView:matrix];
464 [matrixManager syncModelView];
465}
466
468{
469 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
470 [matrixManager multModelView:matrix];
471 [matrixManager syncModelView];
472}
473
475{
476 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
477 [matrixManager translateModelView:vector];
478 [matrixManager syncModelView];
479}
480
481void OOGLRotateModelView(GLfloat angle, Vector axis)
482{
483 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
484 [matrixManager rotateModelView:angle axis:axis];
485 [matrixManager syncModelView];
486}
487
489{
490 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
491 [matrixManager scaleModelView:scale];
492 [matrixManager syncModelView];
493}
494
495void OOGLLookAt(Vector eye, Vector center, Vector up)
496{
497 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
498 [matrixManager lookAtWithEye:eye center:center up:up];
499 [matrixManager syncModelView];
500}
501
503{
504 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
505 [matrixManager resetProjection];
506 [matrixManager syncProjection];
507}
508
510{
511 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
512 [matrixManager pushProjection];
513 [matrixManager syncProjection];
514}
515
517{
518 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
519 OOMatrix matrix = [matrixManager popProjection];
520 [matrixManager syncProjection];
521 return matrix;
522}
523
525{
526 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
527 OOMatrix matrix = [matrixManager getProjection];
528 return matrix;
529}
530
532{
533 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
534 [matrixManager loadProjection:matrix];
535 [matrixManager syncProjection];
536}
537
539{
540 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
541 [matrixManager multProjection:matrix];
542 [matrixManager syncProjection];
543}
544
546{
547 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
548 [matrixManager translateProjection:vector];
549 [matrixManager syncProjection];
550}
551
552void OOGLRotateProjection(GLfloat angle, Vector axis)
553{
554 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
555 [matrixManager rotateProjection:angle axis:axis];
556 [matrixManager syncProjection];
557}
558
560{
561 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
562 [matrixManager scaleProjection:scale];
563 [matrixManager syncProjection];
564}
565
566void OOGLFrustum(double frleft, double frright, double frbottom, double frtop, double frnear, double frfar)
567{
568 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
569 [matrixManager frustumLeft:frleft right:frright bottom:frbottom top:frtop near:frnear far:frfar];
570 [matrixManager syncProjection];
571}
572
573void OOGLOrtho(double orleft, double orright, double orbottom, double ortop, double ornear, double orfar)
574{
575 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
576 [matrixManager orthoLeft:orleft right:orright bottom:orbottom top:ortop near:ornear far:orfar];
577 [matrixManager syncProjection];
578}
579
580void OOGLPerspective(double fovy, double aspect, double zNear, double zFar)
581{
582 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
583 [matrixManager perspectiveFovy:fovy aspect:aspect zNear:zNear zFar:zFar];
584 [matrixManager syncProjection];
585}
586
588{
589 OOOpenGLMatrixManager* matrixManager = [[UNIVERSE gameView] getOpenGLMatrixManager];
590 return [matrixManager getMatrix:OOLITE_GL_MATRIX_MODELVIEW_PROJECTION];
591}
#define OO_ENTER_OPENGL()
GLfloat OOScalar
Definition OOMaths.h:49
#define M_PI
Definition OOMaths.h:54
OOINLINE OOMatrix OOMatrixConstruct(OOScalar aa, OOScalar ab, OOScalar ac, OOScalar ad, OOScalar ba, OOScalar bb, OOScalar bc, OOScalar bd, OOScalar ca, OOScalar cb, OOScalar cc, OOScalar cd, OOScalar da, OOScalar db, OOScalar dc, OOScalar dd) INLINE_CONST_FUNC
Definition OOMatrix.h:158
const OOMatrix kIdentityMatrix
Definition OOMatrix.m:28
OOMatrix OOMatrixInverseWithDeterminant(OOMatrix M, OOScalar *determinant)
Definition OOMatrix.m:364
#define GLLoadOOMatrix(M)
Definition OOMatrix.h:98
OOMatrix OOMatrixInverse(OOMatrix M)
Definition OOMatrix.m:359
OOINLINE OOMatrix OOMatrixTranspose(OOMatrix m) INLINE_CONST_FUNC
Definition OOMatrix.h:327
OOMatrix OOMatrixMultiply(OOMatrix a, OOMatrix b) CONST_FUNC
Definition OOMatrix.m:105
@ 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_END
@ 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()
void OOGLPushModelView()
#define OOGL(statement)
Definition OOOpenGL.h:262
unsigned count
OOINLINE Vector vector_subtract(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:218
OOINLINE Vector cross_product(Vector first, Vector second) INLINE_CONST_FUNC
Definition OOVector.h:294
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
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)
OOMatrix matrices[OOLITE_GL_MATRIX_END]
OOOpenGLMatrixStack * projectionStack
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)
void scaleModelView:(Vector scale)
void multProjection:(OOMatrix matrix)
OOOpenGLMatrixStack * modelViewStack
BOOL valid[OOLITE_GL_MATRIX_END]
void push:(OOMatrix matrix)
OOScalar m[4][4]
Definition OOMatrix.h:31
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34