Skip to content

Commit

Permalink
Move horizon distance calculation from WWMath to Globe.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Mar 27, 2022
1 parent c406f69 commit 834910e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
14 changes: 10 additions & 4 deletions src/WorldWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ define([
*/
this.verticalExaggeration = 1;

/**
* Distance from camera point to horizon.
* @type {Number}
*/
this.horizonDistance = 0;

/**
* Indicates that picking will return all objects at the pick point, if any. The top-most object will have
* its isOnTop flag set to true.
Expand Down Expand Up @@ -707,11 +713,10 @@ define([
this.camera.computeViewingTransform(modelview);

if (projection) {
var globeRadius = WWMath.max(this.globe.equatorialRadius, this.globe.polarRadius),
eyePos = this.camera.position,
var eyePos = this.camera.position,
fieldOfView = this.camera.fieldOfView,
eyeHorizon = WWMath.horizonDistanceForGlobeRadius(globeRadius, eyePos.altitude),
atmosphereHorizon = WWMath.horizonDistanceForGlobeRadius(globeRadius, 160000),
eyeHorizon = this.globe.horizonDistance(eyePos.altitude),
atmosphereHorizon = this.globe.horizonDistance(160000),
viewport = this.viewport;

// Set the far clip distance to the smallest value that does not clip the atmosphere.
Expand Down Expand Up @@ -805,6 +810,7 @@ define([
dc.globe = this.globe;
dc.navigator = this.navigator;
dc.camera = this.camera;
dc.horizonDistance = this.globe.horizonDistance(this.camera.position.altitude);
dc.layers = this.layers.slice();
dc.layers.push(dc.screenCreditController);
this.computeDrawContext();
Expand Down
3 changes: 1 addition & 2 deletions src/geom/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ define([
modelview.extractEyePoint(forwardRay.origin);
modelview.extractForwardVector(forwardRay.direction);

var globeRadius = WWMath.max(globe.equatorialRadius, globe.polarRadius);
var horizon = WWMath.horizonDistanceForGlobeRadius(globeRadius, this.position.altitude);
var horizon = globe.horizonDistance(this.position.altitude);
forwardRay.pointAt(horizon, originPoint);

globe.computePositionFromPoint(originPoint[0], originPoint[1], originPoint[2], originPos);
Expand Down
13 changes: 13 additions & 0 deletions src/globe/Globe.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,5 +677,18 @@ define([
return this.elevationModel.elevationsForGrid(sector, numLat, numLon, targetResolution, result);
};

/**
* Computes the distance to a globe's horizon from a viewer at a given altitude.
*
* Only the globe's ellipsoid is considered; terrain height is not incorporated.
* This returns zero if the altitude is less than or equal to zero.
*
* @param {Number} altitude The viewer's altitude above the globe, in meters.
* @returns {Number} The distance to the horizon, in model coordinates.
*/
Globe.prototype.horizonDistance = function (altitude) {
return (altitude > 0) ? Math.sqrt(altitude * (2 * this.equatorialRadius + altitude)) : 0;
};

return Globe;
});
20 changes: 0 additions & 20 deletions src/util/WWMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,26 +600,6 @@ define([
yAxisResult.normalize();
},

/**
* Computes the distance to a globe's horizon from a viewer at a given altitude.
*
* Only the globe's ellipsoid is considered; terrain height is not incorporated. This returns zero if the radius is zero
* or if the altitude is less than or equal to zero.
*
* @param {Number} radius The globe's radius, in meters.
* @param {Number} altitude The viewer's altitude above the globe, in meters.
* @returns {Number} The distance to the horizon, in model coordinates.
* @throws {ArgumentError} If the specified globe radius is negative.
*/
horizonDistanceForGlobeRadius: function (radius, altitude) {
if (radius < 0) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "WWMath",
"horizontalDistanceForGlobeRadius", "The specified globe radius is negative."));
}

return (radius > 0 && altitude > 0) ? Math.sqrt(altitude * (2 * radius + altitude)) : 0;
},

/**
* Computes the near clip distance that corresponds to a specified far clip distance and resolution at the far clip
* plane.
Expand Down

0 comments on commit 834910e

Please sign in to comment.