Use matrix manager for OpenGL matrices

This commit is contained in:
Kevin Anthoney 2014-07-05 20:30:14 +01:00
parent 36a0522e86
commit 91d583ae22
17 changed files with 337 additions and 147 deletions

View File

@ -30,6 +30,7 @@ MA 02110-1301, USA.
#import "Universe.h" #import "Universe.h"
#import "OOMacroOpenGL.h" #import "OOMacroOpenGL.h"
#import "PlayerEntity.h" #import "PlayerEntity.h"
#import "OOOpenGLMatrixManager.h"
#import "OOTexture.h" #import "OOTexture.h"
#import "OOGraphicsResetManager.h" #import "OOGraphicsResetManager.h"
@ -331,6 +332,7 @@ static GLfloat pA[6] = { 0.01, 0.0, 2.0, 4.0, 6.0, 10.0 }; // phase adjustments
- (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent - (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent
{ {
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (!translucent) return; if (!translucent) return;
ShipEntity *ship = [self owner]; ShipEntity *ship = [self owner];
@ -339,8 +341,9 @@ static GLfloat pA[6] = { 0.01, 0.0, 2.0, 4.0, 6.0, 10.0 }; // phase adjustments
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
OOSetOpenGLState(OPENGL_STATE_ADDITIVE_BLENDING); OOSetOpenGLState(OPENGL_STATE_ADDITIVE_BLENDING);
OOGL(glPopMatrix()); // restore absolute positioning [matrixManager popModelView];
OOGL(glPushMatrix()); // avoid stack underflow [matrixManager pushModelView];
[matrixManager syncModelView];
// GLTranslateOOVector(vector_flip([self cameraRelativePosition])); // GLTranslateOOVector(vector_flip([self cameraRelativePosition]));
HPVector cam = [PLAYER viewpointPosition]; HPVector cam = [PLAYER viewpointPosition];
for (unsigned n=0;n<34*3;n++) for (unsigned n=0;n<34*3;n++)

View File

@ -31,6 +31,8 @@ MA 02110-1301, USA.
#import "OOTexture.h" #import "OOTexture.h"
#import "OOGraphicsResetManager.h" #import "OOGraphicsResetManager.h"
#import "OOOpenGLMatrixManager.h"
#define kLaserDuration (0.09) // seconds #define kLaserDuration (0.09) // seconds
@ -190,7 +192,8 @@ static const GLfloat kLaserVertices[] =
- (void) drawImmediate:(bool)immediate translucent:(bool)translucent - (void) drawImmediate:(bool)immediate translucent:(bool)translucent
{ {
if (!translucent || [UNIVERSE breakPatternHide]) return; if (!translucent || [UNIVERSE breakPatternHide]) return;
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
OOSetOpenGLState(OPENGL_STATE_ADDITIVE_BLENDING); OOSetOpenGLState(OPENGL_STATE_ADDITIVE_BLENDING);
@ -201,7 +204,7 @@ static const GLfloat kLaserVertices[] =
*/ */
OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY)); OOGL(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
OOGL(glEnable(GL_TEXTURE_2D)); OOGL(glEnable(GL_TEXTURE_2D));
OOGL(glPushMatrix()); [matrixManager pushModelView];
[[self texture1] apply]; [[self texture1] apply];
GLfloat s = sin([UNIVERSE getTime]); GLfloat s = sin([UNIVERSE getTime]);
@ -239,7 +242,8 @@ static const GLfloat kLaserVertices[] =
glTexCoordPointer(2, GL_FLOAT, 0, laserTexCoords); glTexCoordPointer(2, GL_FLOAT, 0, laserTexCoords);
glDrawArrays(GL_QUADS, 0, 8); glDrawArrays(GL_QUADS, 0, 8);
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY)); OOGL(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
OOGL(glDisable(GL_TEXTURE_2D)); OOGL(glDisable(GL_TEXTURE_2D));

View File

@ -32,6 +32,7 @@ MA 02110-1301, USA.
#import "OOFunctionAttributes.h" #import "OOFunctionAttributes.h"
#import "OOMacroOpenGL.h" #import "OOMacroOpenGL.h"
#import "OOGraphicsResetManager.h" #import "OOGraphicsResetManager.h"
#import "OOOpenGLMatrixManager.h"
#define PARTICLE_DISTANCE_SCALE_LOW 12.0 #define PARTICLE_DISTANCE_SCALE_LOW 12.0
@ -112,6 +113,7 @@ static OOTexture *sBlobTexture = nil;
Entity *father = [self owner]; Entity *father = [self owner];
Entity *last = nil; Entity *last = nil;
HPVector abspos = position; HPVector abspos = position;
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
while (father != nil && father != last && father != NO_TARGET) while (father != nil && father != last && father != NO_TARGET)
{ {
@ -123,13 +125,15 @@ static OOTexture *sBlobTexture = nil;
father = [father owner]; father = [father owner];
} }
OOMatrix temp_matrix = OOMatrixLoadGLMatrix(GL_MODELVIEW_MATRIX); OOMatrix temp_matrix = [matrixManager getModelView];
OOGL(glPopMatrix()); OOGL(glPushMatrix()); // restore zero! [matrixManager popModelView];
[matrixManager pushModelView];
GLTranslateOOVector(HPVectorToVector(HPvector_subtract(abspos,[PLAYER viewpointPosition]))); // move to camera-relative position [matrixManager translateModelView:HPVectorToVector(HPvector_subtract(abspos,[PLAYER viewpointPosition]))]; // move to camera-relative position
GLLoadOOMatrix([matrixManager getModelView]);
[self drawImmediate:immediate translucent:translucent]; [self drawImmediate:immediate translucent:translucent];
GLLoadOOMatrix(temp_matrix); [matrixManager loadModelView: temp_matrix];
} }

View File

@ -29,6 +29,7 @@ MA 02110-1301, USA.
#import "PlayerEntity.h" #import "PlayerEntity.h"
#import "OOLightParticleEntity.h" #import "OOLightParticleEntity.h"
#import "OOMacroOpenGL.h" #import "OOMacroOpenGL.h"
#import "OOOpenGLMatrixManager.h"
// Testing toy: cause particle systems to stop after half a second. // Testing toy: cause particle systems to stop after half a second.
@ -160,11 +161,14 @@ do { \
GLfloat (*particleColor)[4] = _particleColor; GLfloat (*particleColor)[4] = _particleColor;
GLfloat *particleSize = _particleSize; GLfloat *particleSize = _particleSize;
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if ([UNIVERSE reducedDetail]) if ([UNIVERSE reducedDetail])
{ {
// Quick rendering - particle cloud is effectively a 2D billboard. // Quick rendering - particle cloud is effectively a 2D billboard.
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLMultOOMatrix(OOMatrixForBillboard(selfPosition, viewPosition)); [matrixManager multModelView: OOMatrixForBillboard(selfPosition, viewPosition)];
[matrixManager syncModelView];
OOGLBEGIN(GL_QUADS); OOGLBEGIN(GL_QUADS);
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
@ -174,7 +178,8 @@ do { \
} }
OOGLEND(); OOGLEND();
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
else else
{ {
@ -192,16 +197,18 @@ do { \
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLTranslateOOVector(particlePosition[i]); [matrixManager translateModelView: particlePosition[i]];
GLMultOOMatrix(bbMatrix); [matrixManager multModelView: bbMatrix];
[matrixManager syncModelView];
glColor4fv(particleColor[i]); glColor4fv(particleColor[i]);
OOGLBEGIN(GL_QUADS); OOGLBEGIN(GL_QUADS);
DrawQuadForView(0, 0, 0, particleSize[i]); DrawQuadForView(0, 0, 0, particleSize[i]);
OOGLEND(); OOGLEND();
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
} }
else else
@ -215,16 +222,17 @@ do { \
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLTranslateOOVector(particlePosition[i]); [matrixManager translateModelView: particlePosition[i]];
GLMultOOMatrix(OOMatrixForBillboard(HPvector_add(selfPosition, vectorToHPVector(vector_multiply_scalar(particlePosition[i], individuality))), viewPosition)); [matrixManager multModelView: OOMatrixForBillboard(HPvector_add(selfPosition, vectorToHPVector(vector_multiply_scalar(particlePosition[i], individuality))), viewPosition)];
glColor4fv(particleColor[i]); glColor4fv(particleColor[i]);
OOGLBEGIN(GL_QUADS); OOGLBEGIN(GL_QUADS);
DrawQuadForView(0, 0, 0, particleSize[i]); DrawQuadForView(0, 0, 0, particleSize[i]);
OOGLEND(); OOGLEND();
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
} }

View File

@ -51,6 +51,8 @@ MA 02110-1301, USA.
#import "OOFilteringEnumerator.h" #import "OOFilteringEnumerator.h"
#import "OOOpenGLMatrixManager.h"
@interface OOVisualEffectEntity (Private) @interface OOVisualEffectEntity (Private)
- (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent; - (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent;
@ -389,17 +391,20 @@ MA 02110-1301, USA.
- (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent - (void) drawSubEntityImmediate:(bool)immediate translucent:(bool)translucent
{ {
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (cam_zero_distance > no_draw_distance) // this test provides an opportunity to do simple LoD culling if (cam_zero_distance > no_draw_distance) // this test provides an opportunity to do simple LoD culling
{ {
return; // TOO FAR AWAY return; // TOO FAR AWAY
} }
OOGL(glPushMatrix()); [matrixManager pushModelView];
// HPVect: camera position // HPVect: camera position
GLTranslateOOVector(HPVectorToVector(position)); [matrixManager translateModelView: HPVectorToVector(position)];
GLMultOOMatrix(rotMatrix); [matrixManager multModelView: rotMatrix];
[matrixManager syncModelView];
[self drawImmediate:immediate translucent:translucent]; [self drawImmediate:immediate translucent:translucent];
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
@ -640,18 +645,21 @@ static GLfloat scripted_color[4] = { 0.0, 0.0, 0.0, 0.0};
- (void) drawImmediate:(bool)immediate translucent:(bool)translucent - (void) drawImmediate:(bool)immediate translucent:(bool)translucent
{ {
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (no_draw_distance < cam_zero_distance) if (no_draw_distance < cam_zero_distance)
{ {
return; // too far away to draw return; // too far away to draw
} }
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glScalef(scaleX,scaleY,scaleZ)); [matrixManager scaleModelView: make_vector(scaleX,scaleY,scaleZ)];
[matrixManager syncModelView];
if ([self mesh] != nil) if ([self mesh] != nil)
{ {
[super drawImmediate:immediate translucent:translucent]; [super drawImmediate:immediate translucent:translucent];
} }
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
// Draw subentities. // Draw subentities.
if (!immediate) // TODO: is this relevant any longer? if (!immediate) // TODO: is this relevant any longer?

View File

@ -43,6 +43,7 @@ MA 02110-1301, USA.
#import "OOCollectionExtractors.h" #import "OOCollectionExtractors.h"
#import "OODebugFlags.h" #import "OODebugFlags.h"
#import "OOGraphicsResetManager.h" #import "OOGraphicsResetManager.h"
#import "OOOpenGLMatrixManager.h"
#if !OOLITE_MAC_OS_X #if !OOLITE_MAC_OS_X
@ -786,6 +787,7 @@ static const BaseFace kTexturedFaces[][3] =
double drawFactor = [[UNIVERSE gameView] viewSize].width / 100.0; double drawFactor = [[UNIVERSE gameView] viewSize].width / 100.0;
double drawRatio2 = drawFactor * collision_radius / sqrt_zero_distance; // equivalent to size on screen in pixels double drawRatio2 = drawFactor * collision_radius / sqrt_zero_distance; // equivalent to size on screen in pixels
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (cam_zero_distance > 0.0) if (cam_zero_distance > 0.0)
{ {
@ -835,7 +837,8 @@ static const BaseFace kTexturedFaces[][3] =
{ {
subdivideLevel = root_planet->lastSubdivideLevel; // copy it from the planet (stops jerky LOD and such) subdivideLevel = root_planet->lastSubdivideLevel; // copy it from the planet (stops jerky LOD and such)
} }
GLMultOOMatrix(rotMatrix); // rotate the clouds! [matrixManager multModelVew: rotMatrix]; // rotate the clouds!
[matrixManager syncModelView];
OOGL(glEnable(GL_BLEND)); OOGL(glEnable(GL_BLEND));
// OOGL(glDisable(GL_LIGHTING)); // OOGL(glDisable(GL_LIGHTING));
// Fall through. // Fall through.
@ -977,8 +980,11 @@ static const BaseFace kTexturedFaces[][3] =
if (atmosphere) if (atmosphere)
{ {
OOGL(glPopMatrix()); // get old draw matrix back [matrixManager popModelView];
OOGL(glPushMatrix()); // and store it again [matrixManager pushModelView];
[matrixManager syncModelView];
//OOGL(glPopMatrix()); // get old draw matrix back
//OOGL(glPushMatrix()); // and store it again
OOGL(glTranslatef(cameraRelativePosition.x,cameraRelativePosition.y,cameraRelativePosition.z)); // centre on the planet OOGL(glTranslatef(cameraRelativePosition.x,cameraRelativePosition.y,cameraRelativePosition.z)); // centre on the planet
// rotate // rotate
// GLMultOOMatrix([atmosphere rotationMatrix]); // GLMultOOMatrix([atmosphere rotationMatrix]);

View File

@ -91,6 +91,8 @@ MA 02110-1301, USA.
#import "OOJSVector.h" #import "OOJSVector.h"
#import "OOJSEngineTimeManagement.h" #import "OOJSEngineTimeManagement.h"
#import "OOOpenGLMatrixManager.h"
#define USEMASC 1 #define USEMASC 1
@ -5872,8 +5874,9 @@ ShipEntity* doOctreesCollide(ShipEntity* prime, ShipEntity* other)
{ {
return; // TOO FAR AWAY return; // TOO FAR AWAY
} }
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glPushMatrix()); [matrixManager pushModelView];
if ([self status] == STATUS_ACTIVE) if ([self status] == STATUS_ACTIVE)
{ {
@ -5892,17 +5895,18 @@ ShipEntity* doOctreesCollide(ShipEntity* prime, ShipEntity* other)
} */ } */
HPVector abspos = [self absolutePositionForSubentity]; HPVector abspos = [self absolutePositionForSubentity];
GLLoadOOMatrix([UNIVERSE viewMatrix]); [matrixManager loadModelView: [UNIVERSE viewMatrix]];
// HPVect: need to make camera-relative // HPVect: need to make camera-relative
GLTranslateOOVector(HPVectorToVector(abspos)); [matrixManager translateModelView:HPVectorToVector(abspos)];
} }
else else
{ {
// HPVect: need to make camera-relative // HPVect: need to make camera-relative
GLTranslateOOVector(HPVectorToVector(position)); [matrixManager translateModelView: HPVectorToVector(position)];
} }
GLMultOOMatrix(rotMatrix); [matrixManager multModelView: rotMatrix];
[matrixManager syncModelView];
[self drawImmediate:immediate translucent:translucent]; [self drawImmediate:immediate translucent:translucent];
#ifndef NDEBUG #ifndef NDEBUG
@ -5912,7 +5916,8 @@ ShipEntity* doOctreesCollide(ShipEntity* prime, ShipEntity* other)
} }
#endif #endif
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
OOVerifyOpenGLState(); OOVerifyOpenGLState();
} }

View File

@ -42,6 +42,7 @@ MA 02110-1301, USA.
#import "OODebugSupport.h" #import "OODebugSupport.h"
#import "legacy_random.h" #import "legacy_random.h"
#import "OOOXZManager.h" #import "OOOXZManager.h"
#import "OOOpenGLMatrixManager.h"
#if OOLITE_MAC_OS_X #if OOLITE_MAC_OS_X
#import "JAPersistentFileReference.h" #import "JAPersistentFileReference.h"
@ -901,6 +902,7 @@ static NSMutableArray *sMessageStack;
- (void)setUpBasicOpenGLStateWithSize:(NSSize)viewSize - (void)setUpBasicOpenGLStateWithSize:(NSSize)viewSize
{ {
OOOpenGLExtensionManager *extMgr = [OOOpenGLExtensionManager sharedManager]; OOOpenGLExtensionManager *extMgr = [OOOpenGLExtensionManager sharedManager];
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
float ratio = 0.5; float ratio = 0.5;
float aspect = viewSize.height/viewSize.width; float aspect = viewSize.height/viewSize.width;
@ -911,12 +913,18 @@ static NSMutableArray *sMessageStack;
OOGL(glClearDepth(MAX_CLEAR_DEPTH)); OOGL(glClearDepth(MAX_CLEAR_DEPTH));
OOGL(glViewport(0, 0, viewSize.width, viewSize.height)); OOGL(glViewport(0, 0, viewSize.width, viewSize.height));
OOGL(glMatrixMode(GL_PROJECTION)); [matrixManager resetProjection];
OOGL(glLoadIdentity()); // reset matrix [matrixManager syncProjection];
OOGL(glFrustum(-ratio, ratio, -aspect*ratio, aspect*ratio, 1.0, MAX_CLEAR_DEPTH)); // set projection matrix OOGL(glLoadIdentity());
OOGL(glOrtho(0.0f, 640.0f, 480.0f, 0.0f, -1.0f, 1.0f));
OOGL(glMatrixMode(GL_MODELVIEW)); [matrixManager orthoLeft: 0.0 right: 640 bottom: 480.0f top: 0.0 near: -1.0 far: 1.0];
OOLog(@"kja", @"Projection matrix: %@", OOMatrixDescription([matrixManager getProjection]));
OOLog(@"kja", @"OpenGL Projection matrix: %@", OOMatrixDescription(OOMatrixLoadGLMatrix(GL_PROJECTION_MATRIX)));
[matrixManager resetProjection]; // reset matrix
[matrixManager frustumLeft:-ratio right:ratio bottom: -aspect*ratio top: aspect*ratio near: 1.0 far: MAX_CLEAR_DEPTH]; // set projection matrix
[matrixManager syncProjection];
OOGL(glDepthFunc(GL_LESS)); // depth buffer OOGL(glDepthFunc(GL_LESS)); // depth buffer
if (UNIVERSE) if (UNIVERSE)

View File

@ -45,6 +45,7 @@ MA 02110-1301, USA.
#import "OOStringParsing.h" #import "OOStringParsing.h"
#import "OOJoystickManager.h" #import "OOJoystickManager.h"
#import "OOJavaScriptEngine.h" #import "OOJavaScriptEngine.h"
#import "OOOpenGLMatrixManager.h"
#define ONE_SIXTEENTH 0.0625 #define ONE_SIXTEENTH 0.0625
@ -1777,9 +1778,11 @@ OOINLINE void SetCompassBlipColor(GLfloat relativeZ, GLfloat alpha)
} }
OOGLEND(); OOGLEND();
#else #else
OOGL(glPushMatrix()); OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glTranslatef(x, y, z1)); [matrixManager pushModelView];
OOGL(glScalef(w, -h, 1.0f)); [matrixManager translateModelView: make_vector(x, y, z1)];
[matrixManager scaleModelView: make_vector(w, -h, 1.0f)];
[matrixManager syncModelView];
OOGL(glColor4f(0.0f, 1.0f, 0.0f, alpha)); OOGL(glColor4f(0.0f, 1.0f, 0.0f, alpha));
OOGL(glVertexPointer(2, GL_FLOAT, 0, strip)); OOGL(glVertexPointer(2, GL_FLOAT, 0, strip));
@ -1788,8 +1791,9 @@ OOINLINE void SetCompassBlipColor(GLfloat relativeZ, GLfloat alpha)
OOGL(glDrawArrays(GL_QUAD_STRIP, 0, sizeof strip / sizeof *strip / 2)); OOGL(glDrawArrays(GL_QUAD_STRIP, 0, sizeof strip / sizeof *strip / 2));
OOGL(glDisableClientState(GL_VERTEX_ARRAY)); OOGL(glDisableClientState(GL_VERTEX_ARRAY));
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
#endif #endif
} }
@ -2334,24 +2338,27 @@ static OOPolygonSprite *IconForMissileRole(NSString *role)
width:(GLfloat)width height:(GLfloat)height alpha:(GLfloat)alpha width:(GLfloat)width height:(GLfloat)height alpha:(GLfloat)alpha
{ {
OOPolygonSprite *sprite = IconForMissileRole([missile primaryRole]); OOPolygonSprite *sprite = IconForMissileRole([missile primaryRole]);
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (selected) if (selected)
{ {
// Draw yellow outline. // Draw yellow outline.
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(x - width * 2.0f, y - height * 2.0f, z1)); [matrixManager translateModelView: make_vector(x - width * 2.0f, y - height * 2.0f, z1)];
OOGL(glScalef(width, height, 1.0f)); [matrixManager scaleModelView: make_vector(width, height, 1.0f)];
[matrixManager syncModelView];
GLColorWithOverallAlpha(yellow_color, alpha); GLColorWithOverallAlpha(yellow_color, alpha);
[sprite drawOutline]; [sprite drawOutline];
OOGL(glPopMatrix()); [matrixManager popModelView];
// Draw black backing, so outline colour isnt blended into missile colour. // Draw black backing, so outline colour isnt blended into missile colour.
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(x - width * 2.0f, y - height * 2.0f, z1)); [matrixManager translateModelView: make_vector(x - width * 2.0f, y - height * 2.0f, z1)];
OOGL(glScalef(width, height, 1.0f)); [matrixManager scaleModelView: make_vector(width, height, 1.0f)];
[matrixManager syncModelView];
GLColorWithOverallAlpha(black_color, alpha); GLColorWithOverallAlpha(black_color, alpha);
[sprite drawFilled]; [sprite drawFilled];
OOGL(glPopMatrix()); [matrixManager popModelView];
switch (status) switch (status)
{ {
@ -2369,11 +2376,13 @@ static OOPolygonSprite *IconForMissileRole(NSString *role)
else GLColorWithOverallAlpha(red_color, alpha); else GLColorWithOverallAlpha(red_color, alpha);
} }
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(x - width * 2.0f, y - height * 2.0f, z1)); [matrixManager translateModelView: make_vector(x - width * 2.0f, y - height * 2.0f, z1)];
OOGL(glScalef(width, height, 1.0f)); [matrixManager scaleModelView: make_vector(width, height, 1.0f)];
[matrixManager syncModelView];
[sprite drawFilled]; [sprite drawFilled];
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
@ -2382,14 +2391,17 @@ static OOPolygonSprite *IconForMissileRole(NSString *role)
width:(GLfloat)width height:(GLfloat)height alpha:(GLfloat)alpha width:(GLfloat)width height:(GLfloat)height alpha:(GLfloat)alpha
{ {
OOPolygonSprite *sprite = IconForMissileRole(kDefaultMissileIconKey); OOPolygonSprite *sprite = IconForMissileRole(kDefaultMissileIconKey);
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
// Draw gray outline. // Draw gray outline.
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(x - width * 2.0f, y - height * 2.0f, z1)); [matrixManager translateModelView: make_vector(x - width * 2.0f, y - height * 2.0f, z1)];
OOGL(glScalef(width, height, 1.0f)); [matrixManager scaleModelView: make_vector(width, height, 1.0f)];
[matrixManager syncModelView];
GLColorWithOverallAlpha(lightgray_color, alpha); GLColorWithOverallAlpha(lightgray_color, alpha);
[sprite drawOutline]; [sprite drawOutline];
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
@ -3247,6 +3259,8 @@ static void hudDrawReticleOnTarget(Entity *target, PlayerEntity *player1, GLfloa
NSString *legal_desc = nil; NSString *legal_desc = nil;
GLfloat scale = [info oo_floatForKey:@"reticle_scale" defaultValue:ONE_SIXTYFOURTH]; GLfloat scale = [info oo_floatForKey:@"reticle_scale" defaultValue:ONE_SIXTYFOURTH];
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (target == nil || player1 == nil) return; if (target == nil || player1 == nil) return;
@ -3456,7 +3470,8 @@ static void hudDrawReticleOnTarget(Entity *target, PlayerEntity *player1, GLfloa
} }
} }
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
@ -3466,6 +3481,8 @@ static void hudDrawWaypoint(OOWaypointEntity *waypoint, PlayerEntity *player1, G
{ {
return; return;
} }
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
Vector p1 = HPVectorToVector(HPvector_subtract([waypoint position], [player1 viewpointPosition])); Vector p1 = HPVectorToVector(HPvector_subtract([waypoint position], [player1 viewpointPosition]));
@ -3516,7 +3533,8 @@ static void hudDrawWaypoint(OOWaypointEntity *waypoint, PlayerEntity *player1, G
OODrawString(infoline, rs0 * 0.5, -rs2 - line_height, 0, textsize); OODrawString(infoline, rs0 * 0.5, -rs2 - line_height, 0, textsize);
} }
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
static void hudRotateViewpointForVirtualDepth(PlayerEntity * player1, Vector p1) static void hudRotateViewpointForVirtualDepth(PlayerEntity * player1, Vector p1)
@ -3525,8 +3543,9 @@ static void hudRotateViewpointForVirtualDepth(PlayerEntity * player1, Vector p1)
Quaternion back_q = [player1 orientation]; Quaternion back_q = [player1 orientation];
back_q.w = -back_q.w; // invert back_q.w = -back_q.w; // invert
Vector v1 = vector_up_from_quaternion(back_q); Vector v1 = vector_up_from_quaternion(back_q);
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glPushMatrix()); [matrixManager pushModelView];
// deal with view directions // deal with view directions
Vector view_dir, view_up = kBasisYVector; Vector view_dir, view_up = kBasisYVector;
@ -3558,17 +3577,18 @@ static void hudRotateViewpointForVirtualDepth(PlayerEntity * player1, Vector p1)
back_q = quaternion_multiply([player1 customViewQuaternion], back_q); back_q = quaternion_multiply([player1 customViewQuaternion], back_q);
break; break;
} }
OOGL(gluLookAt(view_dir.x, view_dir.y, view_dir.z, 0.0, 0.0, 0.0, view_up.x, view_up.y, view_up.z)); [matrixManager lookAtWithEye: view_dir center: kZeroVector up: view_up];
back_mat = OOMatrixForQuaternionRotation(back_q); back_mat = OOMatrixForQuaternionRotation(back_q);
// rotate the view // rotate the view
GLMultOOMatrix([player1 rotationMatrix]); [matrixManager multModelView: [player1 rotationMatrix]];
// translate the view // translate the view
OOGL(glTranslatef(p1.x, p1.y, p1.z)); [matrixManager translateModelView: p1];
//rotate to face player1 //rotate to face player1
GLMultOOMatrix(back_mat); [matrixManager multModelView: back_mat];
// draw the waypoint // draw the waypoint
[matrixManager syncModelView];
} }
@ -4103,14 +4123,17 @@ static void DrawSpecialOval(GLfloat x, GLfloat y, GLfloat z, NSSize siz, GLfloat
GLfloat oy = y - size.height * 0.5; GLfloat oy = y - size.height * 0.5;
GLfloat width = size.width * (1.0f / 6.0f); GLfloat width = size.width * (1.0f / 6.0f);
GLfloat height = size.height * (1.0f / 6.0f); GLfloat height = size.height * (1.0f / 6.0f);
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(ox, oy, z)); [matrixManager translateModelView: make_vector(ox, oy, z)];
OOGL(glScalef(width, height, 1.0f)); [matrixManager scaleModelView: make_vector(width, height, 1.0f)];
[matrixManager syncModelView];
[self drawFilled]; [self drawFilled];
glColor4f(0.0, 0.0, 0.0, 0.5 * alpha); glColor4f(0.0, 0.0, 0.0, 0.5 * alpha);
[self drawOutline]; [self drawOutline];
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
} }
@end @end

