From 14628e39372dd26b0d0e081f5d36fd2d3e9a9809 Mon Sep 17 00:00:00 2001 From: James Urquhart Date: Fri, 20 May 2016 16:15:56 +0100 Subject: [PATCH] Fix setNearFarDist for off-center projections --- Engine/source/math/util/frustum.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Engine/source/math/util/frustum.cpp b/Engine/source/math/util/frustum.cpp index bbcc16f83..181e140ef 100644 --- a/Engine/source/math/util/frustum.cpp +++ b/Engine/source/math/util/frustum.cpp @@ -214,8 +214,26 @@ void Frustum::setNearFarDist( F32 nearDist, F32 farDist ) return; // Recalculate the frustum. - MatrixF xfm( mTransform ); - set( mIsOrtho, getFov(), getAspectRatio(), nearDist, farDist, xfm ); + MatrixF xfm( mTransform ); + + const F32 CENTER_EPSILON = 0.01; + F32 centerX = mNearLeft + (mNearRight - mNearLeft) * 0.5; + F32 centerY = mNearBottom + (mNearTop - mNearBottom) * 0.5; + if ((centerX > CENTER_EPSILON || centerX < -CENTER_EPSILON) || (centerY > CENTER_EPSILON || centerY < -CENTER_EPSILON) ) + { + // Off-center projection, so re-calc use the new distances + FovPort expectedFovPort; + expectedFovPort.leftTan = -(mNearLeft / mNearDist); + expectedFovPort.rightTan = (mNearRight / mNearDist); + expectedFovPort.upTan = (mNearTop / mNearDist); + expectedFovPort.downTan = -(mNearBottom / mNearDist); + MathUtils::makeFovPortFrustum(this, mIsOrtho, nearDist, farDist, expectedFovPort); + } + else + { + // Projection is not off-center, use the normal code + set(mIsOrtho, getFov(), getAspectRatio(), nearDist, farDist, xfm); + } } //-----------------------------------------------------------------------------