Fix lookAt

This commit is contained in:
Jack Andersen 2018-11-17 17:59:53 -10:00
parent e572a46949
commit a427e0a8a2
1 changed files with 12 additions and 6 deletions

View File

@ -167,14 +167,20 @@ CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3
CVector3f vLook, vRight, vUp; CVector3f vLook, vRight, vUp;
vLook = lookPos - pos; vLook = lookPos - pos;
if (vLook.magnitude() < FLT_EPSILON) if (vLook.magnitude() <= FLT_EPSILON)
vLook = {0.f, 1.f, 0.f}; vLook = {0.f, 1.f, 0.f};
else
vLook.normalize(); vLook.normalize();
vRight = vLook.cross(up); vUp = up - vLook * clamp(-1.f, up.dot(vLook), 1.f);
vRight.normalize(); if (vUp.magnitude() <= FLT_EPSILON)
{
vUp = vRight.cross(vLook); vUp = CVector3f(0.f, 0.f, 1.f) - vLook * vLook.z;
if (vUp.magnitude() <= FLT_EPSILON)
vUp = CVector3f(0.f, 1.f, 0.f) - vLook * vLook.y;
}
vUp.normalize();
vRight = vLook.cross(vUp);
CMatrix3f rmBasis(vRight, vLook, vUp); CMatrix3f rmBasis(vRight, vLook, vUp);
return CTransform(rmBasis, pos); return CTransform(rmBasis, pos);