View File

@ -32,6 +32,7 @@ SOFTWARE.
#import "Universe.h" #import "Universe.h"
#import "MyOpenGLView.h" #import "MyOpenGLView.h"
#import "OOMacroOpenGL.h" #import "OOMacroOpenGL.h"
#import "OOOpenGLMatrixManager.h"
@interface OOCrosshairs (Private) @interface OOCrosshairs (Private)
@ -83,12 +84,14 @@ SOFTWARE.
{ {
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
OOSetOpenGLState(OPENGL_STATE_OVERLAY); OOSetOpenGLState(OPENGL_STATE_OVERLAY);
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glPushAttrib(GL_ENABLE_BIT)); OOGL(glPushAttrib(GL_ENABLE_BIT));
OOGL(glDisable(GL_LIGHTING)); OOGL(glDisable(GL_LIGHTING));
OOGL(glDisable(GL_TEXTURE_2D)); OOGL(glDisable(GL_TEXTURE_2D));
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef(0, 0, [[UNIVERSE gameView] display_z])); [matrixManager translateModelView: make_vector(0.0, 0.0, [[UNIVERSE gameView] display_z])];
[matrixManager syncModelView];
OOGL(glVertexPointer(2, GL_FLOAT, sizeof (GLfloat) * 6, _data)); OOGL(glVertexPointer(2, GL_FLOAT, sizeof (GLfloat) * 6, _data));
OOGL(glColorPointer(4, GL_FLOAT, sizeof (GLfloat) * 6, _data + 2)); OOGL(glColorPointer(4, GL_FLOAT, sizeof (GLfloat) * 6, _data + 2));
@ -101,7 +104,8 @@ SOFTWARE.
OOGL(glDisableClientState(GL_VERTEX_ARRAY)); OOGL(glDisableClientState(GL_VERTEX_ARRAY));
OOGL(glDisableClientState(GL_COLOR_ARRAY)); OOGL(glDisableClientState(GL_COLOR_ARRAY));
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
OOGL(glPopAttrib()); OOGL(glPopAttrib());
OOVerifyOpenGLState(); OOVerifyOpenGLState();

