Fix Mtx_LookAt

This commit is contained in:
Michael Theall 2016-08-04 23:41:47 -05:00
parent 59b0311f48
commit 24548b03d1
2 changed files with 52 additions and 48 deletions

View File

@ -8,44 +8,30 @@ void Mtx_LookAt(C3D_Mtx* out, C3D_FVec cameraPosition, C3D_FVec cameraTarget, C3
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));
if (isLeftHanded)
zaxis = FVec3_Normalize(FVec3_Subtract(cameraTarget, cameraPosition));
else
zaxis = FVec3_Normalize(FVec3_Subtract(cameraPosition, cameraTarget));
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)
{
//In LH matrix, the Y is in Row 2 of 4 and the Z is in Row 3 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;
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
{
//In RH matrix, the Z is in Row 2 of 4, and the Y is in Row 3 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;
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[0].y = xaxis.y;
out->r[0].z = xaxis.z;
out->r[0].w = -FVec3_Dot(xaxis, cameraPosition);
out->r[1].x = yaxis.x;
out->r[1].y = yaxis.y;
out->r[1].z = yaxis.z;
out->r[1].w = -FVec3_Dot(yaxis, cameraPosition);
out->r[2].x = zaxis.x;
out->r[2].y = zaxis.y;
out->r[2].z = zaxis.z;
out->r[2].w = -FVec3_Dot(zaxis, cameraPosition);
out->r[3].x = 0.0f;
out->r[3].y = 0.0f;
out->r[3].z = 0.0f;
out->r[3].w = 1.0f;
}

View File

@ -99,7 +99,7 @@ operator==(const glm::mat4 &lhs, const C3D_Mtx &rhs)
for(size_t j = 0; j < 4; ++j)
{
if(std::abs(lhs[i][j] - rhs.m[j*4+3-i]) > 0.001f)
return false;
return false; // LCOV_EXCL_LINE This would cause an assertion failure
}
}
@ -115,12 +115,6 @@ operator==(const C3D_Mtx &lhs, const glm::mat4 &rhs)
static inline bool
operator==(const glm::quat &lhs, const C3D_FQuat &rhs)
{
if((std::isnan(lhs.w) && std::isnan(rhs.r))
|| (std::isnan(lhs.x) && std::isnan(rhs.i))
|| (std::isnan(lhs.y) && std::isnan(rhs.j))
|| (std::isnan(lhs.z) && std::isnan(rhs.k)))
return true;
return std::abs(lhs.w - rhs.r) < 0.01f
&& std::abs(lhs.x - rhs.i) < 0.01f
&& std::abs(lhs.y - rhs.j) < 0.01f
@ -136,12 +130,6 @@ operator==(const C3D_FQuat &lhs, const glm::quat &rhs)
static inline bool
operator==(const C3D_FQuat &lhs, const C3D_FQuat &rhs)
{
if((std::isnan(lhs.r) && std::isnan(rhs.r))
|| (std::isnan(lhs.i) && std::isnan(rhs.i))
|| (std::isnan(lhs.j) && std::isnan(rhs.j))
|| (std::isnan(lhs.k) && std::isnan(rhs.k)))
return true;
return std::abs(lhs.r - rhs.r) < 0.01f
&& std::abs(lhs.i - rhs.i) < 0.01f
&& std::abs(lhs.j - rhs.j) < 0.01f
@ -530,6 +518,36 @@ check_matrix(generator_t &gen, distribution_t &dist)
assert(m == tilt*fix_depth*glm::scale(g, z_flip));
}
// check lookAt
{
C3D_Mtx m;
C3D_FVec camera, target, diff, up;
// avoid very small distances and 'up' pointing near the target
do
{
camera = FVec3_New(dist(gen), dist(gen), dist(gen));
target = FVec3_New(dist(gen), dist(gen), dist(gen));
up = FVec3_New(dist(gen), dist(gen), dist(gen));
diff = FVec3_Subtract(target, camera);
} while(FVec3_Magnitude(diff) < 0.25f
|| FVec3_Magnitude(up) < 0.25f
|| FVec3_Dot(up, diff) / FVec3_Magnitude(up) / FVec3_Magnitude(diff) < cosf(30.0f*M_TAU/360.0f));
glm::mat4 g = glm::lookAt(glm::vec3(camera.x, camera.y, camera.z),
glm::vec3(target.x, target.y, target.z),
glm::vec3(up.x, up.y, up.z));
// RH
Mtx_LookAt(&m, camera, target, up, false);
assert(m == g);
// LH
Mtx_LookAt(&m, camera, target, up, true);
// I can't say for certain that this is the correct test
assert(m == glm::scale(glm::mat4(), glm::vec3(-1.0f, 1.0f, -1.0f))*g);
}
// check multiply
{
C3D_Mtx m1, m2;