#include "vec.h"

#define DEPTH_MAX 255

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);
}

M4x4f projection(f32 coeff) {
  // clang-format off
  return (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, coeff, 1.0f},
  };
  // clang-format on
}

M4x4f viewport(f32 x, f32 y, u64 w, u64 h) {
  M4x4f output = mat4x4_identity;

  f32 half_width = (f32)w * 0.5f;
  f32 half_height = (f32)h * 0.5f;
  f32 half_depth = (f32)DEPTH_MAX * 0.5f;

  output.row0.x = half_width;
  output.row0.w = x + half_width;
  output.row1.y = half_height;
  output.row1.w = y + half_height;
  output.row2.z = output.row2.w = half_depth;

  return output;
}

M3x3f mat3x3_inv(M3x3f matrix) {
  M3x3f dest = {0};
  float det;

  float a = matrix.rows[0].elements[0], b = matrix.rows[0].elements[1],
        c = matrix.rows[0].elements[2], d = matrix.rows[1].elements[0],
        e = matrix.rows[1].elements[1], f = matrix.rows[1].elements[2],
        g = matrix.rows[2].elements[0], h = matrix.rows[2].elements[1],
        i = matrix.rows[2].elements[2];

  dest.rows[0].elements[0] = e * i - f * h;
  dest.rows[0].elements[1] = -(b * i - h * c);
  dest.rows[0].elements[2] = b * f - e * c;
  dest.rows[1].elements[0] = -(d * i - g * f);
  dest.rows[1].elements[1] = a * i - c * g;
  dest.rows[1].elements[2] = -(a * f - d * c);
  dest.rows[2].elements[0] = d * h - g * e;
  dest.rows[2].elements[1] = -(a * h - g * b);
  dest.rows[2].elements[2] = a * e - b * d;

  det = 1.0f / (a * dest.rows[0].elements[0] + b * dest.rows[1].elements[0] +
                c * dest.rows[2].elements[0]);

  dest.rows[0].elements[0] *= det;
  dest.rows[0].elements[1] *= det;
  dest.rows[0].elements[2] *= det;
  dest.rows[1].elements[0] *= det;
  dest.rows[1].elements[1] *= det;
  dest.rows[1].elements[2] *= det;
  dest.rows[2].elements[0] *= det;
  dest.rows[2].elements[1] *= det;
  dest.rows[2].elements[2] *= det;

  return dest;
}

M4x4f mat4x4_inv(M4x4f matrix) {
  f32 mat[4][4] = mat4x4_to_array(matrix);
  f32 dest[4][4] = {0};

  f32 t[6];
  f32 det;
  f32 a = mat[0][0], b = mat[0][1], c = mat[0][2], d = mat[0][3], e = mat[1][0],
      f = mat[1][1], g = mat[1][2], h = mat[1][3], i = mat[2][0], j = mat[2][1],
      k = mat[2][2], l = mat[2][3], m = mat[3][0], n = mat[3][1], o = mat[3][2],
      p = mat[3][3];

  t[0] = k * p - o * l;
  t[1] = j * p - n * l;
  t[2] = j * o - n * k;
  t[3] = i * p - m * l;
  t[4] = i * o - m * k;
  t[5] = i * n - m * j;

  dest[0][0] = f * t[0] - g * t[1] + h * t[2];
  dest[1][0] = -(e * t[0] - g * t[3] + h * t[4]);
  dest[2][0] = e * t[1] - f * t[3] + h * t[5];
  dest[3][0] = -(e * t[2] - f * t[4] + g * t[5]);

  dest[0][1] = -(b * t[0] - c * t[1] + d * t[2]);
  dest[1][1] = a * t[0] - c * t[3] + d * t[4];
  dest[2][1] = -(a * t[1] - b * t[3] + d * t[5]);
  dest[3][1] = a * t[2] - b * t[4] + c * t[5];

  t[0] = g * p - o * h;
  t[1] = f * p - n * h;
  t[2] = f * o - n * g;
  t[3] = e * p - m * h;
  t[4] = e * o - m * g;
  t[5] = e * n - m * f;

  dest[0][2] = b * t[0] - c * t[1] + d * t[2];
  dest[1][2] = -(a * t[0] - c * t[3] + d * t[4]);
  dest[2][2] = a * t[1] - b * t[3] + d * t[5];
  dest[3][2] = -(a * t[2] - b * t[4] + c * t[5]);

  t[0] = g * l - k * h;
  t[1] = f * l - j * h;
  t[2] = f * k - j * g;
  t[3] = e * l - i * h;
  t[4] = e * k - i * g;
  t[5] = e * j - i * f;

  dest[0][3] = -(b * t[0] - c * t[1] + d * t[2]);
  dest[1][3] = a * t[0] - c * t[3] + d * t[4];
  dest[2][3] = -(a * t[1] - b * t[3] + d * t[5]);
  dest[3][3] = a * t[2] - b * t[4] + c * t[5];

  det = 1.0f /
        (a * dest[0][0] + b * dest[1][0] + c * dest[2][0] + d * dest[3][0]);

  dest[0][0] *= det;
  dest[0][1] *= det;
  dest[0][2] *= det;
  dest[0][3] *= det;
  dest[1][0] *= det;
  dest[1][1] *= det;
  dest[1][2] *= det;
  dest[1][3] *= det;
  dest[2][0] *= det;
  dest[2][1] *= det;
  dest[2][2] *= det;
  dest[2][3] *= det;
  dest[3][0] *= det;
  dest[3][1] *= det;
  dest[3][2] *= det;
  dest[3][3] *= det;

  return array_to_mat4x4(dest);
}
