Work on replacing two different matrix types with different problems with one type that may have less problems. (I need the matrix to implement generalized cameras. I need the cameras to implement generalized lights. I need the lights to solve a problem with shaders. There might be an additional advantage or two to being able to place a camera anywhere rather than attached to the player, though.)

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@1385 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2008-02-17 00:50:01 +00:00
parent 890204c26d
commit b73d9d98ec
18 changed files with 590 additions and 278 deletions

View File

@ -477,6 +477,10 @@
1A9406850BAF66D6005F6CF3 /* OOVoxel.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A9406830BAF66D6005F6CF3 /* OOVoxel.m */; settings = {COMPILER_FLAGS = "-O3 -falign-loops=32 -falign-loops-max-skip=31 -falign-functions=32"; }; };
1A9406B40BAF67BF005F6CF3 /* OOTriangle.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A9406B20BAF67BF005F6CF3 /* OOTriangle.h */; };
1A9406B50BAF67BF005F6CF3 /* OOTriangle.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A9406B30BAF67BF005F6CF3 /* OOTriangle.m */; settings = {COMPILER_FLAGS = "-O3 -falign-loops=32 -falign-loops-max-skip=31 -falign-functions=32"; }; };
1A94D5AE0D65A6960072C805 /* OOLight.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A94D5AC0D65A6960072C805 /* OOLight.m */; };
1A94D5AF0D65A6960072C805 /* OOLight.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A94D5AD0D65A6960072C805 /* OOLight.h */; };
1A94D5B50D65A6B40072C805 /* OOCamera.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A94D5B30D65A6B40072C805 /* OOCamera.h */; };
1A94D5B60D65A6B40072C805 /* OOCamera.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A94D5B40D65A6B40072C805 /* OOCamera.m */; };
1A95338B0C02089E004EBB58 /* material-defaults.plist in Copy Config */ = {isa = PBXBuildFile; fileRef = 1A9533890C02089E004EBB58 /* material-defaults.plist */; };
1A95338C0C02089E004EBB58 /* planetinfo.plist in Copy Config */ = {isa = PBXBuildFile; fileRef = 1A95338A0C02089E004EBB58 /* planetinfo.plist */; };
1AA82C8A0CC10E700023B797 /* OOJSWorldScripts.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AA82C820CC10E3D0023B797 /* OOJSWorldScripts.m */; };
@ -1442,6 +1446,10 @@
1A9406B20BAF67BF005F6CF3 /* OOTriangle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOTriangle.h; sourceTree = "<group>"; };
1A9406B30BAF67BF005F6CF3 /* OOTriangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOTriangle.m; sourceTree = "<group>"; };
1A9407BF0BAF7032005F6CF3 /* GNUmakefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GNUmakefile; sourceTree = "<group>"; };
1A94D5AC0D65A6960072C805 /* OOLight.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOLight.m; sourceTree = "<group>"; };
1A94D5AD0D65A6960072C805 /* OOLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOLight.h; sourceTree = "<group>"; };
1A94D5B30D65A6B40072C805 /* OOCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOCamera.h; sourceTree = "<group>"; };
1A94D5B40D65A6B40072C805 /* OOCamera.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOCamera.m; sourceTree = "<group>"; };
1A9533890C02089E004EBB58 /* material-defaults.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "material-defaults.plist"; sourceTree = "<group>"; };
1A95338A0C02089E004EBB58 /* planetinfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = planetinfo.plist; sourceTree = "<group>"; };
1AA82C810CC10E3D0023B797 /* OOJSWorldScripts.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSWorldScripts.h; sourceTree = "<group>"; };
@ -1990,6 +1998,10 @@
1A2A1DEA0BD2A28E00152975 /* OOMacroOpenGL.h */,
1A43234A0BCFC9BB00F65914 /* OOOpenGLExtensionManager.h */,
1A43234B0BCFC9BB00F65914 /* OOOpenGLExtensionManager.m */,
1A94D5AD0D65A6960072C805 /* OOLight.h */,
1A94D5AC0D65A6960072C805 /* OOLight.m */,
1A94D5B30D65A6B40072C805 /* OOCamera.h */,
1A94D5B40D65A6B40072C805 /* OOCamera.m */,
25161100099544380037C2E1 /* OpenGLSprite.h */,
251610FF099544380037C2E1 /* OpenGLSprite.m */,
1AED2D0A0C04586C004A1118 /* OOGraphicsResetManager.h */,
@ -2757,6 +2769,8 @@
1A28AA160D55438200BC0CE4 /* OOJSSound.h in Headers */,
1AB784F90D554F7B00517983 /* OOJSSoundSource.h in Headers */,
1A60AFB70D56093B0070510D /* OOMusicController.h in Headers */,
1A94D5AF0D65A6960072C805 /* OOLight.h in Headers */,
1A94D5B50D65A6B40072C805 /* OOCamera.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -3114,6 +3128,8 @@
1A28AA170D55438200BC0CE4 /* OOJSSound.m in Sources */,
1AB784FA0D554F7B00517983 /* OOJSSoundSource.m in Sources */,
1A60AFB80D56093B0070510D /* OOMusicController.m in Sources */,
1A94D5AE0D65A6960072C805 /* OOLight.m in Sources */,
1A94D5B60D65A6B40072C805 /* OOCamera.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -33,7 +33,7 @@ this.description = "Cloaking device mission in galaxy 5.";
this.version = "1.71";
this.shipWillExitWitchspace = function()
this.shipWillExitWitchspace = function ()
{
// If we're in galaxy 5...
if (galaxyNumber == 4)

View File

@ -32,7 +32,7 @@ this.copyright = "© 2007 the Oolite team.";
this.version = "1.71";
this.shipWasScooped = function(scooper)
this.shipWasScooped = function (scooper)
{
if (scooper == player)
{

View File

@ -36,7 +36,7 @@ this.version = "1.71";
// launch_actions handled on didSpawn().
if (this.legacy_launchActions != undefined)
{
this.shipSpawned = function()
this.shipSpawned = function ()
{
/* IMPORTANT: runLegacyScriptActions() is a private function. It may
be removed, renamed or have its semantics changed at any time in
@ -54,7 +54,7 @@ if (this.legacy_launchActions != undefined)
// death_actions handled on didDie().
if (this.legacy_deathActions != undefined)
{
this.shipDied = function()
this.shipDied = function ()
{
/* IMPORTANT: runLegacyScriptActions() is a private function. It may
be removed, renamed or have its semantics changed at any time in
@ -72,7 +72,7 @@ if (this.legacy_scriptActions != undefined)
docks, and for cargo pods when they are is scooped. No sane vessel can
be scooped _and_ docked with. Non-sane vessels are certified insane.
*/
this.otherShipDocked = function(docker)
this.otherShipDocked = function (docker)
{
if (docker == player)
{
@ -83,7 +83,7 @@ if (this.legacy_scriptActions != undefined)
this.ship.runLegacyScriptActions(docker, this.legacy_scriptActions);
}
}
this.shipWasScooped = function(scooper)
this.shipWasScooped = function (scooper)
{
/* IMPORTANT: runLegacyScriptActions() is a private function. It may
be removed, renamed or have its semantics changed at any time in

View File

@ -32,7 +32,7 @@ this.description = "Random offers of trumbles.";
this.version = "1.71";
this.startUp = this.reset = function()
this.startUp = this.reset = function ()
{
/* For simplicity, ensure that missionVariables.trumbles is never
undefined when running the rest of the script. If it could be
@ -47,7 +47,7 @@ this.startUp = this.reset = function()
}
this.shipDockedWithStation = function()
this.shipDockedWithStation = function ()
{
/* In the pre-JavaScript implementation, the mission variable was set to
OFFER_MADE while the mission screen was shown. If the player lanched
@ -88,7 +88,7 @@ this.shipDockedWithStation = function()
}
this.missionScreenEnded = function()
this.missionScreenEnded = function ()
{
if (missionVariables.trumbles == "BUY_ME")
{
@ -112,7 +112,7 @@ this.missionScreenEnded = function()
}
this.shipWillExitWitchspace = function()
this.shipWillExitWitchspace = function ()
{
// If player has rejected a trumble offer, reset trumble mission with 2% probability per jump.
if (missionVariables.trumbles == "NOT_NOW" && Math.random < 0.02)

View File

@ -165,6 +165,7 @@ typedef struct
- (BOOL)isPlayer;
- (BOOL)isPlanet;
- (BOOL)isSun;
- (BOOL)isWormhole;
- (BOOL) validForAddToUniverse;
- (void) addToLinkedLists;

View File

@ -136,6 +136,12 @@ static NSString * const kOOLogEntityUpdateError = @"entity.linkedList.update.
}
- (BOOL)isWormhole
{
return isWormhole;
}
- (BOOL) validForAddToUniverse
{
return YES;

View File

@ -1404,7 +1404,7 @@ FAIL:
if ((particle_type == PARTICLE_FLASHER)&&(status != STATUS_INACTIVE))
{
gl_matrix temp_matrix;
OOMatrix temp_matrix;
Vector abspos = position; // in control of it's own orientation
int view_dir = [UNIVERSE viewDirection];
@ -1425,7 +1425,7 @@ FAIL:
{
if (translucent)
{
glGetFloatv(GL_MODELVIEW_MATRIX, temp_matrix);
temp_matrix = OOMatrixLoadGLMatrix(GL_MODELVIEW_MATRIX);
glPopMatrix(); glPushMatrix(); // restore zero!
glTranslatef(abspos.x, abspos.y, abspos.z); // move to absolute position
GLfloat xx = 0.5 * size.width;
@ -1457,12 +1457,12 @@ FAIL:
glEnd();
glLoadMatrixf(temp_matrix);
GLLoadOOMatrix(temp_matrix);
}
}
else
{
glGetFloatv(GL_MODELVIEW_MATRIX, temp_matrix);
temp_matrix = OOMatrixLoadGLMatrix(GL_MODELVIEW_MATRIX);
glPopMatrix(); // restore zero!
glPushMatrix();
// position and orientation is absolute
@ -1470,8 +1470,8 @@ FAIL:
glMultMatrixf([[PlayerEntity sharedPlayer] drawRotationMatrix]);
[self drawEntity:immediate:translucent];
glLoadMatrixf(temp_matrix);
GLLoadOOMatrix(temp_matrix);
}
}
}

View File

@ -1859,10 +1859,10 @@ static NSDictionary* instructions(int station_id, Vector coords, float speed, fl
{
OODebugDrawBasisAtOrigin(50.0f);
gl_matrix matrix;
quaternion_into_gl_matrix(port_orientation, matrix);
OOMatrix matrix;
matrix = OOMatrixForQuaternionRotation(port_orientation);
glPushMatrix();
glMultMatrixf(matrix);
GLMultOOMatrix(matrix);
halfDimensions = vector_multiply_scalar(port_dimensions, 0.5f);
adjustedPosition = port_position;

View File

@ -350,24 +350,24 @@ static BOOL hostiles;
double z_factor = siz.height / siz.width; // approx 1/4
double y_factor = 1.0 - sqrt(z_factor); // approx 1/2
int i;
int scanner_cx = x;
int scanner_cy = y;
double mass_lock_range2 = 25600.0*25600.0;
int i;
int scanner_cx = x;
int scanner_cy = y;
double mass_lock_range2 = 25600.0*25600.0;
int scanner_scale = SCANNER_MAX_RANGE * 2.5 / siz.width;
int scanner_scale = SCANNER_MAX_RANGE * 2.5 / siz.width;
double max_zoomed_range2 = SCANNER_SCALE*SCANNER_SCALE*10000.0/(scanner_zoom*scanner_zoom);
double max_zoomed_range2 = SCANNER_SCALE*SCANNER_SCALE*10000.0/(scanner_zoom*scanner_zoom);
GLfloat max_zoomed_range = sqrtf(max_zoomed_range2);
GLfloat max_zoomed_range = sqrtf(max_zoomed_range2);
BOOL isHostile = NO;
BOOL foundHostiles = NO;
BOOL mass_locked = NO;
BOOL isHostile = NO;
BOOL foundHostiles = NO;
BOOL mass_locked = NO;
Vector position, relativePosition;
Matrix rotMatrix;
int flash = ((int)([UNIVERSE getTime] * 4))&1;
Vector position, relativePosition;
OOMatrix rotMatrix;
int flash = ((int)([UNIVERSE getTime] * 4))&1;
Universe *uni = UNIVERSE;
PlayerEntity *player = [PlayerEntity sharedPlayer];
@ -387,16 +387,15 @@ static BOOL hostiles;
GLfloat col[4] = { 1.0, 1.0, 1.0, 1.0 }; // can be manipulated
position = player->position;
gl_matrix_into_matrix([player rotationMatrix], &rotMatrix);
position = [player position];
rotMatrix = OOMatrixFromGLMatrix([player rotationMatrix]);
glColor4fv(scanner_color);
drawScannerGrid(x, y, z1, siz, [UNIVERSE viewDirection], line_width, scanner_zoom);
GLfloat off_scope2 = (siz.width > siz.height) ? siz.width * siz.width : siz.height * siz.height;
//
int p_status = player->status;
OOEntityStatus p_status = [player status];
if ((p_status == STATUS_IN_FLIGHT)||(p_status == STATUS_AUTOPILOT_ENGAGED)||(p_status == STATUS_LAUNCHING)||(p_status == STATUS_WITCHSPACE_COUNTDOWN))
{
@ -485,7 +484,7 @@ static BOOL hostiles;
scale_vector(&relativePosition, max_zoomed_range / act_dist);
// rotate the view
mult_vector(&relativePosition, rotMatrix);
relativePosition = OOVectorMultiplyMatrix(relativePosition, rotMatrix);
// scale the view
scale_vector(&relativePosition, upscale);
@ -494,7 +493,7 @@ static BOOL hostiles;
y2 = y1 + y_factor * relativePosition.y;
isHostile = NO;
if (drawthing->isShip)
if ([drawthing isShip])
{
ShipEntity* ship = (ShipEntity *)drawthing;
double wr = [ship weaponRange];
@ -503,7 +502,7 @@ static BOOL hostiles;
col[0] = base_col[0]; col[1] = base_col[1]; col[2] = base_col[2]; col[3] = alpha * base_col[3];
}
if (drawthing->isWormhole)
if ([drawthing isWormhole])
{
col[0] = blue_color[0]; col[1] = (flash)? 1.0 : blue_color[1]; col[2] = blue_color[2]; col[3] = alpha * blue_color[3];
}
@ -531,13 +530,12 @@ static BOOL hostiles;
break;
}
if (drawthing->isShip)
if ([drawthing isShip])
{
ShipEntity* ship = (ShipEntity*)drawthing;
if (ship->collision_radius * upscale > 4.5)
{
Vector bounds[6];
// BoundingBox bb = [ship boundingBox];
BoundingBox bb = ship->totalBoundingBox;
bounds[0] = ship->v_forward; scale_vector(&bounds[0], bb.max.z);
bounds[1] = ship->v_forward; scale_vector(&bounds[1], bb.min.z);
@ -549,8 +547,7 @@ static BOOL hostiles;
int i;
for (i = 0; i < 6; i++)
{
bounds[i].x += rp.x; bounds[i].y += rp.y; bounds[i].z += rp.z;
mult_vector(&bounds[i], rotMatrix);
bounds[i] = OOVectorMultiplyMatrix(vector_add(bounds[i], rp), rotMatrix);
scale_vector(&bounds[i], upscale);
bounds[i] = make_vector(bounds[i].x + scanner_cx, bounds[i].z * z_factor + bounds[i].y * y_factor + scanner_cy, z1 );
}
@ -598,9 +595,9 @@ static BOOL hostiles;
}
}
}
//
[player setAlertFlag:ALERT_FLAG_HOSTILES to:foundHostiles];
//
if ((foundHostiles)&&(!hostiles))
{
hostiles = YES;
@ -611,7 +608,6 @@ static BOOL hostiles;
}
}
//
for (i = 0; i < ent_count; i++)
[my_entities[i] release]; // released
@ -677,12 +673,12 @@ static BOOL hostiles;
alpha = [info nonNegativeFloatForKey:ALPHA_KEY defaultValue:1.0];
// draw the compass
Matrix rotMatrix;
PlayerEntity *player = [PlayerEntity sharedPlayer];
Vector position = player->position;
gl_matrix_into_matrix([player rotationMatrix], &rotMatrix);
//
// new
OOMatrix rotMatrix;
PlayerEntity *player = [PlayerEntity sharedPlayer];
Vector position = [player position];
rotMatrix = OOMatrixFromGLMatrix([player rotationMatrix]);
GLfloat h1 = siz.height * 0.125;
GLfloat h3 = siz.height * 0.375;
GLfloat w1 = siz.width * 0.125;
@ -699,7 +695,6 @@ static BOOL hostiles;
glEnd();
glLineWidth(line_width); // thinner
//
PlanetEntity* the_sun = [UNIVERSE sun];
PlanetEntity* the_planet = [UNIVERSE planet];
StationEntity* the_station = [UNIVERSE station];
@ -752,15 +747,10 @@ static BOOL hostiles;
break;
}
// translate the view
relativePosition.x -= position.x; relativePosition.y -= position.y; relativePosition.z -= position.z;
// rotate the view
mult_vector(&relativePosition, rotMatrix);
if (relativePosition.x||relativePosition.y||relativePosition.z)
relativePosition = unit_vector(&relativePosition);
else
relativePosition.z = 1.0;
relativePosition = unit_vector(&relativePosition);
// translate and rotate the view
relativePosition = OOVectorMultiplyMatrix(vector_subtract(relativePosition, position), rotMatrix);
relativePosition = vector_normal_or_fallback(relativePosition, kBasisZVector);
relativePosition.x *= siz.width * 0.4;
relativePosition.y *= siz.height * 0.4;
relativePosition.x += x;
@ -1469,26 +1459,27 @@ static BOOL hostiles;
if ([player dialMissileStatus] == MISSILE_STATUS_TARGET_LOCKED)
{
GLfloat clear_color[4] = {0.0, 1.0, 0.0, 0.0};
Entity *target = [player primaryTarget];
GLfloat clear_color[4] = {0.0, 1.0, 0.0, 0.0};
Entity *target = [player primaryTarget];
if (!target)
return;
// draw the direction cue
Matrix rotMatrix;
Vector position = player->position;
gl_matrix_into_matrix([player rotationMatrix], &rotMatrix);
//
OOMatrix rotMatrix;
Vector position = [player position];
rotMatrix = OOMatrixFromGLMatrix([player rotationMatrix]);
if ([UNIVERSE viewDirection] != VIEW_GUI_DISPLAY)
{
GLfloat siz1 = CROSSHAIR_SIZE * (1.0 - ONE_EIGHTH);
GLfloat siz0 = CROSSHAIR_SIZE * ONE_EIGHTH;
GLfloat siz2 = CROSSHAIR_SIZE * (1.0 + ONE_EIGHTH);
Vector rpn = target->position;
// translate the view
rpn.x -= position.x; rpn.y -= position.y; rpn.z -= position.z;
// rotate the view
mult_vector(&rpn, rotMatrix);
// Transform the view
Vector rpn = vector_subtract([target position], position);
rpn = OOVectorMultiplyMatrix(rpn, rotMatrix);
switch ([UNIVERSE viewDirection])
{
case VIEW_AFT :
@ -1876,46 +1867,39 @@ void hudDrawStatusIconAt(int x, int y, int z, NSSize siz)
void hudDrawReticleOnTarget(Entity* target, PlayerEntity* player1, GLfloat z1)
{
ShipEntity* target_ship = (ShipEntity *)target;
NSString* legal_desc = nil;
ShipEntity *target_ship = (ShipEntity *)target;
NSString *legal_desc = nil;
if ((!target)||(!player1))
return;
if ([target_ship isCloaked]) return;
switch (target_ship->scanClass)
switch ([target_ship scanClass])
{
case CLASS_NEUTRAL :
{
int target_legal = [target_ship legalStatus];
int legal_i = 0;
if (target_legal > 0)
legal_i = (target_legal <= 50) ? 1 : 2;
legal_desc = [[[UNIVERSE descriptions] arrayForKey:@"legal_status"] stringAtIndex:legal_i];
}
break;
case CLASS_NEUTRAL:
{
int target_legal = [target_ship legalStatus];
int legal_i = 0;
if (target_legal > 0)
legal_i = (target_legal <= 50) ? 1 : 2;
legal_desc = [[[UNIVERSE descriptions] arrayForKey:@"legal_status"] stringAtIndex:legal_i];
}
break;
case CLASS_THARGOID :
legal_desc = @"Alien";
break;
case CLASS_THARGOID:
legal_desc = @"Alien";
break;
case CLASS_POLICE :
legal_desc = @"System Vessel";
break;
case CLASS_POLICE:
legal_desc = @"System Vessel";
break;
case CLASS_MILITARY :
legal_desc = @"Military Vessel";
break;
case CLASS_MILITARY:
legal_desc = @"Military Vessel";
break;
default :
case CLASS_BUOY :
case CLASS_CARGO :
case CLASS_ROCK :
case CLASS_MISSILE :
case CLASS_NO_DRAW :
case CLASS_STATION :
case CLASS_TARGET :
break;
default:
break;
}
if ([player1 guiScreen] != GUI_SCREEN_MAIN) // don't draw on text screens
@ -1924,29 +1908,27 @@ void hudDrawReticleOnTarget(Entity* target, PlayerEntity* player1, GLfloat z1)
if (!target)
return;
gl_matrix back_mat;
Quaternion back_q = player1->orientation;
OOMatrix back_mat;
Quaternion back_q = [player1 orientation];
back_q.w = -back_q.w; // invert
Vector v1 = vector_up_from_quaternion(back_q);
Vector p0 = [player1 viewpointPosition];
Vector p1 = target->position;
p1.x -= p0.x; p1.y -= p0.y; p1.z -= p0.z;
double rdist = sqrt(magnitude2(p1));
double rsize = target->collision_radius;
Vector v1 = vector_up_from_quaternion(back_q);
Vector p1;
p1 = vector_subtract([target position], [player1 viewpointPosition]);
double rdist = fast_magnitude(p1);
double rsize = [target collisionRadius];
if (rsize < rdist * ONE_SIXTYFOURTH)
rsize = rdist * ONE_SIXTYFOURTH;
GLfloat rs0 = rsize;
//double rs3 = rsize * 0.75;
GLfloat rs2 = rsize * 0.50;
//double rs1 = rsize * 0.25;
GLfloat rs0 = rsize;
GLfloat rs2 = rsize * 0.50;
glPushMatrix();
//
// deal with view directions
Vector view_dir, view_up;
view_up.x = 0.0; view_up.y = 1.0; view_up.z = 0.0;
Vector view_dir, view_up = kBasisYVector;
switch ([UNIVERSE viewDirection])
{
default:
@ -1973,14 +1955,14 @@ void hudDrawReticleOnTarget(Entity* target, PlayerEntity* player1, GLfloat z1)
}
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);
//
quaternion_into_gl_matrix(back_q, back_mat);
back_mat = OOMatrixForQuaternionRotation(back_q);
//
// rotate the view
glMultMatrixf([player1 rotationMatrix]);
// translate the view
glTranslatef(p1.x, p1.y, p1.z);
//rotate to face player1
glMultMatrixf(back_mat);
GLMultOOMatrix(back_mat);
// draw the reticle
glColor4fv(green_color);
glBegin(GL_LINES);

View File

@ -127,7 +127,7 @@ typedef uint16_t OOUniformConvertOptions;
* NSNumber.
* Vector.
* Quaternion.
* Matrix.
* OOMatrix.
* OOColor.
The "convert" flag has different meanings for different types:

View File

@ -72,7 +72,7 @@ SOFTWARE.
GLint constInt;
GLfloat constFloat;
GLfloat constVector[4];
gl_matrix constMatrix;
OOMatrix constMatrix;
struct
{
OOWeakReference *object;
@ -87,7 +87,7 @@ SOFTWARE.
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram vectorValue:(Vector)constValue;
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram colorValue:(OOColor *)constValue; // Converted to vector
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram quaternionValue:(Quaternion)constValue asMatrix:(BOOL)asMatrix; // Converted to vector (in xyzw order, not wxyz!) or rotation matrix.
// - (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram matrixValue:(Matrix)constValue;
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram matrixValue:(OOMatrix)constValue;
/* "Convert" has different meanings for different types.
For float and int types, it clamps to the range [0, 1].

View File

@ -64,7 +64,7 @@ typedef float (*FloatReturnMsgSend)(id, SEL);
typedef double (*DoubleReturnMsgSend)(id, SEL);
typedef Vector (*VectorReturnMsgSend)(id, SEL);
typedef Quaternion (*QuaternionReturnMsgSend)(id, SEL);
typedef Matrix (*MatrixReturnMsgSend)(id, SEL);
typedef OOMatrix (*MatrixReturnMsgSend)(id, SEL);
typedef NSPoint (*PointReturnMsgSend)(id, SEL);
@ -158,7 +158,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
if (asMatrix)
{
type = kOOShaderUniformTypeMatrix;
quaternion_into_gl_matrix(constValue, value.constMatrix);
value.constMatrix = OOMatrixForQuaternionRotation(constValue);
}
else
{
@ -174,13 +174,13 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
}
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram matrixValue:(Matrix)constValue
- (id)initWithName:(NSString *)uniformName shaderProgram:(OOShaderProgram *)shaderProgram matrixValue:(OOMatrix)constValue
{
self = [self initWithName:uniformName shaderProgram:shaderProgram];
if (self != nil)
{
type = kOOShaderUniformTypeMatrix;
matrix_into_gl_matrix(constValue, value.constMatrix);
value.constMatrix = constValue;
}
return self;
@ -458,7 +458,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
break;
case kOOShaderUniformTypeMatrix:
glUniformMatrix4fvARB(location, 1, NO, value.constMatrix);
glUniformMatrix4fvARB(location, 1, NO, OOMatrixValuesForOpenGL(value.constMatrix));
}
}
@ -471,7 +471,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
GLfloat fVal;
Vector vVal;
GLfloat expVVal[4];
gl_matrix mVal;
OOMatrix mVal;
Quaternion qVal;
NSPoint pVal = {0};
BOOL isInt = NO, isFloat = NO, isVector = NO, isMatrix = NO, isPoint = NO;
@ -538,7 +538,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
qVal = ((QuaternionReturnMsgSend)value.binding.method)(object, value.binding.selector);
if (convertToMatrix)
{
quaternion_into_gl_matrix(qVal, mVal);
mVal = OOMatrixForQuaternionRotation(qVal);
isMatrix = YES;
}
else
@ -552,7 +552,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
break;
case kOOShaderUniformTypeMatrix:
matrix_into_gl_matrix(((MatrixReturnMsgSend)value.binding.method)(object, value.binding.selector), mVal);
mVal = ((MatrixReturnMsgSend)value.binding.method)(object, value.binding.selector);
isMatrix = YES;
break;
@ -600,7 +600,7 @@ OOINLINE BOOL ValidBindingType(OOShaderUniformType type)
}
else if (isMatrix)
{
glUniformMatrix4fvARB(location, 1, NO, mVal);
glUniformMatrix4fvARB(location, 1, NO, OOMatrixValuesForOpenGL(mVal));
}
}

View File

@ -110,7 +110,7 @@ OOShaderUniformType OOShaderUniformTypeFromMethodSignature(NSMethodSignature *si
- (unsigned long)unsignedLongMethod;
- (Vector)vectorMethod;
- (Quaternion)quaternionMethod;
- (Matrix)matrixMethod;
- (OOMatrix)matrixMethod;
- (NSPoint)pointMethod;
- (id)idMethod;
@ -234,7 +234,7 @@ static const char *CopyTemplateForSelector(SEL selector)
}
- (Matrix)matrixMethod
- (OOMatrix)matrixMethod
{
return kZeroMatrix;
}

View File

@ -98,8 +98,8 @@ extern "C" {
#include "OOFastArithmetic.h"
#include "OOVector.h"
#include "OOMatrix.h"
#include "OOQuaternion.h"
#include "OOMatrix.h"
#include "OOVoxel.h"
#include "OOTriangle.h"
#include "OOBoundingBox.h"

View File

@ -29,38 +29,262 @@ MA 02110-1301, USA.
#error Do not include OOMatrix.h directly; include OOMaths.h.
#else
/* Deprecated legacy representations */
typedef GLfloat gl_matrix[16];
/* NOTE: this definition makes Matrix a pointer type. */
//typedef Vector Matrix[3];
typedef struct Matrix
typedef struct OOMatrix
{
Vector m[3];
} Matrix;
GLfloat m[4][4];
} OOMatrix;
extern const Matrix kIdentityMatrix; // {1, 0, 0}, {0, 1, 0}, {0, 0, 1}
extern const Matrix kZeroMatrix; // {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
extern const OOMatrix kIdentityMatrix; /* {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} */
extern const OOMatrix kZeroMatrix; /* {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} */
/* Matrix construction and standard primitive matrices */
OOINLINE OOMatrix OOMatrixConstruct(GLfloat aa, GLfloat ab, GLfloat ac, GLfloat ad,
GLfloat ba, GLfloat bb, GLfloat bc, GLfloat bd,
GLfloat ca, GLfloat cb, GLfloat cc, GLfloat cd,
GLfloat da, GLfloat db, GLfloat dc, GLfloat dd) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixFromOrientationAndPosition(Quaternion orientation, Vector position) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixFromBasisVectorsAndPosition(Vector i, Vector j, Vector k, Vector position) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixFromBasisVectors(Vector i, Vector j, Vector k) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixForRotationX(GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixForRotationY(GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixForRotationZ(GLfloat angle) INLINE_CONST_FUNC;
OOMatrix OOMatrixForRotation(Vector axis, GLfloat angle) CONST_FUNC;
OOMatrix OOMatrixForQuaternionRotation(Quaternion orientation);
OOINLINE OOMatrix OOMatrixForTranslation(Vector v) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixForTranslationComponents(GLfloat dx, GLfloat dy, GLfloat dz) INLINE_CONST_FUNC;
/* Matrix transformations */
OOINLINE OOMatrix OOMatrixTranslate(OOMatrix m, Vector offset) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixTranslateComponents(OOMatrix m, GLfloat dx, GLfloat dy, GLfloat dz) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixRotateX(OOMatrix m, GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixRotateY(OOMatrix m, GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixRotateZ(OOMatrix m, GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixRotate(OOMatrix m, Vector axis, GLfloat angle) INLINE_CONST_FUNC;
OOINLINE OOMatrix OOMatrixRotateQuaternion(OOMatrix m, Quaternion quat) INLINE_CONST_FUNC;
/* Matrix multiplication */
OOMatrix OOMatrixMultiply(OOMatrix a, OOMatrix b) CONST_FUNC;
Vector OOVectorMultiplyMatrix(Vector v, OOMatrix m) CONST_FUNC;
/* Orthogonalizion - avoidance of distortions due to numerical inaccuracy. */
OOMatrix OOMatrixOrthogonalize(OOMatrix m) CONST_FUNC;
/* OpenGL conveniences. Need to be macros to work with OOMacroOpenGL. */
#define OOMatrixValuesForOpenGL(M) (&(M).m[0][0])
#define GLMultOOMatrix(M) do { OOMatrix m_ = M; glMultMatrixf(OOMatrixValuesForOpenGL(m_)); } while (0)
#define GLLoadOOMatrix(M) do { OOMatrix m_ = M; glLoadMatrixf(OOMatrixValuesForOpenGL(m_)); } while (0)
#define GLMultTransposeOOMatrix(M) do { OOMatrix m_ = M; glMultTransposeMatrixf(OOMatrixValuesForOpenGL(m_)); } while (0)
#define GLLoadTransposeOOMatrix(M) do { OOMatrix m_ = M; glLoadTransposeMatrixf(OOMatrixValuesForOpenGL(m_)); } while (0)
OOINLINE OOMatrix OOMatrixLoadGLMatrix(unsigned long /* GLenum */ matrixID) ALWAYS_INLINE_FUNC;
/* Conversion to/from legacy representations */
OOINLINE OOMatrix OOMatrixFromGLMatrix(gl_matrix m) NONNULL_FUNC;
#ifdef __OBJC__
NSString *OOMatrixDescription(OOMatrix matrix); // @"{{#, #, #, #}, {#, #, #, #}, {#, #, #, #}, {#, #, #, #}}"
#endif
/*** Only inline definitions beyond this point ***/
OOINLINE OOMatrix OOMatrixConstruct(GLfloat aa, GLfloat ab, GLfloat ac, GLfloat ad,
GLfloat ba, GLfloat bb, GLfloat bc, GLfloat bd,
GLfloat ca, GLfloat cb, GLfloat cc, GLfloat cd,
GLfloat da, GLfloat db, GLfloat dc, GLfloat dd)
{
OOMatrix r =
{{
{ aa, ab, ac, ad },
{ ba, bb, bc, bd },
{ ca, cb, cc, cd },
{ da, db, dc, dd }
}};
return r;
}
OOINLINE OOMatrix OOMatrixFromOrientationAndPosition(Quaternion orientation, Vector position)
{
OOMatrix m = OOMatrixForQuaternionRotation(orientation);
return OOMatrixTranslate(m, position);
}
OOINLINE OOMatrix OOMatrixFromBasisVectorsAndPosition(Vector i, Vector j, Vector k, Vector p)
{
return OOMatrixConstruct
(
i.x, i.y, i.z, 0.0f,
j.x, j.y, j.z, 0.0f,
k.x, k.y, k.z, 0.0f,
p.x, p.y, p.z, 1.0f
);
}
OOINLINE OOMatrix OOMatrixFromBasisVectors(Vector i, Vector j, Vector k)
{
return OOMatrixFromBasisVectorsAndPosition(i, j, k, kZeroVector);
}
/* Standard primitive transformation matrices: */
OOMatrix OOMatrixForRotationX(GLfloat angle)
{
GLfloat s, c;
s = sinf(angle);
c = cosf(angle);
return OOMatrixConstruct
(
1, 0, 0, 0,
0, c, s, 0,
0, -s, c, 0,
0, 0, 0, 1
);
}
OOMatrix OOMatrixForRotationY(GLfloat angle)
{
GLfloat s, c;
s = sinf(angle);
c = cosf(angle);
return OOMatrixConstruct
(
c, 0, -s, 0,
0, 1, 0, 0,
s, 0, c, 0,
0, 0, 0, 1
);
}
OOMatrix OOMatrixForRotationZ(GLfloat angle)
{
GLfloat s, c;
s = sinf(angle);
c = cosf(angle);
return OOMatrixConstruct
(
c, s, 0, 0,
-s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
OOINLINE OOMatrix OOMatrixForTranslationComponents(GLfloat dx, GLfloat dy, GLfloat dz)
{
return OOMatrixConstruct
(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
dx, dy, dz, 1
);
}
OOINLINE OOMatrix OOMatrixForTranslation(Vector v)
{
return OOMatrixForTranslationComponents(v.x, v.y, v.z);
}
OOINLINE OOMatrix OOMatrixTranslateComponents(OOMatrix m, GLfloat dx, GLfloat dy, GLfloat dz)
{
m.m[3][0] += dx;
m.m[3][1] += dy;
m.m[3][2] += dz;
return m;
}
OOINLINE OOMatrix OOMatrixTranslate(OOMatrix m, Vector offset)
{
return OOMatrixTranslateComponents(m, offset.x, offset.y, offset.z);
}
OOINLINE OOMatrix OOMatrixRotateX(OOMatrix m, GLfloat angle)
{
return OOMatrixMultiply(m, OOMatrixForRotationX(angle));
}
OOINLINE OOMatrix OOMatrixRotateY(OOMatrix m, GLfloat angle)
{
return OOMatrixMultiply(m, OOMatrixForRotationY(angle));
}
OOINLINE OOMatrix OOMatrixRotateZ(OOMatrix m, GLfloat angle)
{
return OOMatrixMultiply(m, OOMatrixForRotationZ(angle));
}
OOINLINE OOMatrix OOMatrixRotate(OOMatrix m, Vector axis, GLfloat angle)
{
return OOMatrixMultiply(m, OOMatrixForRotation(axis, angle));
}
OOINLINE OOMatrix OOMatrixRotateQuaternion(OOMatrix m, Quaternion quat)
{
return OOMatrixMultiply(m, OOMatrixForQuaternionRotation(quat));
}
OOINLINE OOMatrix OOMatrixFromGLMatrix(gl_matrix m)
{
assert(m != NULL);
OOMatrix r;
memcpy(&r.m[0][0], m, sizeof (GLfloat) * 16);
return r;
}
OOINLINE OOMatrix OOMatrixLoadGLMatrix(unsigned long /* GLenum */ matrixID)
{
OOMatrix m;
glGetFloatv(matrixID, OOMatrixValuesForOpenGL(m));
return m;
}
/***** Deprecated legacy stuff beyond this point, do not use *****/
/* Set matrix to identity matrix */
OOINLINE void set_matrix_identity(Matrix *outMatrix) ALWAYS_INLINE_FUNC NONNULL_FUNC DEPRECATED_FUNC;
/* Copy one matrix to another */
OOINLINE void matrix_copy(Matrix *outMatrix, const Matrix value) ALWAYS_INLINE_FUNC NONNULL_FUNC DEPRECATED_FUNC;
OOINLINE void OOCopyGLMatrix(gl_matrix dst, const gl_matrix src) ALWAYS_INLINE_FUNC NONNULL_FUNC;
/* Mutiply two matrices, storing the result in a. */
void mult_matrix(Matrix *outA, const Matrix b) NONNULL_FUNC;
/* Muliply a vector by a matrix, storing the result in v. */
void mult_vector(Vector *outV, const Matrix m) NONNULL_FUNC;
/* Convert between Matrix and OpenGL matrix */
void matrix_into_gl_matrix(const Matrix m, gl_matrix outGLMatrix) NONNULL_FUNC;
void gl_matrix_into_matrix(const gl_matrix glmat, Matrix *outMatrix) NONNULL_FUNC;
/* Multiply vector by OpenGL matrix */
void mult_vector_gl_matrix(Vector *outVector, const gl_matrix glmat) NONNULL_FUNC;
@ -71,22 +295,10 @@ void vectors_into_gl_matrix(Vector forward, Vector right, Vector up, gl_matrix o
/*** Only inline definitions beyond this point ***/
OOINLINE void matrix_copy(Matrix *matrix, const Matrix value)
{
*matrix = value;
}
OOINLINE void OOCopyGLMatrix(gl_matrix dst, const gl_matrix src)
{
memcpy(dst, src, sizeof dst);
}
OOINLINE void set_matrix_identity(Matrix *matrix)
{
*matrix = kIdentityMatrix;
}
#endif /* INCLUDED_OOMATHS_h */

View File

@ -26,86 +26,156 @@ MA 02110-1301, USA.
#import "OOMaths.h"
const Matrix kIdentityMatrix =
{{
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f }
}};
const Matrix kZeroMatrix =
{{
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f }
}};
const OOMatrix kIdentityMatrix =
{ m: {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}
}};
const OOMatrix kZeroMatrix = { m: {
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
}};
void mult_matrix(Matrix *a, const Matrix b)
OOMatrix OOMatrixForRotation(Vector axis, GLfloat angle)
{
int i;
Matrix rv;
axis = vector_normal(axis);
GLfloat x = axis.x, y = axis.y, z = axis.z;
GLfloat s = sinf(angle), c = cosf(angle);
GLfloat t = 1.0f - c;
// Lots of opportunity for common subexpression elimintation here, but I'll leave it to the compiler for now.
return OOMatrixConstruct
(
t * x * x + c, t * x * y + s * z, t * x * z - s * y, 0.0f,
t * x * y - s * z, t * y * y + c, t * y * z + s * x, 0.0f,
t * x * y + s * y, t * y * z - s * x, t * z * z + c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
for (i = 0; i < 3; i++)
OOMatrix OOMatrixForQuaternionRotation(Quaternion orientation)
{
GLfloat w, wz, wy, wx;
GLfloat x, xz, xy, xx;
GLfloat y, yz, yy;
GLfloat z, zz;
Quaternion q = orientation;
quaternion_normalize(&q);
w = q.w;
z = q.z;
y = q.y;
x = q.x;
xx = 2.0f * x; yy = 2.0f * y; zz = 2.0f * z;
wx = w * xx; wy = w * yy; wz = w * zz;
xx = x * xx; xy = x * yy; xz = x * zz;
yy = y * yy; yz = y * zz;
zz = z * zz;
#if 0
OOMatrix r;
r.vals[ 0] = 1.0f - yy - zz; r.vals[ 4] = xy + wz; r.vals[ 8] = xz - wy; r.vals[12] = 0.0f;
r.vals[ 1] = xy - wz; r.vals[ 5] = 1.0f - xx - zz; r.vals[ 9] = yz + wx; r.vals[13] = 0.0f;
r.vals[ 2] = xz + wy; r.vals[ 6] = yz - wx; r.vals[10] = 1.0f - xx - yy; r.vals[14] = 0.0f;
r.vals[ 3] = 0.0f; r.vals[ 7] = 0.0f; r.vals[11] = 0.0f; r.vals[15] = 1.0f;
return r;
#else
return OOMatrixConstruct
(
1.0f - yy - zz, xy - wz, xz + wy, 0.0f,
xy + wz, 1.0f - xx - zz, yz - wx, 0.0f,
xz - wy, yz + wx, 1.0f - xx - yy, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
#endif
}
OOMatrix OOMatrixMultiply(OOMatrix a, OOMatrix b)
{
unsigned i = 0;
OOMatrix r;
// This is amenable to significant optimization with Altivec, and presumably also SSE.
for (i = 0; i != 4; ++i)
{
rv.m[i].x = (a->m[0].x * b.m[i].x) +
(a->m[1].x * b.m[i].y) +
(a->m[2].x * b.m[i].z);
rv.m[i].y = (a->m[0].y * b.m[i].x) +
(a->m[1].y * b.m[i].y) +
(a->m[2].y * b.m[i].z);
rv.m[i].z = (a->m[0].z * b.m[i].x) +
(a->m[1].z * b.m[i].y) +
(a->m[2].z * b.m[i].z);
r.m[i][0] = a.m[i][0] * b.m[0][0] + a.m[i][1] * b.m[1][0] + a.m[i][2] * b.m[2][0] + a.m[i][3] * b.m[3][0];
r.m[i][1] = a.m[i][0] * b.m[0][1] + a.m[i][1] * b.m[1][1] + a.m[i][2] * b.m[2][1] + a.m[i][3] * b.m[3][1];
r.m[i][2] = a.m[i][0] * b.m[0][2] + a.m[i][1] * b.m[1][2] + a.m[i][2] * b.m[2][2] + a.m[i][3] * b.m[3][2];
r.m[i][3] = a.m[i][0] * b.m[0][3] + a.m[i][1] * b.m[1][3] + a.m[i][2] * b.m[2][3] + a.m[i][3] * b.m[3][3];
}
*a = rv;
return r;
}
void mult_vector(Vector *v, const Matrix m)
Vector OOVectorMultiplyMatrix(Vector v, OOMatrix m)
{
GLfloat x;
GLfloat y;
GLfloat z;
x = (v->x * m.m[0].x) +
(v->y * m.m[0].y) +
(v->z * m.m[0].z);
y = (v->x * m.m[1].x) +
(v->y * m.m[1].y) +
(v->z * m.m[1].z);
z = (v->x * m.m[2].x) +
(v->y * m.m[2].y) +
(v->z * m.m[2].z);
v->x = x;
v->y = y;
v->z = z;
GLfloat x, y, z, w;
#if 0
x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3];
#else
x = m.m[0][0] * v.x + m.m[1][0] * v.y + m.m[2][0] * v.z + m.m[3][0];
y = m.m[0][1] * v.x + m.m[1][1] * v.y + m.m[2][1] * v.z + m.m[3][1];
z = m.m[0][2] * v.x + m.m[1][2] * v.y + m.m[2][2] * v.z + m.m[3][2];
w = m.m[0][3] * v.x + m.m[1][3] * v.y + m.m[2][3] * v.z + m.m[3][3];
#endif
w = 1.0f/w;
return make_vector(x * w, y * w, z * w);
}
void matrix_into_gl_matrix(const Matrix mat, gl_matrix glmat)
OOMatrix OOMatrixOrthogonalize(OOMatrix m)
{
glmat[0] = mat.m[0].x; glmat[4] = mat.m[0].y; glmat[8] = mat.m[0].z; glmat[3] = 0.0f;
glmat[1] = mat.m[1].x; glmat[5] = mat.m[1].y; glmat[9] = mat.m[1].z; glmat[7] = 0.0f;
glmat[2] = mat.m[2].x; glmat[6] = mat.m[2].y; glmat[10] = mat.m[2].z; glmat[11] = 0.0f;
glmat[12] = 0.0f; glmat[13] = 0.0f; glmat[14] = 0.0f; glmat[15] = 1.0f;
// Simple orthogonalization: make everything orthogonal to everything else.
Vector i = { m.m[0][0], m.m[1][0], m.m[2][0] };
Vector j = { m.m[0][1], m.m[1][1], m.m[2][1] };
Vector k = { m.m[0][2], m.m[1][2], m.m[2][2] };
k = vector_normal(k);
i = vector_normal(cross_product(j, k));
j = cross_product(k, i);
m.m[0][0] = i.x; m.m[1][0] = i.y; m.m[2][0] = i.z;
m.m[0][1] = j.x; m.m[1][1] = j.y; m.m[2][1] = j.z;
m.m[0][2] = k.x; m.m[1][2] = k.y; m.m[2][2] = k.z;
return m;
}
void gl_matrix_into_matrix(const gl_matrix glmat, Matrix *mat)
NSString *OOMatrixDescription(OOMatrix matrix)
{
mat->m[0].x = glmat[0]; mat->m[0].y = glmat[4]; mat->m[0].z = glmat[8];
mat->m[1].x = glmat[1]; mat->m[1].y = glmat[5]; mat->m[1].z = glmat[9];
mat->m[2].x = glmat[2]; mat->m[2].y = glmat[6]; mat->m[2].z = glmat[10];
return [NSString stringWithFormat:@"{{%g, %g, %g, %g}, {%g, %g, %g, %g}, {%g, %g, %g, %g}, {%g, %g, %g, %g}}",
matrix.m[0][0], matrix.m[0][1], matrix.m[0][2], matrix.m[0][3],
matrix.m[1][0], matrix.m[1][1], matrix.m[1][2], matrix.m[1][3],
matrix.m[2][0], matrix.m[2][1], matrix.m[2][2], matrix.m[2][3],
matrix.m[3][0], matrix.m[3][1], matrix.m[3][2], matrix.m[3][3]];
}
/***** Deprecated legacy stuff beyond this point, do not use *****/
// Multiply vector by gl_matrix.
void mult_vector_gl_matrix (Vector *vec, const gl_matrix glmat)
{

View File

@ -3306,11 +3306,64 @@ void setDemoLight(BOOL yesno, Vector position)
// global rotation matrix definitions
GLfloat fwd_matrix[] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
GLfloat aft_matrix[] = { -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
GLfloat port_matrix[] = { 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
GLfloat starboard_matrix[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
GLfloat* custom_matrix;
static const OOMatrix fwd_matrix =
{{
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
}};
static const OOMatrix aft_matrix =
{{
{-1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, -1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
}};
static const OOMatrix port_matrix =
{{
{ 0.0f, 0.0f, -1.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
}};
static const OOMatrix starboard_matrix =
{{
{ 0.0f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{-1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f }
}};
- (OOMatrix) activeViewMatrix
{
switch (viewDirection)
{
case VIEW_FORWARD:
return fwd_matrix;
case VIEW_AFT:
return aft_matrix;
case VIEW_PORT:
return port_matrix;
case VIEW_STARBOARD:
return starboard_matrix;
case VIEW_CUSTOM:
return OOMatrixFromGLMatrix([[PlayerEntity sharedPlayer] customViewMatrix]);
case VIEW_NONE:
case VIEW_GUI_DISPLAY:
case VIEW_BREAK_PATTERN:
break;
}
return fwd_matrix;
}
- (void) drawFromEntity:(OOUniversalID) n
{
@ -3356,43 +3409,18 @@ GLfloat* custom_matrix;
viewthing = [entities objectAtIndex:n];
}
if ((viewthing)&&(viewthing->isPlayer))
if (![viewthing isPlayer])
{
inGUIMode = [(PlayerEntity*)viewthing showDemoShips];
custom_matrix = [(PlayerEntity*)viewthing customViewMatrix];
/* -- */
}
else
{
OOLog(kOOLogInconsistentState, @"***** Universe trying to draw from the view of an entity NOT the player");
// throw an exception here...
OOLog(kOOLogInconsistentState, @"***** Universe trying to draw from the view of an entity NOT the player: %@", viewthing);
[NSException raise:@"OoliteException"
format:@"Universe cannot draw from a non-player entity."];
}
inGUIMode = [(PlayerEntity*)viewthing showDemoShips];
position = [viewthing viewpointPosition];
v_status = viewthing->status;
GLfloat* view_matrix = fwd_matrix;
switch (viewDirection)
{
case VIEW_FORWARD:
view_matrix = fwd_matrix; break;
case VIEW_AFT:
view_matrix = aft_matrix; break;
case VIEW_PORT:
view_matrix = port_matrix; break;
case VIEW_STARBOARD:
view_matrix = starboard_matrix; break;
/* GILES custom view points */
case VIEW_CUSTOM:
view_matrix = custom_matrix; break;
/* -- */
case VIEW_NONE:
case VIEW_GUI_DISPLAY:
case VIEW_BREAK_PATTERN:
break;
}
OOMatrix view_matrix = [self activeViewMatrix];
CheckOpenGLErrors(@"Universe before doing anything");
@ -3413,11 +3441,14 @@ GLfloat* custom_matrix;
// HACK BUSTED
glScalef( -1.0, 1.0, 1.0); // flip left and right
gl_matrix saved_flat_matrix;
glGetFloatv(GL_MODELVIEW_MATRIX, saved_flat_matrix);
glPushMatrix(); // save this flat viewpoint
// Set up view transformation matrix
OOMatrix flipMatrix = kIdentityMatrix;
flipMatrix.m[2][2] = -1;
view_matrix = OOMatrixMultiply(view_matrix, flipMatrix);
Vector viewOffset = [viewthing viewpointOffset];
view_up.x = 0.0; view_up.y = 1.0; view_up.z = 0.0;
switch (viewDirection)
{
@ -3520,14 +3551,10 @@ GLfloat* custom_matrix;
}
else
{
Vector viewOffset = [viewthing viewpointOffset];
// get saved viewpoint
glLoadMatrixf(saved_flat_matrix);
// rotate according to view direction
glMultMatrixf(view_matrix);
// Load transformation matrix
GLLoadOOMatrix(view_matrix);
//translate the object from the viewpoint
glTranslatef(-viewOffset.x, -viewOffset.y, -viewOffset.z);
}
// atmospheric fog
@ -3598,12 +3625,10 @@ GLfloat* custom_matrix;
}
else
{
Vector viewOffset = [viewthing viewpointOffset];
// get saved viewpoint
glLoadMatrixf(saved_flat_matrix);
// rotate according to view direction
glMultMatrixf(view_matrix);
// Load transformation matrix
GLLoadOOMatrix(view_matrix);
//translate the object from the viewpoint
glTranslatef(-viewOffset.x, -viewOffset.y, -viewOffset.z);
}