From 2f126f95ff7d4e30010d79faddcd69081897e3a8 Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Mon, 12 Aug 2024 00:24:12 +0100
Subject: [PATCH] Implement camera movement

---
 src/obj.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 95 insertions(+), 8 deletions(-)

diff --git a/src/obj.c b/src/obj.c
index 4b8a2a6..e5b562c 100644
--- a/src/obj.c
+++ b/src/obj.c
@@ -28,13 +28,55 @@
     V.x /= magnitude;                                                          \
     V.y /= magnitude;                                                          \
     V.z /= magnitude;                                                          \
-  } while (0);
+  } while (0)
 #define cross_product(V1, V2)                                                  \
   ((V3f){                                                                      \
       .x = V1.y * V2.z - V1.z * V2.y,                                          \
       .y = V1.z * V2.x - V1.x * V2.z,                                          \
       .z = V1.x * V2.y - V1.y * V2.x,                                          \
   })
+#define mat4x4_identity                                                        \
+  ((M4x4f){                                                                    \
+      .row0 = {1.0f, 0.0f, 0.0f, 0.0f},                                        \
+      .row1 = {0.0f, 1.0f, 0.0f, 0.0f},                                        \
+      .row2 = {0.0f, 0.0f, 1.0f, 0.0f},                                        \
+      .row3 = {0.0f, 0.0f, 0.0f, 1.0f},                                        \
+  })
+#define mat4x4_mul(MAT1, MAT2)                                                 \
+  ((M4x4f){                                                                    \
+      .row0.x = MAT1.row0.x * MAT2.row0.x + MAT1.row0.y * MAT2.row1.x +        \
+                MAT1.row0.z * MAT2.row2.x + MAT1.row0.w * MAT2.row3.x,         \
+      .row0.y = MAT1.row0.x * MAT2.row0.y + MAT1.row0.y * MAT2.row1.y +        \
+                MAT1.row0.z * MAT2.row2.y + MAT1.row0.w * MAT2.row3.y,         \
+      .row0.z = MAT1.row0.x * MAT2.row0.z + MAT1.row0.y * MAT2.row1.z +        \
+                MAT1.row0.z * MAT2.row2.z + MAT1.row0.w * MAT2.row3.z,         \
+      .row0.w = MAT1.row0.x * MAT2.row0.w + MAT1.row0.y * MAT2.row1.w +        \
+                MAT1.row0.z * MAT2.row2.w + MAT1.row0.w * MAT2.row3.w,         \
+      .row1.x = MAT1.row1.x * MAT2.row0.x + MAT1.row1.y * MAT2.row1.x +        \
+                MAT1.row1.z * MAT2.row2.x + MAT1.row1.w * MAT2.row3.x,         \
+      .row1.y = MAT1.row1.x * MAT2.row0.y + MAT1.row1.y * MAT2.row1.y +        \
+                MAT1.row1.z * MAT2.row2.y + MAT1.row1.w * MAT2.row3.y,         \
+      .row1.z = MAT1.row1.x * MAT2.row0.z + MAT1.row1.y * MAT2.row1.z +        \
+                MAT1.row1.z * MAT2.row2.z + MAT1.row1.w * MAT2.row3.z,         \
+      .row1.w = MAT1.row1.x * MAT2.row0.w + MAT1.row1.y * MAT2.row1.w +        \
+                MAT1.row1.z * MAT2.row2.w + MAT1.row1.w * MAT2.row3.w,         \
+      .row2.x = MAT1.row2.x * MAT2.row0.x + MAT1.row2.y * MAT2.row1.x +        \
+                MAT1.row2.z * MAT2.row2.x + MAT1.row2.w * MAT2.row3.x,         \
+      .row2.y = MAT1.row2.x * MAT2.row0.y + MAT1.row2.y * MAT2.row1.y +        \
+                MAT1.row2.z * MAT2.row2.y + MAT1.row2.w * MAT2.row3.y,         \
+      .row2.z = MAT1.row2.x * MAT2.row0.z + MAT1.row2.y * MAT2.row1.z +        \
+                MAT1.row2.z * MAT2.row2.z + MAT1.row2.w * MAT2.row3.z,         \
+      .row2.w = MAT1.row2.x * MAT2.row0.w + MAT1.row2.y * MAT2.row1.w +        \
+                MAT1.row2.z * MAT2.row2.w + MAT1.row2.w * MAT2.row3.w,         \
+      .row3.x = MAT1.row3.x * MAT2.row0.x + MAT1.row3.y * MAT2.row1.x +        \
+                MAT1.row3.z * MAT2.row2.x + MAT1.row3.w * MAT2.row3.x,         \
+      .row3.y = MAT1.row3.x * MAT2.row0.y + MAT1.row3.y * MAT2.row1.y +        \
+                MAT1.row3.z * MAT2.row2.y + MAT1.row3.w * MAT2.row3.y,         \
+      .row3.z = MAT1.row3.x * MAT2.row0.z + MAT1.row3.y * MAT2.row1.z +        \
+                MAT1.row3.z * MAT2.row2.z + MAT1.row3.w * MAT2.row3.z,         \
+      .row3.w = MAT1.row3.x * MAT2.row0.w + MAT1.row3.y * MAT2.row1.w +        \
+                MAT1.row3.z * MAT2.row2.w + MAT1.row3.w * MAT2.row3.w,         \
+  })
 #define mat4x4_mul_vec4(MAT, V)                                                \
   ((V4f){                                                                      \
       .x = MAT.row0.x * V.x + MAT.row0.y * V.y + MAT.row0.z * V.z +            \
@@ -58,13 +100,12 @@ struct triangle_bbox {
 
 internal void render_triangle(const Triangle *triangle, const Model *model,
                               Render *render, Colour colour, RenderType type,
-                              ProjectionType projection);
+                              ProjectionType projection, M4x4f mv);
 internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
                             V3f normals[TRIANGLE_VERTICES],
                             V2f coordinates[TRIANGLE_VERTICES], Colour colour,
                             Image *texture, RenderType type);
-internal void reorder_points(V2u vertices[TRIANGLE_VERTICES],
-                             V2f coordinates[TRIANGLE_VERTICES]);
+internal M4x4f lookat(V3f eye, V3f target, V3f up);
 internal TriangleBBox get_triangle_bbox(const Image *img,
                                         V3f vertices[TRIANGLE_VERTICES]);
 internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
@@ -85,6 +126,10 @@ M4x4f g_cam_matrix = {
 };
 // clang-format on
 
+V3f g_eye = {0.2f, 0.1f, 0.7f};
+V3f g_target = {0};
+V3f g_up = {0.0f, 1.0f, 0.0f};
+
 Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
   if (!arena) {
     return INVALID_MODEL;
@@ -182,6 +227,7 @@ void render_model(const Model *model, Render *render, Colour colour,
                   RenderType type, ColourType colour_type,
                   ProjectionType projection) {
   Triangle triangle;
+  M4x4f model_view = lookat(g_eye, g_target, g_up);
 
   for (u64 i = 0; i < model->triangles->count; ++i) {
     triangle = list_get(model->triangles, i);
@@ -191,13 +237,14 @@ void render_model(const Model *model, Render *render, Colour colour,
                         .b = rand() % UINT8_MAX,
                         .a = 255};
     }
-    render_triangle(&triangle, model, render, colour, type, projection);
+    render_triangle(&triangle, model, render, colour, type, projection,
+                    model_view);
   }
 }
 
 internal void render_triangle(const Triangle *triangle, const Model *model,
                               Render *render, Colour colour, RenderType type,
-                              ProjectionType projection) {
+                              ProjectionType projection, M4x4f mv) {
   Image *img = &(render->img);
   V3f vertices[TRIANGLE_VERTICES] = {
       list_get(model->vertices, triangle->p0),
@@ -215,8 +262,21 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
       list_get(model->texture_coordinates, triangle->tx2),
   };
 
+  // Camera
+  for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
+    V4f vertex;
+    vertex = (V4f){
+        .x = vertices[i].x,
+        .y = vertices[i].y,
+        .z = vertices[i].z,
+        .w = 1.0f,
+    };
+    vertex = mat4x4_mul_vec4(mv, vertex);
+    vertices[i] = project_vec4(vertex);
+  }
+
+  // Basic perspective projection
   if (projection == PROJECTION_TYPE_PERSPECTIVE) {
-    // Basic perspective projection
     V4f vertex;
     for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
       vertex = (V4f){
@@ -304,7 +364,7 @@ internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
         }
 
         if (intensity < 0.0f) {
-          continue;
+          intensity = 0.01f;
         }
 
         if (texture) {
@@ -329,6 +389,33 @@ internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
   }
 }
 
+internal M4x4f lookat(V3f eye, V3f target, V3f up) {
+  V3f z = V3(V3f, f32, target.x, target.y, target.z, eye.x, eye.y, eye.z);
+  normalise_v3(z);
+  V3f x = cross_product(up, z);
+  normalise_v3(x);
+  V3f y = cross_product(z, x);
+  normalise_v3(y);
+
+  M4x4f rotation = mat4x4_identity;
+  rotation.row0.x = x.x;
+  rotation.row0.y = x.y;
+  rotation.row0.z = x.z;
+  rotation.row1.x = y.x;
+  rotation.row1.y = y.y;
+  rotation.row1.z = y.z;
+  rotation.row2.x = z.x;
+  rotation.row2.y = z.y;
+  rotation.row2.z = z.z;
+
+  M4x4f translation = mat4x4_identity;
+  translation.row0.w = -(eye.x);
+  translation.row1.w = -(eye.y);
+  translation.row2.w = -(eye.z);
+
+  return mat4x4_mul(rotation, translation);
+}
+
 internal TriangleBBox get_triangle_bbox(const Image *img,
                                         V3f vertices[TRIANGLE_VERTICES]) {
   f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x));