Handle zero-vector normalization. (#63)

* Handle zero-vector normalization.

When normalizing a vectors, we have to check whether vector length is
not zero, to avoid dividing by zero when normalizing zero-vectors.

* Test for normalization of zero vectors
This commit is contained in:
Emil Lauridsen
2017-06-13 18:21:55 +02:00
committed by Ben Visness
parent 09524f72ed
commit 98f535aeec
2 changed files with 69 additions and 9 deletions

View File

@@ -923,8 +923,12 @@ HMM_NormalizeVec2(hmm_vec2 A)
float VectorLength = HMM_LengthVec2(A);
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
if (VectorLength != 0.0f)
{
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
}
return (Result);
}
@@ -936,9 +940,13 @@ HMM_NormalizeVec3(hmm_vec3 A)
float VectorLength = HMM_LengthVec3(A);
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
Result.Z = A.Z * (1.0f / VectorLength);
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
if (VectorLength != 0.0f)
{
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
Result.Z = A.Z * (1.0f / VectorLength);
}
return (Result);
}
@@ -950,10 +958,14 @@ HMM_NormalizeVec4(hmm_vec4 A)
float VectorLength = HMM_LengthVec4(A);
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
Result.Z = A.Z * (1.0f / VectorLength);
Result.W = A.W * (1.0f / VectorLength);
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
if (VectorLength != 0.0f)
{
Result.X = A.X * (1.0f / VectorLength);
Result.Y = A.Y * (1.0f / VectorLength);
Result.Z = A.Z * (1.0f / VectorLength);
Result.W = A.W * (1.0f / VectorLength);
}
return (Result);
}