Skip to content

Commit

Permalink
Simplify camera to look at transformation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Aug 26, 2022
1 parent 56c4e8d commit b1fe177
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ public void onNavigatorEvent(WorldWindow wwd, NavigatorEvent event) {
// Update the status overlay views whenever the navigator stops moving,
// and also it is moving but at an (arbitrary) maximum refresh rate of 20 Hz.
if (eventAction == WorldWind.NAVIGATOR_STOPPED || elapsedTime > 50) {
// Pick terrain located behind the viewport center point
PickedObject terrainPickedObject = wwd.pick(wwd.getViewport().width / 2f, wwd.getViewport().height / 2f).terrainPickedObject();
Position terrainPosition = terrainPickedObject != null ? terrainPickedObject.getTerrainPosition() : null;

// Get the current camera state to apply to the overlays
event.getCamera().getAsLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), terrainPosition, lookAt);
wwd.cameraAsLookAt(lookAt);

// Update the overlays
updateOverlayContents(lookAt, event.getCamera());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {

// And finally, for this demo, position the viewer to look at the sightline position
LookAt lookAt = new LookAt().set(pos.latitude, pos.longitude, pos.altitude, WorldWind.ABSOLUTE, 2e4 /*range*/, 0 /*heading*/, 45 /*tilt*/, 0 /*roll*/);
this.getWorldWindow().getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), lookAt);
this.getWorldWindow().cameraFromLookAt(lookAt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
Position pos = new Position(32.4520, 63.44553, 0);
LookAt lookAt = new LookAt().set(pos.latitude, pos.longitude, pos.altitude, WorldWind.ABSOLUTE,
1e5 /*range*/, 0 /*heading*/, 45 /*tilt*/, 0 /*roll*/);
this.getWorldWindow().getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), lookAt);
this.getWorldWindow().cameraFromLookAt(lookAt);

// The MIL-STD-2525 rendering library takes time initialize, we'll perform this task via the
// AsyncTask's background thread and then load the symbols in its post execute handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected void onCreate(Bundle savedInstanceState) {

