Browse Source

This should have been submitted long ago.. Oops.

Godzil 3 years ago
parent
commit
1e4e5fc89c
1 changed files with 58 additions and 0 deletions
  1. 58 0
      source/matrix.c

+ 58 - 0
source/matrix.c

@@ -0,0 +1,58 @@
+ /*
+ * 3D Engine 
+ * matrice.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 06/03/2021.
+ */
+
+#include <matrix.h>
+
+/* Matrix operations */
+vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v)
+{
+    vec4_t ret =
+    {
+        .x = FastGet4(mat, 0, 0) * v.x +
+             FastGet4(mat, 0, 1) * v.y +
+             FastGet4(mat, 0, 2) * v.z +
+             FastGet4(mat, 0, 3) * v.w,
+        .y = FastGet4(mat, 1, 0) * v.x +
+             FastGet4(mat, 1, 1) * v.y +
+             FastGet4(mat, 1, 2) * v.z +
+             FastGet4(mat, 1, 3) * v.w,
+        .z = FastGet4(mat, 2, 0) * v.x +
+             FastGet4(mat, 2, 1) * v.y +
+             FastGet4(mat, 2, 2) * v.z +
+             FastGet4(mat, 2, 3) * v.w,
+        .w = FastGet4(mat, 3, 0) * v.x +
+             FastGet4(mat, 3, 1) * v.y +
+             FastGet4(mat, 3, 2) * v.z +
+             FastGet4(mat, 3, 3) * v.w,
+    };
+    return ret;
+}
+
+/* Matrix creations */
+matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ)
+{
+    matrix4_t ret = {
+        scaleX, 0, 0,0,
+        0, scaleY, 0, 0,
+        0, 0, scaleZ, 0,
+        0, 0, 0, 1
+    };
+    return ret;
+}
+
+matrix4_t mat4Translate(double tX, double tY, double tZ)
+{
+    matrix4_t ret = {
+        1, 0, 0, tX,
+        0, 1, 0, tY,
+        0, 0, 1, tZ,
+        0, 0, 0, 1
+    };
+    return ret;
+}