From 21452fe6ee702b806f2d844b62a8bb42fae45b5c Mon Sep 17 00:00:00 2001 From: fincs Date: Thu, 23 Jul 2015 22:21:49 +0200 Subject: [PATCH] Fix some broken 3D math --- source/maths/mtx_orthotilt.c | 23 ++++++++++++----------- source/maths/mtx_translate.c | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/source/maths/mtx_orthotilt.c b/source/maths/mtx_orthotilt.c index cc23cd3..635c3d9 100644 --- a/source/maths/mtx_orthotilt.c +++ b/source/maths/mtx_orthotilt.c @@ -1,11 +1,7 @@ #include -void Mtx_OrthoTilt(C3D_Mtx* mtx, float top, float bottom, float left, float right, float near, float far) +void Mtx_OrthoTilt(C3D_Mtx* mtx, float left, float right, float bottom, float top, float near, float far) { - // Notes: - // The 3DS screens are sideways and so are the top/bottom/left/right parameters - // that the caller passed. Hence why the parameter names are swapped in the implementation. - C3D_Mtx mp; Mtx_Zeros(&mp); @@ -14,17 +10,22 @@ void Mtx_OrthoTilt(C3D_Mtx* mtx, float top, float bottom, float left, float righ mp.r[0].w = (left + right) / (left - right); mp.r[1].y = 2.0f / (top - bottom); mp.r[1].w = (bottom + top) / (bottom - top); - mp.r[2].z = 2.0f / (far - near); - mp.r[2].w = (near + far) / (near - far); + mp.r[2].z = 2.0f / (near - far); + mp.r[2].w = (far + near) / (far - near); mp.r[3].w = 1.0f; // Fix depth range to [-1, 0] - C3D_Mtx mp2; + C3D_Mtx mp2, mp3; Mtx_Identity(&mp2); mp2.r[2].z = 0.5; mp2.r[2].w = -0.5; - Mtx_Multiply(mtx, &mp2, &mp); + Mtx_Multiply(&mp3, &mp2, &mp); - // Rotate the matrix one quarter of a turn CCW in order to fix the 3DS screens' orientation - Mtx_RotateZ(mtx, M_TAU/4, true); + // Fix the 3DS screens' orientation by swapping the X and Y axis + Mtx_Identity(&mp2); + mp2.r[0].x = 0.0; + mp2.r[0].y = 1.0; + mp2.r[1].x = -1.0; // flipped + mp2.r[1].y = 0.0; + Mtx_Multiply(mtx, &mp2, &mp3); } diff --git a/source/maths/mtx_translate.c b/source/maths/mtx_translate.c index 3f04302..0ed26d4 100644 --- a/source/maths/mtx_translate.c +++ b/source/maths/mtx_translate.c @@ -9,6 +9,6 @@ void Mtx_Translate(C3D_Mtx* mtx, float x, float y, float z) tm.r[1].w = y; tm.r[2].w = z; - Mtx_Multiply(&om, &tm, mtx); + Mtx_Multiply(&om, mtx, &tm); Mtx_Copy(mtx, &om); }