View File

@ -39,6 +39,7 @@ SOFTWARE.
#import "OOPlanetEntity.h" #import "OOPlanetEntity.h"
#import "OODrawable.h" #import "OODrawable.h"
#import "OOEntityFilterPredicate.h" #import "OOEntityFilterPredicate.h"
#import "OOOpenGLMatrixManager"
#if OO_USE_FBO && OO_TEXTURE_CUBE_MAP #if OO_USE_FBO && OO_TEXTURE_CUBE_MAP
@ -81,19 +82,19 @@ SOFTWARE.
- (void) render - (void) render
{ {
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
if (_textureName == 0) [self setUp]; if (_textureName == 0) [self setUp];
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
// Save stuff. // Save stuff.
OOGL(glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT)); OOGL(glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT));
OOGL(glMatrixMode(GL_MODELVIEW)); [matrixManager pushModelView];
OOGL(glPushMatrix()); [matrixManager pushProjection];
OOGL(glMatrixMode(GL_PROJECTION));
OOGL(glPushMatrix());
OOGL(glViewport(0, 0, _size, _size)); OOGL(glViewport(0, 0, _size, _size));
[matrixManager scaleProjection: make_vector(-1.0, 1.0, 1.0)];
OOGL(glScalef(-1.0, 1.0, 1.0)); // flip left and right OOGL(glScalef(-1.0, 1.0, 1.0)); // flip left and right
/* TODO: once confirmed working (and rendering everything in the right /* TODO: once confirmed working (and rendering everything in the right
@ -106,8 +107,12 @@ SOFTWARE.
...and appropriate rotations thereof. ...and appropriate rotations thereof.
*/ */
[matrixManager resetProjection];
OOGL(glLoadIdentity()); OOGL(glLoadIdentity());
OOGL(gluPerspective(90.0, 1.0, 1.0, MAX_CLEAR_DEPTH)); OOGL(gluPerspective(90.0, 1.0, 1.0, MAX_CLEAR_DEPTH));
[matrixManager perspectiveFovy: 90.0 aspect: 1.0 zNear: 1.0 zFar: MAX_CLEAR_DEPTH];
OOLog(@"kja", @"Projection matrix: %@", OOMatrixDescription([matrixManager getProjection]));
OOLog(@"kja", @"OpenGL Projection matrix: %@", OOMatrixDescription(OOMatrixLoadGLMatrix(GL_PROJECTION_MATRIX)));
OODrawable *sky = [[UNIVERSE nearestEntityMatchingPredicate:HasClassPredicate parameter:[SkyEntity class] relativeToEntity:nil] drawable]; OODrawable *sky = [[UNIVERSE nearestEntityMatchingPredicate:HasClassPredicate parameter:[SkyEntity class] relativeToEntity:nil] drawable];
OOSunEntity *sun = [UNIVERSE sun]; OOSunEntity *sun = [UNIVERSE sun];
@ -119,26 +124,23 @@ SOFTWARE.
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++)
{ {
OOGL(glPushMatrix()); [matrixManager pushProjection];
Vector center = centers[i]; Vector center = centers[i];
Vector up = ups[i]; Vector up = ups[i];
OOGL(gluLookAt(0, 0, 0, center.x, center.y, center.z, up.x, up.y, up.z)); [matrixManager lookAtWithEye: kZeroVector, center: center up: up];
[matrixManager syncProjection];
OOGL(glMatrixMode(GL_MODELVIEW));
OOGL(glPushMatrix());
OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i])); OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
[self renderOnePassWithSky:sky sun:sun planets:planets]; [self renderOnePassWithSky:sky sun:sun planets:planets];
OOGL(glPopMatrix()); [matrixManager popProjection];
OOGL(glMatrixMode(GL_PROJECTION)); [matrixManager syncProjection];
OOGL(glPopMatrix());
} }
OOGL(glMatrixMode(GL_PROJECTION)); [matrixManager popProjection];
OOGL(glPopMatrix()); [matrixManager syncProjection];
OOGL(glMatrixMode(GL_MODELVIEW)); [matrixManager popModelView];
OOGL(glPopMatrix()); [matrixManager syncModelView];
OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)); OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
OOGL(glPopAttrib()); OOGL(glPopAttrib());
} }
@ -146,6 +148,7 @@ SOFTWARE.
- (void) renderOnePassWithSky:(OODrawable *)sky sun:(OOSunEntity *)sun planets:(NSArray *)planets - (void) renderOnePassWithSky:(OODrawable *)sky sun:(OOSunEntity *)sun planets:(NSArray *)planets
{ {
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
OOGL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); OOGL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
@ -156,27 +159,32 @@ SOFTWARE.
[sky renderOpaqueParts]; [sky renderOpaqueParts];
OOGL(glDepthMask(GL_TRUE)); OOGL(glDepthMask(GL_TRUE));
OOGL(glLoadIdentity()); [matrixManager pushModelView];
GLTranslateOOVector(vector_flip([PLAYER position])); [matrixManager resetModelView];
[matrixManager translateModelView: vector_flip([PLAYER position])];
NSEnumerator *planetEnum = nil; NSEnumerator *planetEnum = nil;
OOPlanetEntity *planet = nil; OOPlanetEntity *planet = nil;
for (planetEnum = [planets objectEnumerator]; (planet = [planetEnum nextObject]); ) for (planetEnum = [planets objectEnumerator]; (planet = [planetEnum nextObject]); )
{ {
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLTranslateOOVector([planet position]); [matrixManager translateModelView: [planet position]];
[matrixManager syncModelView];
#if NEW_PLANETS #if NEW_PLANETS
[[planet drawable] renderOpaqueParts]; [[planet drawable] renderOpaqueParts];
#else #else
[planet drawUnconditionally]; [planet drawUnconditionally];
#endif #endif
OOGL(glPopMatrix()); [matrixManager popModelView];
} }
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLTranslateOOVector([sun position]); [matrixManager translateModelView: [sun position]];
[matrixManager syncModelView];
[sun drawUnconditionally]; [sun drawUnconditionally];
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager popModelView];
[matrixManager syncModelView];
} }
- (void) setUp - (void) setUp

