From c2047c0ef9339deb1952d29bb3c5698e4b328c03 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Dec 2016 15:48:04 -0800 Subject: [PATCH] fix matrix z range and remove hack ported from -js: 0b5520fa5ab2a4659d80dcffa8b035a0d84fe1ca This should fix the issue behind #2270 and remove the need for the hack added in #3740. --- src/mbgl/map/transform_state.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 51ecf52e405..f7419fa5b4b 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -36,10 +36,13 @@ void TransformState::getProjMatrix(mat4& projMatrix) const { const double groundAngle = M_PI / 2.0 + getPitch(); const double topHalfSurfaceDistance = std::sin(halfFov) * getCameraToCenterDistance() / std::sin(M_PI - groundAngle - halfFov); - // Calculate z value of the farthest fragment that should be rendered. - const double farZ = std::cos(M_PI / 2.0 - getPitch()) * topHalfSurfaceDistance + getCameraToCenterDistance(); - matrix::perspective(projMatrix, getFieldOfView(), double(size.width) / size.height, 0.1, farZ); + // Calculate z distance of the farthest fragment that should be rendered. + const double furthestDistance = std::cos(M_PI / 2 - getPitch()) * topHalfSurfaceDistance + getCameraToCenterDistance(); + // Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance` + const double farZ = furthestDistance * 1.01; + + matrix::perspective(projMatrix, getFieldOfView(), double(size.width) / size.height, 1, farZ); const bool flippedY = viewportMode == ViewportMode::FlippedY; matrix::scale(projMatrix, projMatrix, 1, flippedY ? 1 : -1, 1);