From 32bbc68adbc815b2f58378cf049b8d0ba1ebc47f Mon Sep 17 00:00:00 2001 From: Thompson Lee Date: Thu, 4 Aug 2016 06:34:55 -0400 Subject: [PATCH] Combined Mtx_LookAtLH and Mtx_LookAtRH into Mtx_LookAt(). --- include/c3d/maths.h | 13 ++-------- source/maths/mtx_lookat.c | 51 +++++++++++++++++++++++++++++++++++++ source/maths/mtx_lookatlh.c | 35 ------------------------- source/maths/mtx_lookatrh.c | 35 ------------------------- 4 files changed, 53 insertions(+), 81 deletions(-) create mode 100644 source/maths/mtx_lookat.c delete mode 100644 source/maths/mtx_lookatlh.c delete mode 100644 source/maths/mtx_lookatrh.c diff --git a/include/c3d/maths.h b/include/c3d/maths.h index 7139ec7..21050fc 100644 --- a/include/c3d/maths.h +++ b/include/c3d/maths.h @@ -461,18 +461,9 @@ void Mtx_PerspStereoTilt(C3D_Mtx* mtx, float fovy, float aspect, float near, flo * @param[in] cameraPosition Position of the intended camera in 3D space. * @param[in] cameraTarget Position of the intended target the camera is supposed to face in 3D space. * @param[in] cameraUpVector The vector that points straight up depending on the camera's "Up" direction. + * @param[in] isLeftHanded If true, output matrix is left-handed. If false, output matrix is right-handed. */ -void Mtx_LookAtLH(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector); - -/** - * @brief Right-handed Look-At matrix, using DirectX implementation - * @note See https://msdn.microsoft.com/en-us/library/windows/desktop/bb205343 - * @param[out] out Output matrix. - * @param[in] cameraPosition Position of the intended camera in 3D space. - * @param[in] cameraTarget Position of the intended target the camera is supposed to face in 3D space. - * @param[in] cameraUpVector The vector that points straight up depending on the camera's "Up" direction. - */ -void Mtx_LookAtRH(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector); +void Mtx_LookAt(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector, bool isLeftHanded); ///@} ///@name Quaternion Math diff --git a/source/maths/mtx_lookat.c b/source/maths/mtx_lookat.c new file mode 100644 index 0000000..bbb1a8e --- /dev/null +++ b/source/maths/mtx_lookat.c @@ -0,0 +1,51 @@ +#include + +void Mtx_LookAt(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector, bool isLeftHanded) +{ + //Left-handed Look-At matrix is a DirectX implementation. + //https://msdn.microsoft.com/en-us/library/windows/desktop/bb281710 + C3D_FVec xaxis, yaxis, zaxis; + + //Order of operations is crucial. + zaxis = FVec3_Normalize(FVec3_New(cameraPosition.x - cameraTarget.x, cameraPosition.y - cameraTarget.y, cameraPosition.z - cameraTarget.z)); + xaxis = FVec3_Normalize(FVec3_Cross(cameraUpVector, zaxis)); + yaxis = FVec3_Cross(zaxis, xaxis); + + out->r[0].x = xaxis.x; + out->r[0].y = yaxis.x; + out->r[0].z = zaxis.x; + out->r[0].w = 0.0f; + + if (isLeftHanded) + { + //Notice in LH matrix, the Y is in Row 2 of 4. In RH matrix, the Z is in Row 2 of 4. + out->r[1].x = xaxis.y; + out->r[1].y = yaxis.y; + out->r[1].z = zaxis.y; + out->r[1].w = 0.0f; + + //Notice in LH matrix, the Z is in Row 3 of 4. In RH matrix, the Y is in Row 3 of 4. + out->r[2].x = xaxis.z; + out->r[2].y = yaxis.z; + out->r[2].z = zaxis.z; + out->r[2].w = 0.0f; + } + else { + //Notice in LH matrix, the Y is in Row 2 of 4. In RH matrix, the Z is in Row 2 of 4. + out->r[1].x = xaxis.z; + out->r[1].y = yaxis.z; + out->r[1].z = zaxis.z; + out->r[1].w = 0.0f; + + //Notice in LH matrix, the Z is in Row 3 of 4. In RH matrix, the Y is in Row 3 of 4. + out->r[2].x = xaxis.y; + out->r[2].y = yaxis.y; + out->r[2].z = zaxis.y; + out->r[2].w = 0.0f; + } + + out->r[3].x = -FVec3_Dot(xaxis, cameraPosition); + out->r[3].y = -FVec3_Dot(yaxis, cameraPosition); + out->r[3].z = -FVec3_Dot(zaxis, cameraPosition); + out->r[3].w = 1.0f; +} diff --git a/source/maths/mtx_lookatlh.c b/source/maths/mtx_lookatlh.c deleted file mode 100644 index bf55004..0000000 --- a/source/maths/mtx_lookatlh.c +++ /dev/null @@ -1,35 +0,0 @@ -#include - -void Mtx_LookAtLH(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector) -{ - //Left-handed Look-At matrix is a DirectX implementation. - //https://msdn.microsoft.com/en-us/library/windows/desktop/bb281710 - C3D_FVec xaxis, yaxis, zaxis; - - //Order of operations is crucial. - zaxis = FVec3_Normalize(FVec3_New(cameraPosition.x - cameraTarget.x, cameraPosition.y - cameraTarget.y, cameraPosition.z - cameraTarget.z)); - xaxis = FVec3_Normalize(FVec3_Cross(cameraUpVector, zaxis)); - yaxis = FVec3_Cross(zaxis, xaxis); - - out->r[0].x = xaxis.x; - out->r[0].y = yaxis.x; - out->r[0].z = zaxis.x; - out->r[0].w = 0.0f; - - //Notice in LH matrix, the Y is in Row 2 of 4. In RH matrix, the Z is in Row 2 of 4. - out->r[1].x = xaxis.y; - out->r[1].y = yaxis.y; - out->r[1].z = zaxis.y; - out->r[1].w = 0.0f; - - //Notice in LH matrix, the Z is in Row 3 of 4. In RH matrix, the Y is in Row 3 of 4. - out->r[2].x = xaxis.z; - out->r[2].y = yaxis.z; - out->r[2].z = zaxis.z; - out->r[2].w = 0.0f; - - out->r[3].x = -FVec3_Dot(xaxis, cameraPosition); - out->r[3].y = -FVec3_Dot(yaxis, cameraPosition); - out->r[3].z = -FVec3_Dot(zaxis, cameraPosition); - out->r[3].w = 1.0f; -} diff --git a/source/maths/mtx_lookatrh.c b/source/maths/mtx_lookatrh.c deleted file mode 100644 index 7797163..0000000 --- a/source/maths/mtx_lookatrh.c +++ /dev/null @@ -1,35 +0,0 @@ -#include - -void Mtx_LookAtRH(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3D_FVec cameraUpVector) -{ - //Right-handed Look-At matrix is a DirectX implementation. - //https://msdn.microsoft.com/en-us/library/windows/desktop/bb281711 - C3D_FVec xaxis, yaxis, zaxis; - - //Order of operations is crucial. - zaxis = FVec3_Normalize(FVec3_New(cameraPosition.x - cameraTarget.x, cameraPosition.y - cameraTarget.y, cameraPosition.z - cameraTarget.z)); - xaxis = FVec3_Normalize(FVec3_Cross(cameraUpVector, zaxis)); - yaxis = FVec3_Cross(zaxis, xaxis); - - out->r[0].x = xaxis.x; - out->r[0].y = yaxis.x; - out->r[0].z = zaxis.x; - out->r[0].w = 0.0f; - - //Notice in RH matrix, the Z is in Row 2 of 4. In LH matrix, the Y is in Row 2 of 4. - out->r[1].x = xaxis.z; - out->r[1].y = yaxis.z; - out->r[1].z = zaxis.z; - out->r[1].w = 0.0f; - - //Notice in RH matrix, the Y is in Row 3 of 4. In LH matrix, the Z is in Row 3 of 4. - out->r[2].x = xaxis.y; - out->r[2].y = yaxis.y; - out->r[2].z = zaxis.y; - out->r[2].w = 0.0f; - - out->r[3].x = -FVec3_Dot(xaxis, cameraPosition); - out->r[3].y = -FVec3_Dot(yaxis, cameraPosition); - out->r[3].z = -FVec3_Dot(zaxis, cameraPosition); - out->r[3].w = 1.0f; -}