View File

@ -34,6 +34,15 @@ enum
OOLITE_GL_MATRIX_PROJECTION, OOLITE_GL_MATRIX_PROJECTION,
OOLITE_GL_MATRIX_MODELVIEW_PROJECTION, OOLITE_GL_MATRIX_MODELVIEW_PROJECTION,
OOLITE_GL_MATRIX_NORMAL, OOLITE_GL_MATRIX_NORMAL,
OOLITE_GL_MATRIX_MODELVIEW_INVERSE,
OOLITE_GL_MATRIX_PROJECTION_INVERSE,
OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE,
OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE,
OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE,
OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE,
OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE,
OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE,
OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE,
OOLITE_GL_MATRIX_END OOLITE_GL_MATRIX_END
}; };
@ -72,16 +81,20 @@ enum
- (void) pushModelView; - (void) pushModelView;
- (OOMatrix) popModelView; - (OOMatrix) popModelView;
- (OOMatrix) getModelView; - (OOMatrix) getModelView;
- (void) syncModelView;
- (void) loadProjection: (OOMatrix) matrix; - (void) loadProjection: (OOMatrix) matrix;
- (void) multProjection: (OOMatrix) matrix; - (void) multProjection: (OOMatrix) matrix;
- (void) translateProjection: (Vector) vector; - (void) translateProjection: (Vector) vector;
- (void) rotateProjection: (GLfloat) angle axis: (Vector) axis; - (void) rotateProjection: (GLfloat) angle axis: (Vector) axis;
- (void) scaleProjection: (Vector) scale; - (void) scaleProjection: (Vector) scale;
- (void) frustumLeft: (double) l right: (double) r top: (double) t bottom: (double) b near: (double) n far: (double) f; - (void) frustumLeft: (double) l right: (double) r bottom: (double) b top: (double) t near: (double) n far: (double) f;
- (void) orthoLeft: (double) l right: (double) r bottom: (double) b top: (double) t near: (double) n far: (double) f;
- (void) perspectiveFovy: (double) fovy aspect: (double) aspect zNear: (double) zNear zFar: (double) zFar;
- (void) resetProjection; - (void) resetProjection;
- (void) pushProjection; - (void) pushProjection;
- (OOMatrix) popProjection; - (OOMatrix) popProjection;
- (OOMatrix) getProjection; - (OOMatrix) getProjection;
- (void) syncProjection;
- (OOMatrix) getMatrix: (int) which; - (OOMatrix) getMatrix: (int) which;
@end @end

