summaryrefslogtreecommitdiff
path: root/src/mbgl/map/transform_state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/map/transform_state.cpp')
-rw-r--r--src/mbgl/map/transform_state.cpp20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp
index 92c02d0bc7..77309a2a55 100644
--- a/src/mbgl/map/transform_state.cpp
+++ b/src/mbgl/map/transform_state.cpp
@@ -36,18 +36,18 @@ void TransformState::getProjMatrix(mat4& projMatrix, uint16_t nearZ, bool aligne
const double cameraToCenterDistance = getCameraToCenterDistance();
auto offset = getCenterOffset();
- // Find the distance from the viewport center point
- // [width/2 + offset.x, height/2 + offset.y] to the top edge, to point
- // [width/2 + offset.x, 0] in Z units, using the law of sines.
+ // Find the Z distance from the viewport center point
+ // [width/2 + offset.x, height/2 + offset.y] to the top edge; to point
+ // [width/2 + offset.x, 0] in Z units.
// 1 Z unit is equivalent to 1 horizontal px at the center of the map
// (the distance between[width/2, height/2] and [width/2 + 1, height/2])
- const double fovAboveCenter = getFieldOfView() * (0.5 + offset.y / size.height);
- const double groundAngle = M_PI / 2.0 + getPitch();
- const double aboveCenterSurfaceDistance = std::sin(fovAboveCenter) * cameraToCenterDistance / std::sin(M_PI - groundAngle - fovAboveCenter);
-
-
+ // See https://github.com/mapbox/mapbox-gl-native/pull/15195 for details.
+ // See TransformState::fov description: fov = 2 * arctan((height / 2) / (height * 1.5)).
+ const double tanFovAboveCenter = (size.height * 0.5 + offset.y) / (size.height * 1.5);
+ const double tanMultiple = tanFovAboveCenter * std::tan(getPitch());
+ assert(tanMultiple < 1);
// Calculate z distance of the farthest fragment that should be rendered.
- const double furthestDistance = std::cos(M_PI / 2 - getPitch()) * aboveCenterSurfaceDistance + cameraToCenterDistance;
+ const double furthestDistance = cameraToCenterDistance / (1 - tanMultiple);
// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
const double farZ = furthestDistance * 1.01;
@@ -64,7 +64,7 @@ void TransformState::getProjMatrix(mat4& projMatrix, uint16_t nearZ, bool aligne
const bool flippedY = viewportMode == ViewportMode::FlippedY;
matrix::scale(projMatrix, projMatrix, 1.0, flippedY ? 1 : -1, 1);
- matrix::translate(projMatrix, projMatrix, 0, 0, -getCameraToCenterDistance());
+ matrix::translate(projMatrix, projMatrix, 0, 0, -cameraToCenterDistance);
using NO = NorthOrientation;
switch (getNorthOrientation()) {