[core] 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.
This commit is contained in:
Ansis Brammanis
2016-12-14 15:48:04 -08:00
committed by John Firebaugh
parent 29099399a9
commit 2cf238f74e

View File

@@ -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);