View File

@ -85,11 +85,26 @@ static OOOpenGLMatrixManager * sharedMatrixManager = nil;
{ {
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO; valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
valid[OOLITE_GL_MATRIX_NORMAL] = NO; valid[OOLITE_GL_MATRIX_NORMAL] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
} }
- (void) updateProjection - (void) updateProjection
{ {
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO; valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE] = NO;
valid[OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE] = NO;
} }
@end @end
@ -218,6 +233,13 @@ static OOOpenGLMatrixManager * sharedMatrixManager = nil;
return matrices[OOLITE_GL_MATRIX_MODELVIEW]; return matrices[OOLITE_GL_MATRIX_MODELVIEW];
} }
- (void) syncModelView
{
OOGL(glMatrixMode(GL_MODELVIEW));
GLLoadOOMatrix([self getModelView]);
return;
}
- (void) loadProjection: (OOMatrix) matrix - (void) loadProjection: (OOMatrix) matrix
{ {
matrices[OOLITE_GL_MATRIX_PROJECTION] = matrix; matrices[OOLITE_GL_MATRIX_PROJECTION] = matrix;
@ -250,18 +272,43 @@ static OOOpenGLMatrixManager * sharedMatrixManager = nil;
[self multProjection: OOMatrixForScale(scale.x, scale.y, scale.z)]; [self multProjection: OOMatrixForScale(scale.x, scale.y, scale.z)];
} }
- (void) frustumLeft: (double) l right: (double) r top: (double) t bottom: (double) b near: (double) n far: (double) f - (void) frustumLeft: (double) l right: (double) r bottom: (double) b top: (double) t near: (double) n far: (double) f
{ {
if (l == r || t == b || n == f || n <= 0 || f <= 0 ) return; if (l == r || t == b || n == f || n <= 0 || f <= 0) return;
[self multProjection: OOMatrixConstruct [self multProjection: OOMatrixConstruct
( (
2*n/(l+f), 0.0, 0.0, 0.0, 2*n/(r-l), 0.0, 0.0, 0.0,
0.0, 2*n/(t+b), 0.0, 0.0, 0.0, 2*n/(t-b), 0.0, 0.0,
(r+l)/(r-l), (t+b)/(t-b), -(f+n)/(f-n), -1.0, (r+l)/(r-l), (t+b)/(t-b), -(f+n)/(f-n), -1.0,
0.0, 0.0, -2*f*n/(f-n), 0.0 0.0, 0.0, -2*f*n/(f-n), 0.0
)]; )];
} }
- (void) orthoLeft: (double) l right: (double) r bottom: (double) b top: (double) t near: (double) n far: (double) f
{
if (l == r || t == b || n == f) return;
[self multProjection: OOMatrixConstruct
(
2/(r-l), 0.0, 0.0, 0.0,
0.0, 2/(t-b), 0.0, 0.0,
0.0, 0.0, 2/(n-f), 0.0,
(l+r)/(l-r), (b+t)/(b-t), (n+f)/(n-f), 1.0
)];
}
- (void) perspectiveFovy: (double) fovy aspect: (double) aspect zNear: (double) zNear zFar: (double) zFar
{
if (aspect == 0.0 || zNear == zFar) return;
double f = 1.0/tan(M_PI * fovy / 360);
[self multProjection: OOMatrixConstruct
(
f/aspect, 0.0, 0.0, 0.0,
0.0, f, 0.0, 0.0,
0.0, 0.0, (zFar + zNear)/(zNear - zFar), -1.0,
0.0, 0.0, 2*zFar*zNear/(zNear - zFar), 0.0
)];
}
- (void) resetProjection - (void) resetProjection
{ {
matrices[OOLITE_GL_MATRIX_PROJECTION] = kIdentityMatrix; matrices[OOLITE_GL_MATRIX_PROJECTION] = kIdentityMatrix;
@ -285,6 +332,13 @@ static OOOpenGLMatrixManager * sharedMatrixManager = nil;
return matrices[OOLITE_GL_MATRIX_PROJECTION]; return matrices[OOLITE_GL_MATRIX_PROJECTION];
} }
- (void) syncProjection
{
OOGL(glMatrixMode(GL_PROJECTION));
GLLoadOOMatrix([self getProjection]);
return;
}
- (OOMatrix) getMatrix: (int) which - (OOMatrix) getMatrix: (int) which
{ {
if (which < 0 || which >= OOLITE_GL_MATRIX_END) return kIdentityMatrix; if (which < 0 || which >= OOLITE_GL_MATRIX_END) return kIdentityMatrix;
@ -305,6 +359,33 @@ static OOOpenGLMatrixManager * sharedMatrixManager = nil;
matrices[which].m[3][3] = 1.0; matrices[which].m[3][3] = 1.0;
matrices[which] = OOMatrixTranspose(OOMatrixInverse(matrices[which])); matrices[which] = OOMatrixTranspose(OOMatrixInverse(matrices[which]));
break; break;
case OOLITE_GL_MATRIX_MODELVIEW_INVERSE:
matrices[which] = OOMatrixInverse(matrices[OOLITE_GL_MATRIX_MODELVIEW]);
break;
case OOLITE_GL_MATRIX_PROJECTION_INVERSE:
matrices[which] = OOMatrixInverse(matrices[OOLITE_GL_MATRIX_PROJECTION]);
break;
case OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE:
matrices[which] = OOMatrixInverse([self getMatrix: OOLITE_GL_MATRIX_MODELVIEW_PROJECTION]);
break;
case OOLITE_GL_MATRIX_MODELVIEW_TRANSPOSE:
matrices[which] = OOMatrixTranspose(matrices[OOLITE_GL_MATRIX_MODELVIEW]);
break;
case OOLITE_GL_MATRIX_PROJECTION_TRANSPOSE:
matrices[which] = OOMatrixTranspose(matrices[OOLITE_GL_MATRIX_PROJECTION]);
break;
case OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_TRANSPOSE:
matrices[which] = OOMatrixTranspose([self getMatrix: OOLITE_GL_MATRIX_MODELVIEW_PROJECTION]);
break;
case OOLITE_GL_MATRIX_MODELVIEW_INVERSE_TRANSPOSE:
matrices[which] = OOMatrixTranspose([self getMatrix: OOLITE_GL_MATRIX_MODELVIEW_INVERSE]);
break;
case OOLITE_GL_MATRIX_PROJECTION_INVERSE_TRANSPOSE:
matrices[which] = OOMatrixTranspose([self getMatrix: OOLITE_GL_MATRIX_PROJECTION_INVERSE]);
break;
case OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE_TRANSPOSE:
matrices[which] = OOMatrixTranspose([self getMatrix: OOLITE_GL_MATRIX_MODELVIEW_PROJECTION_INVERSE]);
break;
} }
valid[which] = YES; valid[which] = YES;
return matrices[which]; return matrices[which];