// And finally, for this demo, position the viewer to look at the placemarks
LookAt lookAt = new LookAt().set(34.150, -119.150, 0, WorldWind.ABSOLUTE, 2e4 /*range*/, 0 /*heading*/, 45 /*tilt*/, 0 /*roll*/);
this.getWorldWindow().getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), lookAt);
this.getWorldWindow().cameraFromLookAt(lookAt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h3>LookAtViewFragment.java</h3>
// Apply the new view
LookAt lookAt = new LookAt();
lookAt.set(airport.latitude, airport.longitude, airport.altitude, WorldWind.ABSOLUTE, range, heading, tilt, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ <h3>NavigatorEventFragment.java</h3>
// Update the status overlay views whenever the navigator stops moving,
// and also it is moving but at an (arbitrary) maximum refresh rate of 20 Hz.
if (eventAction == WorldWind.NAVIGATOR_STOPPED || elapsedTime > 50) {
// Pick terrain located behind the viewport center point
PickedObject terrainPickedObject = wwd.pick(wwd.getViewport().width / 2f, wwd.getViewport().height / 2f).terrainPickedObject();
Position terrainPosition = terrainPickedObject != null ? terrainPickedObject.getTerrainPosition() : null;

// Get the current camera state to apply to the overlays
event.getCamera().getAsLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), terrainPosition, lookAt);
wwd.cameraAsLookAt(lookAt);

// Update the overlays
updateOverlayContents(lookAt, event.getCamera());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h3>PlacemarksPickingFragment.java</h3>

// Position the viewer to look near the airports
LookAt lookAt = new LookAt().set(34.15, -119.15, 0, WorldWind.ABSOLUTE, 2e4 /*range*/, 0 /*heading*/, 45 /*tilt*/, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3>PlacemarksFragment.java</h3>
Position pos = airport.getPosition();
LookAt lookAt = new LookAt().set(pos.latitude, pos.longitude, pos.altitude, WorldWind.ABSOLUTE,
1e5 /*range*/, 0 /*heading*/, 80 /*tilt*/, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public WorldWindow createWorldWindow() {
// Apply the new view
LookAt lookAt = new LookAt();
lookAt.set(airport.latitude, airport.longitude, airport.altitude, WorldWind.ABSOLUTE, range, heading, tilt, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ public void onNavigatorEvent(WorldWindow wwd, NavigatorEvent event) {
// Update the status overlay views whenever the navigator stops moving,
// and also it is moving but at an (arbitrary) maximum refresh rate of 20 Hz.
if (eventAction == WorldWind.NAVIGATOR_STOPPED || elapsedTime > 50) {
// Pick terrain located behind the viewport center point
PickedObject terrainPickedObject = wwd.pick(wwd.getViewport().width / 2f, wwd.getViewport().height / 2f).terrainPickedObject();
Position terrainPosition = terrainPickedObject != null ? terrainPickedObject.getTerrainPosition() : null;

// Get the current camera state to apply to the overlays
event.getCamera().getAsLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), terrainPosition, lookAt);
wwd.cameraAsLookAt(lookAt);

// Update the overlays
updateOverlayContents(lookAt, event.getCamera());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ protected void createPlacemark(Position position, RenderableLayer layer) {

protected void positionView(WorldWindow wwd) {
LookAt lookAt = new LookAt().set(46.230, -122.190, 500, WorldWind.ABSOLUTE, 1.5e4 /*range*/, 45.0 /*heading*/, 70.0 /*tilt*/, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public WorldWindow createWorldWindow() {
Position pos = airport.getPosition();
LookAt lookAt = new LookAt().set(pos.latitude, pos.longitude, pos.altitude, WorldWind.ABSOLUTE,
1e5 /*range*/, 0 /*heading*/, 80 /*tilt*/, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WorldWindow createWorldWindow() {

// Position the viewer to look near the airports
LookAt lookAt = new LookAt().set(34.15, -119.15, 0, WorldWind.ABSOLUTE, 2e4 /*range*/, 0 /*heading*/, 45 /*tilt*/, 0 /*roll*/);
wwd.getCamera().setFromLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), lookAt);
wwd.cameraFromLookAt(lookAt);

return wwd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void resetOrientation(boolean headingOnly) {
this.lookAt.tilt = 0;
this.lookAt.roll = 0;
}
this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
this.gestureDidEnd();
}
Expand All @@ -89,7 +89,7 @@ public void zoomIn() {
this.gestureDidBegin();
this.lookAt.range /= ZOOM_FACTOR;
this.applyLimits(lookAt);
this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
this.gestureDidEnd();
}
Expand All @@ -98,7 +98,7 @@ public void zoomOut() {
this.gestureDidBegin();
this.lookAt.range *= ZOOM_FACTOR;
this.applyLimits(lookAt);
this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
this.gestureDidEnd();
}
Expand Down Expand Up @@ -187,7 +187,7 @@ protected void handlePan(GestureRecognizer recognizer) {
this.lookAt.position.longitude = lon;
}

this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
} else if (state == WorldWind.ENDED || state == WorldWind.CANCELLED) {
this.gestureDidEnd();
Expand All @@ -206,7 +206,7 @@ protected void handlePinch(GestureRecognizer recognizer) {
this.lookAt.range = this.beginLookAt.range / scale;
this.applyLimits(this.lookAt);

this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
}
} else if (state == WorldWind.ENDED || state == WorldWind.CANCELLED) {
Expand All @@ -227,7 +227,7 @@ protected void handleRotate(GestureRecognizer recognizer) {
this.lookAt.heading = WWMath.normalizeAngle360(this.lookAt.heading + headingDegrees);
this.lastRotation = rotation;

this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
} else if (state == WorldWind.ENDED || state == WorldWind.CANCELLED) {
this.gestureDidEnd();
Expand All @@ -251,7 +251,7 @@ protected void handleTilt(GestureRecognizer recognizer) {
this.lookAt.tilt = this.beginLookAt.tilt + tiltDegrees;
this.applyLimits(this.lookAt);

this.wwd.getCamera().setFromLookAt(this.wwd.getGlobe(), this.wwd.getVerticalExaggeration(), this.lookAt);
this.wwd.cameraFromLookAt(this.lookAt);
this.wwd.requestRedraw();
} else if (state == WorldWind.ENDED || state == WorldWind.CANCELLED) {
this.gestureDidEnd();
Expand All @@ -275,9 +275,7 @@ protected void applyLimits(LookAt lookAt) {
protected void gestureDidBegin() {
if (this.activeGestures++ == 0) {
// Pick terrain located behind the viewport center point
PickedObject terrainPickedObject = wwd.pick(wwd.getViewport().width / 2f, wwd.getViewport().height / 2f).terrainPickedObject();
Position terrainPosition = terrainPickedObject != null ? terrainPickedObject.getTerrainPosition() : null;
this.wwd.getCamera().getAsLookAt(wwd.getGlobe(), wwd.getVerticalExaggeration(), terrainPosition, this.beginLookAt);
this.wwd.cameraAsLookAt(this.beginLookAt);
this.lookAt.set(this.beginLookAt);
}
}
Expand Down
66 changes: 25 additions & 41 deletions worldwind/src/main/java/gov/nasa/worldwind/Navigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,74 @@
import gov.nasa.worldwind.geom.Camera;
import gov.nasa.worldwind.geom.LookAt;
import gov.nasa.worldwind.geom.Matrix4;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.globe.Globe;
import gov.nasa.worldwind.util.Logger;

@Deprecated
public class Navigator {

private final Camera camera;
private final WorldWindow wwd;

public Navigator(Camera camera) {
if (camera == null) {
public Navigator(WorldWindow wwd) {
if (wwd == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "constructor", "missingCamera"));
Logger.logMessage(Logger.ERROR, "Navigator", "constructor", "missingWorldwindow"));
}

this.camera = camera;
this.wwd = wwd;
}

public double getLatitude() {
return this.camera.position.latitude;
return this.wwd.camera.position.latitude;
}

public Navigator setLatitude(double latitude) {
this.camera.position.latitude = latitude;
this.wwd.camera.position.latitude = latitude;
return this;
}

public double getLongitude() {
return this.camera.position.longitude;
return this.wwd.camera.position.longitude;
}

public Navigator setLongitude(double longitude) {
this.camera.position.longitude = longitude;
this.wwd.camera.position.longitude = longitude;
return this;
}

public double getAltitude() {
return this.camera.position.altitude;
return this.wwd.camera.position.altitude;
}

public Navigator setAltitude(double altitude) {
this.camera.position.altitude = altitude;
this.wwd.camera.position.altitude = altitude;
return this;
}

public double getHeading() {
return this.camera.heading;
return this.wwd.camera.heading;
}

public Navigator setHeading(double headingDegrees) {
this.camera.heading = headingDegrees;
this.wwd.camera.heading = headingDegrees;
return this;
}

public double getTilt() {
return this.camera.tilt;
return this.wwd.camera.tilt;
}

public Navigator setTilt(double tiltDegrees) {
this.camera.tilt = tiltDegrees;
this.wwd.camera.tilt = tiltDegrees;
return this;
}

public double getRoll() {
return this.camera.roll;
return this.wwd.camera.roll;
}

public Navigator setRoll(double rollDegrees) {
this.camera.roll = rollDegrees;
this.wwd.camera.roll = rollDegrees;
return this;
}

Expand All @@ -91,7 +90,7 @@ public Camera getAsCamera(Globe globe, Camera result) {
Logger.logMessage(Logger.ERROR, "Navigator", "getAsCamera", "missingResult"));
}

result.set(this.camera);
result.set(this.wwd.camera);

return result;
}
Expand All @@ -107,55 +106,40 @@ public Navigator setAsCamera(Globe globe, Camera camera) {
Logger.logMessage(Logger.ERROR, "Navigator", "setAsCamera", "missingCamera"));
}

this.camera.set(camera);
this.wwd.camera.set(camera);

return this;
}

public LookAt getAsLookAt(Globe globe, double verticalExaggeration, Position terrainPosition, LookAt result) {
if (globe == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "getAsLookAt", "missingGlobe"));
}

public LookAt getAsLookAt(Globe globe, LookAt result) {
if (result == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "getAsLookAt", "missingResult"));
}

this.camera.getAsLookAt(globe, verticalExaggeration, terrainPosition, result);
this.wwd.cameraAsLookAt(result);

return result;
}

public Navigator setAsLookAt(Globe globe, double verticalExaggeration, LookAt lookAt) {
if (globe == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "setAsLookAt", "missingGlobe"));
}

public Navigator setAsLookAt(Globe globe, LookAt lookAt) {
if (lookAt == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "setAsLookAt", "missingLookAt"));
}

this.camera.setFromLookAt(globe, verticalExaggeration, lookAt);
this.wwd.cameraFromLookAt(lookAt);

return this;
}

public Matrix4 getAsViewingMatrix(Globe globe, double verticalExaggeration, Matrix4 result) {
if (globe == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "getAsViewingMatrix", "missingGlobe"));
}

public Matrix4 getAsViewingMatrix(Globe globe, Matrix4 result) {
if (result == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "Navigator", "getAsViewingMatrix", "missingResult"));
}

this.camera.computeViewingTransform(globe, verticalExaggeration, result);
this.wwd.cameraToViewingTransform(result);

return result;
}
Expand Down
Loading

0 comments on commit b1fe177

Please sign in to comment.