diff --git a/src/Cocoa/MyOpenGLView.h b/src/Cocoa/MyOpenGLView.h index c355bdd12..8a0b35d41 100644 --- a/src/Cocoa/MyOpenGLView.h +++ b/src/Cocoa/MyOpenGLView.h @@ -30,7 +30,7 @@ MA 02110-1301, USA. #define MAX_CLEAR_DEPTH 10000000000.0 // 10 000 000 km. -#define INTERMEDIATE_CLEAR_DEPTH 100.0 +#define INTERMEDIATE_CLEAR_DEPTH 50000.0 // 100 m. #define MIN_FOV_DEG 30.0f diff --git a/src/Core/Entities/Entity.h b/src/Core/Entities/Entity.h index c2583612d..c505d9390 100644 --- a/src/Core/Entities/Entity.h +++ b/src/Core/Entities/Entity.h @@ -150,6 +150,7 @@ enum OOScanClass OOTimeAbsolute spawnTime; struct JSObject *_jsSelf; + NSUInteger lastDrawCounter; @private NSUInteger _sessionID; @@ -274,6 +275,9 @@ enum OOScanClass - (void) dumpState; // General "describe situtation verbosely in log" command. - (void) dumpSelfState; // Subclasses should override this, not -dumpState, and call throught to super first. +- (NSUInteger) lastDrawCounter; +- (void) setLastDrawCounter: (NSUInteger) drawCounter; + // Subclass repsonsibilities - (double) findCollisionRadius; - (void) drawImmediate:(bool)immediate translucent:(bool)translucent; diff --git a/src/Core/Entities/Entity.m b/src/Core/Entities/Entity.m index 75a625a83..b2a5800c0 100644 --- a/src/Core/Entities/Entity.m +++ b/src/Core/Entities/Entity.m @@ -94,6 +94,7 @@ - (id) init gTotalEntityMemory += [self oo_objectSize]; #endif + lastDrawCounter = 0; return self; } @@ -1047,6 +1048,19 @@ - (void)subEntityReallyDied:(ShipEntity *)sub } +- (NSUInteger) lastDrawCounter +{ + return lastDrawCounter; +} + + +- (void) setLastDrawCounter: (NSUInteger) drawCounter +{ + lastDrawCounter = drawCounter; + return; +} + + // For shader bindings. - (GLfloat)universalTime { diff --git a/src/Core/Universe.h b/src/Core/Universe.h index c027e453b..3c621f2e4 100644 --- a/src/Core/Universe.h +++ b/src/Core/Universe.h @@ -309,6 +309,7 @@ enum NSMutableSet *entitiesDeadThisUpdate; int framesDoneThisUpdate; + NSUInteger drawCounter; #if OOLITE_SPEECH_SYNTH #if OOLITE_MAC_OS_X diff --git a/src/Core/Universe.m b/src/Core/Universe.m index d65bc3e2e..33b2eac23 100644 --- a/src/Core/Universe.m +++ b/src/Core/Universe.m @@ -382,6 +382,7 @@ - (id) initWithGameView:(MyOpenGLView *)inGameView universeRegion = [[CollisionRegion alloc] initAsUniverse]; entitiesDeadThisUpdate = [[NSMutableSet alloc] init]; framesDoneThisUpdate = 0; + drawCounter = 0; [[GameController sharedController] logProgress:DESC(@"initializing-debug-support")]; OOInitDebugSupport(); @@ -4397,21 +4398,38 @@ - (void) drawUniverse } BOOL fogging, bpHide = [self breakPatternHide]; - + + drawCounter++; + float breakPlane = INTERMEDIATE_CLEAR_DEPTH; + for( int i = 0; i < draw_count; i++ ) + { + if ([my_entities[i] cameraRangeFront] > breakPlane) + { + continue; + } + if ([my_entities[i] cameraRangeBack] > breakPlane) + { + breakPlane = [my_entities[i] cameraRangeBack]; + } + } + + // We need to bring forward the near plane of the frustum on the long distance pass by this factor to avoid clipping objects at the corner of the window + float distanceFactor = sqrt(1 + ([gameView fov:YES]*[gameView fov:YES] * (1.0 + 1.0/(aspect*aspect)))); + for (vdist=0;vdist<=1;vdist++) { float nearPlane = vdist ? 1.0 : INTERMEDIATE_CLEAR_DEPTH; - float farPlane = vdist ? INTERMEDIATE_CLEAR_DEPTH : MAX_CLEAR_DEPTH; - float ratio = (displayGUI ? 0.5 : [gameView fov:YES]) * nearPlane; // 0.5 is field of view ratio for GUIs + float farPlane = vdist ? breakPlane : MAX_CLEAR_DEPTH; + float ratio = (displayGUI ? 0.5 : [gameView fov:YES]) * nearPlane / distanceFactor; // 0.5 is field of view ratio for GUIs OOGLResetProjection(); if ((displayGUI && 4*aspect >= 3) || (!displayGUI && 4*aspect <= 3)) { - OOGLFrustum(-ratio, ratio, -aspect*ratio, aspect*ratio, nearPlane, farPlane); + OOGLFrustum(-ratio, ratio, -aspect*ratio, aspect*ratio, nearPlane / distanceFactor, farPlane); } else { - OOGLFrustum(-3*ratio/aspect/4, 3*ratio/aspect/4, -3*ratio/4, 3*ratio/4, nearPlane, farPlane); + OOGLFrustum(-3*ratio/aspect/4, 3*ratio/aspect/4, -3*ratio/4, 3*ratio/4, nearPlane / distanceFactor, farPlane); } [self getActiveViewMatrix:&view_matrix forwardVector:&view_dir upVector:&view_up]; @@ -4471,7 +4489,7 @@ - (void) drawUniverse { [self setMainLightPosition:[cachedSun cameraRelativePosition]]; } - OOGL(glLightfv(GL_LIGHT1, GL_POSITION, main_light_position)); + OOGL(glLightfv(GL_LIGHT1, GL_POSITION, main_light_position)); } else { @@ -4494,13 +4512,13 @@ - (void) drawUniverse GLfloat fogFactor = 0.5 / airResistanceFactor; double fog_scale, half_scale; GLfloat flat_ambdiff[4] = {1.0, 1.0, 1.0, 1.0}; // for alpha - GLfloat mat_no[4] = {0.0, 0.0, 0.0, 1.0}; // nothing + GLfloat mat_no[4] = {0.0, 0.0, 0.0, 1.0}; // nothing GLfloat fog_blend; OOGL(glHint(GL_FOG_HINT, [self reducedDetail] ? GL_FASTEST : GL_NICEST)); - + [self defineFrustum]; // camera is set up for this frame - + OOVerifyOpenGLState(); OOCheckOpenGLErrors(@"Universe after setting up for opaque pass"); OOLog(@"universe.profile.draw", @"%@", @"Begin opaque pass"); @@ -4513,9 +4531,11 @@ - (void) drawUniverse OOEntityStatus d_status = [drawthing status]; if (bpHide && !drawthing->isImmuneToBreakPatternHide) continue; - if (vdist == 1 && [drawthing cameraRangeFront] > farPlane*1.5) continue; - if (vdist == 0 && [drawthing cameraRangeBack] < nearPlane) continue; -// if (vdist == 1 && [drawthing isPlanet]) continue; + if ([drawthing lastDrawCounter] == drawCounter) continue; + if (vdist == 0 && [drawthing cameraRangeFront] < nearPlane) + { + continue; + } if (!((d_status == STATUS_COCKPIT_DISPLAY) ^ demoShipMode)) // either demo ship mode or in flight { @@ -4561,6 +4581,7 @@ - (void) drawUniverse [self lightForEntity:demoShipMode || drawthing->isSunlit]; // draw the thing + [drawthing setLastDrawCounter: drawCounter]; [drawthing drawImmediate:false translucent:false]; OOGLPopModelView(); @@ -4611,6 +4632,7 @@ - (void) drawUniverse } // draw the thing + [drawthing setLastDrawCounter: drawCounter]; [drawthing drawImmediate:false translucent:true]; // atmospheric fog diff --git a/src/SDL/MyOpenGLView.h b/src/SDL/MyOpenGLView.h index 4ef32df77..352a48528 100644 --- a/src/SDL/MyOpenGLView.h +++ b/src/SDL/MyOpenGLView.h @@ -41,7 +41,7 @@ MA 02110-1301, USA. #define MAX_CLEAR_DEPTH 10000000000.0 // 10 000 000 km. -#define INTERMEDIATE_CLEAR_DEPTH 100.0 +#define INTERMEDIATE_CLEAR_DEPTH 50000.0 // 100 m.