View File

@ -33,6 +33,7 @@
#import "OOMacroOpenGL.h" #import "OOMacroOpenGL.h"
#import "Universe.h" #import "Universe.h"
#import "MyOpenGLView.h" #import "MyOpenGLView.h"
#import "OOOpenGLMatrixManager.h"
#ifndef NDEBUG #ifndef NDEBUG
#import "Entity.h" #import "Entity.h"
@ -195,6 +196,7 @@
assert(_lod < kOOPlanetDataLevels); assert(_lod < kOOPlanetDataLevels);
const OOPlanetDataLevel *data = &kPlanetData[_lod]; const OOPlanetDataLevel *data = &kPlanetData[_lod];
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OO_ENTER_OPENGL(); OO_ENTER_OPENGL();
@ -217,8 +219,9 @@
[_material apply]; [_material apply];
// Scale the ball. // Scale the ball.
OOGL(glPushMatrix()); [matrixManager pushModelView];
GLMultOOMatrix(_transform); [matrixManager multModelView: _transform];
[matrixManager syncModelView];
OOGL(glEnable(GL_LIGHTING)); OOGL(glEnable(GL_LIGHTING));
OOGL(glEnable(GL_TEXTURE_2D)); OOGL(glEnable(GL_TEXTURE_2D));
@ -267,7 +270,8 @@
#endif #endif
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
#ifndef NDEBUG #ifndef NDEBUG
if (gDebugFlags & DEBUG_DRAW_NORMALS) [self debugDrawNormals]; if (gDebugFlags & DEBUG_DRAW_NORMALS) [self debugDrawNormals];
#endif #endif

