add function to apply a matrix to a vector

Sun, 13 Jul 2025 14:22:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 13 Jul 2025 14:22:40 +0200
changeset 203
1883bdc4fb20
parent 202
d6fd0eabe568
child 204
be5cf64b5c29

add function to apply a matrix to a vector

src/ascension/datatypes.h file | annotate | diff | comparison | revisions
--- a/src/ascension/datatypes.h	Sun Jul 13 13:48:13 2025 +0200
+++ b/src/ascension/datatypes.h	Sun Jul 13 14:22:40 2025 +0200
@@ -114,6 +114,14 @@
 #define ASC_VEC3F_0 (asc_vec3f){{0.0f, 0.0f, 0.0f}}
 #define ASC_VEC3F_1 (asc_vec3f){{1.0f, 1.0f, 1.0f}}
 
+typedef union asc_vec4f {
+    struct { float x, y, z, w; };
+    float data[4];
+} asc_vec4f;
+#define ASC_VEC4F(x, y, z, w) (asc_vec4f){{(float)x, (float)y, (float)(z), (float)(w)}}
+#define ASC_VEC4F_0 (asc_vec4f){{0.0f, 0.0f, 0.0f, 0.0f}}
+#define ASC_VEC4F_1 (asc_vec4f){{1.0f, 1.0f, 1.0f, 1.0f}}
+
 typedef struct asc_col4i {
     asc_ubyte red, green, blue, alpha;
 } asc_col4i;
@@ -387,7 +395,7 @@
     for (unsigned i = 0; i < 4; i++) {
         for (unsigned j = 0; j < 4; j++) {
             dest[asc_mat4_index(i, j)] = 0;
-            for (int k = 0; k < 4; k++) {
+            for (unsigned k = 0; k < 4; k++) {
                 dest[asc_mat4_index(i,j)] +=
                         left[asc_mat4_index(k,j)] * right[asc_mat4_index(i,k)];
             }
@@ -395,4 +403,17 @@
     }
 }
 
+static inline asc_vec3f asc_mat4f_apply(
+    asc_mat4f const mat,
+    asc_vec3f vec
+) {
+    asc_vec4f dest = ASC_VEC4F_0;
+    for (unsigned j = 0; j < 4; j++) {
+        for (unsigned k = 0; k < 4; k++) {
+            dest.data[j] += mat[asc_mat4_index(k,j)] * vec.data[k];
+        }
+    }
+    return ASC_VEC3F(dest.x, dest.y, dest.z);
+}
+
 #endif //ASCENSION_DATATYPES_H

mercurial