Kajetan Johannes Hammerle 11 mesi fa
parent
commit
d5b56e2175
4 ha cambiato i file con 36 aggiunte e 26 eliminazioni
  1. 14 2
      include/core/Generic.h
  2. 0 1
      include/core/Vector.h
  3. 21 22
      src/Matrix.c
  4. 1 1
      test/modules/VectorTests.c

+ 14 - 2
include/core/Generic.h

@@ -1,6 +1,8 @@
 #ifndef CORE_GENERIC_H
 #define CORE_GENERIC_H
 
+#include "core/Matrix.h"
+
 #ifdef IMPORT_CORE
 #define addSet(a, b)                                                           \
     _Generic((a),                                                              \
@@ -56,7 +58,11 @@
 #define mul(a, b, c)                                                           \
     _Generic((a),                                                              \
         Vector2 *: _Generic((c), Vector2 *: mulV2, default: mulV2F),           \
-        Vector3 *: _Generic((c), Vector3 *: mulV3, default: mulV3F),           \
+        Vector3 *: _Generic((c),                                               \
+                 Vector3 *: _Generic((b),                                      \
+                          const Quaternion*: mulQV3,                           \
+                          default: mulV3),                                     \
+                 default: mulV3F),                                             \
         Vector4 *: _Generic((c), Vector4 *: mulV4, default: mulV4F),           \
         IntVector2 *: _Generic((c), IntVector2 *: mulIV2, default: mulIV2F),   \
         IntVector3 *: _Generic((c), IntVector3 *: mulIV3, default: mulIV3F),   \
@@ -107,7 +113,13 @@
         IntVector4 *: invertIV4)(a, b)
 
 #define dot(a, b)                                                              \
-    _Generic((a), Vector2 *: dotV2, Vector3 *: dotV3, Vector4 *: dotV4)(a, b)
+    _Generic((a),                                                              \
+        Vector2 *: dotV2,                                                      \
+        const Vector2*: dotV2,                                                 \
+        Vector3*: dotV3,                                                       \
+        const Vector3*: dotV3,                                                 \
+        Vector4*: dotV4,                                                       \
+        const Vector4*: dotV4)(a, b)
 
 #define squareLength(a)                                                        \
     _Generic((a),                                                              \

+ 0 - 1
include/core/Vector.h

@@ -1,7 +1,6 @@
 #ifndef CORE_VECTOR_H
 #define CORE_VECTOR_H
 
-#include "core/Generic.h"
 #include "core/Types.h"
 
 #define CORE_VECTOR_OP2(name) name *r, const name *a

+ 21 - 22
src/Matrix.c

@@ -1,8 +1,7 @@
-#include "core/Matrix.h"
-
 #include <math.h>
 #include <stdio.h>
 
+#include "core/Generic.h"
 #include "core/Utility.h"
 
 #define M(m, x, y) ((m)->data[x].data[y])
@@ -22,10 +21,10 @@ Matrix* transposeMatrix(Matrix* m) {
 Matrix* mulSetMatrix(Matrix* m, const Matrix* a) {
     for(int i = 0; i < 4; i++) {
         Vector4 d = VECTOR4;
-        addSetV4(&d, mulV4F(CV40, a->data + 0, m->data[i].data[0]));
-        addSetV4(&d, mulV4F(CV40, a->data + 1, m->data[i].data[1]));
-        addSetV4(&d, mulV4F(CV40, a->data + 2, m->data[i].data[2]));
-        addSetV4(&d, mulV4F(CV40, a->data + 3, m->data[i].data[3]));
+        addSet(&d, mul(CV40, a->data + 0, m->data[i].data[0]));
+        addSet(&d, mul(CV40, a->data + 1, m->data[i].data[1]));
+        addSet(&d, mul(CV40, a->data + 2, m->data[i].data[2]));
+        addSet(&d, mul(CV40, a->data + 3, m->data[i].data[3]));
         m->data[i] = d;
     }
     return m;
@@ -39,17 +38,17 @@ Matrix* mulMatrix(Matrix* m, const Matrix* a, const Matrix* b) {
 
 Vector3* mulMatrixV3(Vector3* v, const Matrix* m, const Vector3* a) {
     Vector4 v4 = {{a->data[0], a->data[1], a->data[2], 1.0f}};
-    v->data[0] = dotV4(m->data + 0, &v4);
-    v->data[1] = dotV4(m->data + 1, &v4);
-    v->data[2] = dotV4(m->data + 2, &v4);
-    mulSetV3F(v, 1.0f / dotV4(m->data + 3, &v4));
+    v->data[0] = dot(m->data + 0, &v4);
+    v->data[1] = dot(m->data + 1, &v4);
+    v->data[2] = dot(m->data + 2, &v4);
+    mulSet(v, 1.0f / dot(m->data + 3, &v4));
     return v;
 }
 
 Matrix* scaleMatrix(Matrix* m, const Vector3* v) {
-    mulSetV4F(m->data + 0, v->data[0]);
-    mulSetV4F(m->data + 1, v->data[1]);
-    mulSetV4F(m->data + 2, v->data[2]);
+    mulSet(m->data + 0, v->data[0]);
+    mulSet(m->data + 1, v->data[1]);
+    mulSet(m->data + 2, v->data[2]);
     return m;
 }
 
@@ -65,17 +64,17 @@ Matrix* translateMatrix(Matrix* m, const Vector3* v) {
 }
 
 Matrix* translateMatrixX(Matrix* m, float tx) {
-    addSetV4(m->data + 0, mulV4F(CV40, m->data + 3, tx));
+    addSet(m->data + 0, mul(CV40, m->data + 3, tx));
     return m;
 }
 
 Matrix* translateMatrixY(Matrix* m, float ty) {
-    addSetV4(m->data + 1, mulV4F(CV40, m->data + 3, ty));
+    addSet(m->data + 1, mul(CV40, m->data + 3, ty));
     return m;
 }
 
 Matrix* translateMatrixZ(Matrix* m, float tz) {
-    addSetV4(m->data + 2, mulV4F(CV40, m->data + 3, tz));
+    addSet(m->data + 2, mul(CV40, m->data + 3, tz));
     return m;
 }
 
@@ -92,8 +91,8 @@ static Matrix* rotate(Matrix* m, float degrees, int a, int b) {
     float sin = sinf(degrees);
     float cos = cosf(degrees);
     Vector4 v = m->data[a];
-    subV4(m->data + a, mulV4F(CV40, &v, cos), mulV4F(CV40, m->data + b, sin));
-    addV4(m->data + b, mulV4F(CV40, &v, sin), mulV4F(CV40, m->data + b, cos));
+    sub(m->data + a, mul(CV40, &v, cos), mul(CV40, m->data + b, sin));
+    add(m->data + b, mul(CV40, &v, sin), mul(CV40, m->data + b, cos));
     return m;
 }
 
@@ -114,10 +113,10 @@ Matrix* rotateMatrix(Matrix* m, const Quaternion* q) {
     Vector3 b;
     Vector3 c;
     Vector3 d;
-    mulQV3(&a, q, &(Vector3){{M(m, 0, 0), M(m, 1, 0), M(m, 2, 0)}});
-    mulQV3(&b, q, &(Vector3){{M(m, 0, 1), M(m, 1, 1), M(m, 2, 1)}});
-    mulQV3(&c, q, &(Vector3){{M(m, 0, 2), M(m, 1, 2), M(m, 2, 2)}});
-    mulQV3(&d, q, &(Vector3){{M(m, 0, 3), M(m, 1, 3), M(m, 2, 3)}});
+    mul(&a, q, (&(Vector3){{M(m, 0, 0), M(m, 1, 0), M(m, 2, 0)}}));
+    mul(&b, q, (&(Vector3){{M(m, 0, 1), M(m, 1, 1), M(m, 2, 1)}}));
+    mul(&c, q, (&(Vector3){{M(m, 0, 2), M(m, 1, 2), M(m, 2, 2)}}));
+    mul(&d, q, (&(Vector3){{M(m, 0, 3), M(m, 1, 3), M(m, 2, 3)}}));
     m->data[0] = (Vector4){{a.data[0], b.data[0], c.data[0], d.data[0]}};
     m->data[1] = (Vector4){{a.data[1], b.data[1], c.data[1], d.data[1]}};
     m->data[2] = (Vector4){{a.data[2], b.data[2], c.data[2], d.data[2]}};

+ 1 - 1
test/modules/VectorTests.c

@@ -1,7 +1,7 @@
 #include <math.h>
 
 #include "../Tests.h"
-#include "core/Vector.h"
+#include "core/Generic.h"
 
 static const float eps = 0.0001f;