View File

@ -30,6 +30,7 @@ MA 02110-1301, USA.
#import "OOSound.h" #import "OOSound.h"
#import "OOStringParsing.h" #import "OOStringParsing.h"
#import "OOMaths.h" #import "OOMaths.h"
#import "OOOpenGLMatrixManager.h"
static void InitTrumbleSounds(void); static void InitTrumbleSounds(void);
@ -383,11 +384,13 @@ static void PlayTrumbleSqueal(void);
OOGL(glShadeModel(GL_SMOOTH)); OOGL(glShadeModel(GL_SMOOTH));
OOGL(glEnable(GL_TEXTURE_2D)); OOGL(glEnable(GL_TEXTURE_2D));
[texture apply]; [texture apply];
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
OOGL(glPushMatrix()); [matrixManager pushModelView];
OOGL(glTranslatef( position.x, position.y, z)); [matrixManager translateModelView: make_vector(position.x, position.y, z)];
OOGL(glRotatef( rotation, 0.0, 0.0, 1.0)); [matrixManager multModelView: OOMatrixForRotationZ(rotation)];
[matrixManager syncModelView];
// //
// Body.. // Body..
@ -433,7 +436,8 @@ static void PlayTrumbleSqueal(void);
eyeTextureOffset = 0.5; break; eyeTextureOffset = 0.5; break;
} }
OOGL(glTranslatef( eye_position.x * wd, eye_position.y * ht, 0.0)); [matrixManager translateModelView: make_vector(eye_position.x * wd, eye_position.y * ht, 0.0)];
[matrixManager syncModelView];
OOGL(glColor4fv(colorEyes)); OOGL(glColor4fv(colorEyes));
OOGLBEGIN(GL_QUADS); OOGLBEGIN(GL_QUADS);
@ -467,7 +471,7 @@ static void PlayTrumbleSqueal(void);
mouthTextureOffset = 0.875; break; mouthTextureOffset = 0.875; break;
} }
OOGL(glTranslatef( mouth_position.x * wd, mouth_position.y * ht, 0.0)); [matrixManager translateModelView: make_vector(mouth_position.x * wd, mouth_position.y * ht, 0.0)];
OOGL(glColor4fv(colorBase)); OOGL(glColor4fv(colorBase));
OOGLBEGIN(GL_QUADS); OOGLBEGIN(GL_QUADS);
@ -485,7 +489,8 @@ static void PlayTrumbleSqueal(void);
OOGLEND(); OOGLEND();
// finally.. // finally..
OOGL(glPopMatrix()); [matrixManager popModelView];
[matrixManager syncModelView];
OOGL(glDisable(GL_TEXTURE_2D)); OOGL(glDisable(GL_TEXTURE_2D));
} }

View File

