Repository: HandmadeMath/HandmadeMath (c)

Issue:
HMM_LookAt Using/Returning Partially Uninitialized Matrix
Hi there. I was struggling to debug an issue and traced it back to `HMM_LookAt`...

The issue has been marked below with comments.

	
    hmm_mat4 Result; // uninitialized    

    hmm_vec3 F = HMM_NormalizeVec3(HMM_SubtractVec3(Center, Eye));
    hmm_vec3 S = HMM_NormalizeVec3(HMM_Cross(F, Up));
    hmm_vec3 U = HMM_Cross(S, F);

    Result.Elements[0][0] = S.X;
    Result.Elements[0][1] = U.X;
    Result.Elements[0][2] = -F.X;
    // not setting the element at [0][3]

    Result.Elements[1][0] = S.Y;
    Result.Elements[1][1] = U.Y;
    Result.Elements[1][2] = -F.Y;
    // (or at [1][3])

    Result.Elements[2][0] = S.Z;
    Result.Elements[2][1] = U.Z;
    Result.Elements[2][2] = -F.Z;
    // (or [2][3])

    Result.Elements[3][0] = -HMM_DotVec3(S, Eye);
    Result.Elements[3][1] = -HMM_DotVec3(U, Eye);
    Result.Elements[3][2] = HMM_DotVec3(F, Eye);
    Result.Elements[3][3] = 1.0f;
    
    // result returned without initializing the totality of the bottom row
    return (Result);

It was fixed by initializing `Result` to `HMM_Mat4d(1.f)`, but am unsure how you'd like to solve this particularly (it's probably better to just directly write to those elements that weren't initialized).


Discussion:
I swear I fixed that before...sorry about that. Fix version incoming.

Patch:
diff --git a/HandmadeMath.h b/HandmadeMath.h
--- a/HandmadeMath.h
+++ b/HandmadeMath.h
@@ -1,5 +1,5 @@
 /*
-  HandmadeMath.h v1.5.0
+  HandmadeMath.h v1.5.1
   
   This is a single header file with a bunch of useful functions for game and
   graphics math operations.
@@ -166,6 +166,8 @@
           (*) Changed internal structure for better performance and inlining.
           (*) As a result, HANDMADE_MATH_NO_INLINE has been removed and no
               longer has any effect.
+     1.5.1
+          (*) Fixed a bug with uninitialized elements in HMM_LookAt.
           
           
   LICENSE
@@ -2334,14 +2336,17 @@ hmm_mat4 HMM_LookAt(hmm_vec3 Eye, hmm_vec3 Center, hmm_vec3 Up)
     Result.Elements[0][0] = S.X;
     Result.Elements[0][1] = U.X;
     Result.Elements[0][2] = -F.X;
+    Result.Elements[0][3] = 0.0f;
 
     Result.Elements[1][0] = S.Y;
     Result.Elements[1][1] = U.Y;
     Result.Elements[1][2] = -F.Y;
+    Result.Elements[1][3] = 0.0f;
 
     Result.Elements[2][0] = S.Z;
     Result.Elements[2][1] = U.Z;
     Result.Elements[2][2] = -F.Z;
+    Result.Elements[2][3] = 0.0f;
 
     Result.Elements[3][0] = -HMM_DotVec3(S, Eye);
     Result.Elements[3][1] = -HMM_DotVec3(U, Eye);
diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
 
 Version         | Changes        | 
 ----------------|----------------|
+**1.5.1** | Fixed a bug with uninitialized elements in HMM_LookAt.
 **1.5.0** | Changed internal structure for better performance and inlining. As a result, `HANDMADE_MATH_NO_INLINE` has been removed and no longer has any effect.
 **1.4.0** | Fixed bug when using C mode. SSE'd all vec4 operations. Removed zeroing for better performance.
 **1.3.0** | Removed need to `#define HANDMADE_MATH_CPP_MODE`. C++ definitions are now included automatically in C++ environments.


Test patch:
diff --git a/test/categories/Transformation.h b/test/categories/Transformation.h
--- a/test/categories/Transformation.h
+++ b/test/categories/Transformation.h
@@ -51,3 +51,27 @@ TEST(Transformations, Scale)
     EXPECT_FLOAT_EQ(scaled.Z, 1.5f);
     EXPECT_FLOAT_EQ(scaled.W, 1.0f);
 }
+
+TEST(Transformations, LookAt)
+{
+    const float abs_error = 0.0001f;
+
+    hmm_mat4 result = HMM_LookAt(HMM_Vec3(1.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 2.0f, 1.0f), HMM_Vec3(2.0f, 1.0f, 1.0f));
+
+    EXPECT_NEAR(result.Elements[0][0], 0.169031f, abs_error);
+    EXPECT_NEAR(result.Elements[0][1], 0.897085f, abs_error);
+    EXPECT_NEAR(result.Elements[0][2], 0.408248f, abs_error);
+    EXPECT_FLOAT_EQ(result.Elements[0][3], 0.0f);
+    EXPECT_NEAR(result.Elements[1][0], 0.507093f, abs_error);
+    EXPECT_NEAR(result.Elements[1][1], 0.276026f, abs_error);
+    EXPECT_NEAR(result.Elements[1][2], -0.816497f, abs_error);
+    EXPECT_FLOAT_EQ(result.Elements[1][3], 0.0f);
+    EXPECT_NEAR(result.Elements[2][0], -0.845154f, abs_error);
+    EXPECT_NEAR(result.Elements[2][1], 0.345033f, abs_error);
+    EXPECT_NEAR(result.Elements[2][2], -0.408248f, abs_error);
+    EXPECT_FLOAT_EQ(result.Elements[2][3], 0.0f);
+    EXPECT_NEAR(result.Elements[3][0], -0.169031f, abs_error);
+    EXPECT_NEAR(result.Elements[3][1], -0.897085f, abs_error);
+    EXPECT_NEAR(result.Elements[3][2], -0.408248f, abs_error);
+    EXPECT_FLOAT_EQ(result.Elements[3][3], 1.0f);
+}