From b1355d472cf742ace4d26eb2a12216b953ba57ce Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sun, 8 Apr 2018 03:57:05 +0200 Subject: [PATCH] - Fix model projection matrix --- src/swrenderer/viewport/r_viewport.cpp | 15 +++++++++------ src/swrenderer/viewport/r_viewport.h | 3 ++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/swrenderer/viewport/r_viewport.cpp b/src/swrenderer/viewport/r_viewport.cpp index 73fb7fe482..f9eb35f968 100644 --- a/src/swrenderer/viewport/r_viewport.cpp +++ b/src/swrenderer/viewport/r_viewport.cpp @@ -62,7 +62,7 @@ namespace swrenderer void RenderViewport::SetupPolyViewport(RenderThread *thread) { WorldToView = SoftwareWorldToView(viewpoint); - ViewToClip = SoftwareViewToClip(viewwindow.FocalTangent, CenterY, YaspectMul); + ViewToClip = SoftwareViewToClip(); WorldToClip = ViewToClip * WorldToView; } @@ -78,14 +78,17 @@ namespace swrenderer return m * Mat4f::Translate((float)-viewpoint.Pos.X, (float)-viewpoint.Pos.Y, (float)-viewpoint.Pos.Z); } - Mat4f RenderViewport::SoftwareViewToClip(double focalTangent, double centerY, double YaspectMul) + Mat4f RenderViewport::SoftwareViewToClip() { float near = 5.0f; float far = 65536.0f; - float width = (float)(focalTangent * near); - float top = (float)(centerY / viewheight * YaspectMul * near); - float bottom = (float)(top - YaspectMul * near); - return Mat4f::Frustum(-width, width, bottom, top, near, far, Handedness::Right, ClipZRange::NegativePositiveW); + float width = CenterX / FocalLengthX; + float height = viewheight * 0.5 / FocalLengthY; + float offset = CenterY / FocalLengthY - height; + width *= near; + height *= near; + offset *= near; + return Mat4f::Frustum(-width, width, -height + offset, height + offset, near, far, Handedness::Right, ClipZRange::NegativePositiveW); } void RenderViewport::SetViewport(RenderThread *thread, int fullWidth, int fullHeight, float trueratio) diff --git a/src/swrenderer/viewport/r_viewport.h b/src/swrenderer/viewport/r_viewport.h index 1c6510cca5..d9e337f0b9 100644 --- a/src/swrenderer/viewport/r_viewport.h +++ b/src/swrenderer/viewport/r_viewport.h @@ -84,6 +84,7 @@ namespace swrenderer void SetupBuffer(); static Mat4f SoftwareWorldToView(const FRenderViewpoint &viewpoint); - static Mat4f SoftwareViewToClip(double focalTangent, double centerY, double YaspectMul); + + Mat4f SoftwareViewToClip(); }; }