@ -4107,16 +4107,13 @@ static const OOMatrix starboard_matrix =
} }
OOGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); OOGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
OOGL(glLoadIdentity()); // reset matrix
OOGL(gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0));
[matrixManager resetModelView]; [matrixManager resetModelView];
[matrixManager lookAtWithEye: make_vector(1.0,0.0,1.0) center: make_vector(0.0,0.0,1.0) up: make_vector(0.0,1.0,0.0)]; [matrixManager lookAtWithEye: make_vector(0.0,0.0,0.0) center: make_vector(0.0,0.0,1.0) up: make_vector(0.0,1.0,0.0)];
OOLog(@"kja", @"Projection matrix: %@", OOMatrixDescription([matrixManager getModelView]));
// HACK BUSTED // HACK BUSTED
OOGL(glScalef(-1.0, 1.0, 1.0)); // flip left and right [matrixManager multModelView: OOMatrixForScale(-1.0,1.0,1.0)]; // flip left and right
OOGL(glPushMatrix()); // save this flat viewpoint [matrixManager pushModelView]; // save this flat viewpoint
/* OpenGL viewpoints: /* OpenGL viewpoints:
* *
@ -4148,14 +4145,16 @@ static const OOMatrix starboard_matrix =
view_matrix = OOMatrixMultiply(view_matrix, flipMatrix); view_matrix = OOMatrixMultiply(view_matrix, flipMatrix);
Vector viewOffset = [player viewpointOffset]; Vector viewOffset = [player viewpointOffset];
OOGL(gluLookAt(view_dir.x, view_dir.y, view_dir.z, 0.0, 0.0, 0.0, view_up.x, view_up.y, view_up.z)); [matrixManager lookAtWithEye: view_dir center: make_vector(0.0, 0.0, 0.0) up: view_up];
[matrixManager syncModelView];
if (EXPECT(!displayGUI || demoShipMode)) if (EXPECT(!displayGUI || demoShipMode))
{ {
if (EXPECT(!demoShipMode)) // we're in flight if (EXPECT(!demoShipMode)) // we're in flight
{ {
// rotate the view // rotate the view
OOGL(GLMultOOMatrix([player rotationMatrix])); [matrixManager multModelView: [player rotationMatrix]];
[matrixManager syncModelView];
// translate the view // translate the view
// HPVect: camera-relative position // HPVect: camera-relative position
// OOGL(GLTranslateOOVector(vector_flip(position))); // OOGL(GLTranslateOOVector(vector_flip(position)));
@ -4180,7 +4179,7 @@ static const OOMatrix starboard_matrix =
OOGL([self useGUILightSource:demoShipMode]); OOGL([self useGUILightSource:demoShipMode]);
// HACK: store view matrix for absolute drawing of active subentities (i.e., turrets). // HACK: store view matrix for absolute drawing of active subentities (i.e., turrets).
OOGL(viewMatrix = OOMatrixLoadGLMatrix(GL_MODELVIEW_MATRIX)); viewMatrix = [matrixManager getModelView];
int furthest = draw_count - 1; int furthest = draw_count - 1;
int nearest = 0; int nearest = 0;
@ -4215,22 +4214,27 @@ static const OOMatrix starboard_matrix =
OOGL(glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, flat_ambdiff)); OOGL(glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, flat_ambdiff));
OOGL(glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_no)); OOGL(glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_no));
OOGL(glPushMatrix()); [matrixManager pushModelView];
if (EXPECT(drawthing != player)) if (EXPECT(drawthing != player))
{ {
//translate the object //translate the object
// HPVect: camera relative // HPVect: camera relative
GLTranslateOOVector([drawthing cameraRelativePosition]); //GLTranslateOOVector([drawthing cameraRelativePosition]);
[matrixManager translateModelView: [drawthing cameraRelativePosition]];
//rotate the object //rotate the object
GLMultOOMatrix([drawthing drawRotationMatrix]); //GLMultOOMatrix([drawthing drawRotationMatrix]);
[matrixManager multModelView: [drawthing drawRotationMatrix]];
} }
else else
{ {
// Load transformation matrix // Load transformation matrix
GLLoadOOMatrix(view_matrix); //GLLoadOOMatrix(view_matrix);
[matrixManager loadModelView: view_matrix];
//translate the object from the viewpoint //translate the object from the viewpoint
GLTranslateOOVector(vector_flip(viewOffset)); //GLTranslateOOVector(vector_flip(viewOffset));
[matrixManager translateModelView: vector_flip(viewOffset)];
} }
[matrixManager syncModelView];
// atmospheric fog // atmospheric fog
fogging = (inAtmosphere && ![drawthing isStellarObject]); fogging = (inAtmosphere && ![drawthing isStellarObject]);
@ -4257,7 +4261,7 @@ static const OOMatrix starboard_matrix =
OOGL(glDisable(GL_FOG)); OOGL(glDisable(GL_FOG));
} }
OOGL(glPopMatrix()); [matrixManager popModelView];
} }
} }
@ -4277,21 +4281,21 @@ static const OOMatrix starboard_matrix =
if (!((d_status == STATUS_COCKPIT_DISPLAY) ^ demoShipMode)) // either in flight or in demo ship mode if (!((d_status == STATUS_COCKPIT_DISPLAY) ^ demoShipMode)) // either in flight or in demo ship mode
{ {
OOGL(glPushMatrix()); [matrixManager pushModelView];
if (EXPECT(drawthing != player)) if (EXPECT(drawthing != player))
{ {
//translate the object //translate the object
// HPVect: camera relative positions // HPVect: camera relative positions
GLTranslateOOVector([drawthing cameraRelativePosition]); [matrixManager translateModelView: [drawthing cameraRelativePosition]];
//rotate the object //rotate the object
GLMultOOMatrix([drawthing drawRotationMatrix]); [matrixManager multModelView: [drawthing drawRotationMatrix]];
} }
else else
{ {
// Load transformation matrix // Load transformation matrix
GLLoadOOMatrix(view_matrix); [matrixManager loadModelView: view_matrix];
//translate the object from the viewpoint //translate the object from the viewpoint
GLTranslateOOVector(vector_flip(viewOffset)); [matrixManager translateModelView: vector_flip(viewOffset)];
} }
// experimental - atmospheric fog // experimental - atmospheric fog
@ -4309,6 +4313,7 @@ static const OOMatrix starboard_matrix =
} }
// draw the thing // draw the thing
[matrixManager syncModelView];
[drawthing drawImmediate:false translucent:true]; [drawthing drawImmediate:false translucent:true];
// atmospheric fog // atmospheric fog
@ -4317,12 +4322,13 @@ static const OOMatrix starboard_matrix =
OOGL(glDisable(GL_FOG)); OOGL(glDisable(GL_FOG));
} }
OOGL(glPopMatrix()); [matrixManager popModelView];
} }
} }
} }
OOGL(glPopMatrix()); //restore saved flat viewpoint [matrixManager popModelView];
[matrixManager syncModelView];
if (EXPECT(!displayGUI || demoShipMode)) if (EXPECT(!displayGUI || demoShipMode))
{ {

View File

@ -37,6 +37,7 @@ MA 02110-1301, USA.
#import "OOGraphicsResetManager.h" #import "OOGraphicsResetManager.h"
#import "OOCollectionExtractors.h" // for splash screen settings #import "OOCollectionExtractors.h" // for splash screen settings
#import "OOFullScreenController.h" #import "OOFullScreenController.h"
#import "OOOpenGLMatrixManager.h"
#define kOOLogUnconvertedNSLog @"unclassified.MyOpenGLView" #define kOOLogUnconvertedNSLog @"unclassified.MyOpenGLView"
@ -610,6 +611,7 @@ MA 02110-1301, USA.
SDL_Rect dest; SDL_Rect dest;
NSString *imagesDir = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"]; NSString *imagesDir = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"];
OOOpenGLMatrixManager *matrixManager = [OOOpenGLMatrixManager sharedOpenGLMatrixManager];
image = SDL_LoadBMP([[imagesDir stringByAppendingPathComponent:@"splash.bmp"] UTF8String]); image = SDL_LoadBMP([[imagesDir stringByAppendingPathComponent:@"splash.bmp"] UTF8String]);
@ -656,15 +658,12 @@ MA 02110-1301, USA.
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION ); [matrixManager resetProjection];
glPushMatrix(); [matrixManager orthoLeft: 0.0f right: dest.w bottom: dest.h top: 0.0 near: -1.0 far: 1.0];
glLoadIdentity(); [matrixManager syncProjection];
glOrtho(0.0f, dest.w , dest.h, 0.0f, -1.0f, 1.0f); [matrixManager resetModelView];
[matrixManager syncModelView];
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
GLuint texture; GLuint texture;
GLenum texture_format; GLenum texture_format;
@ -718,7 +717,8 @@ MA 02110-1301, USA.
glEnd(); glEnd();
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
glLoadIdentity(); // reset matrix [matrixManager resetModelView];
[matrixManager syncModelView];
if ( image ) { if ( image ) {
SDL_FreeSurface( image ); SDL_FreeSurface( image );