diff --git a/src/main.c b/src/main.c index 404c622..7bbf969 100644 --- a/src/main.c +++ b/src/main.c @@ -78,7 +78,7 @@ void update(void) { mesh.rotation.x += .01f; mesh.rotation.y += .01f; - mesh.rotation.z += .01f; + mesh.rotation.z += .02f; int num_faces = array_length(mesh.faces); for (int i = 0; i < num_faces; i++) { diff --git a/src/vector.c b/src/vector.c index 5b64cf2..c4deb79 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,7 +1,45 @@ #include #include "vector.h" -// Implement all vector functons +//////////////////////////////////////////////////////////////////// +/// Vec 2d +//////////////////////////////////////////////////////////////////// +float vec2_length(vec2_t v) { + return sqrt(v.x * v.x + v.y *v.y); +} +vec2_t vec2_add(vec2_t a, vec2_t b) { + vec2_t result = { + .x = a.x + b.x, + .y = a.y + b.y, + }; + return result; +} +vec2_t vec2_sub(vec2_t a, vec2_t b) { + vec2_t result = { + .x = a.x - b.x, + .y = a.y - b.y, + }; + return result; +} +vec2_t vec2_mul(vec2_t a, float factor) { + vec2_t result = { + .x = a.x * factor, + .y = a.y * factor, + }; + return result; +} +vec2_t vec2_div(vec2_t a, float factor){ + vec2_t result = { + .x = a.x / factor, + .y = a.y / factor, + }; + return result; +} + + +//////////////////////////////////////////////////////////////////// +// Vec 3d +//////////////////////////////////////////////////////////////////// vec3_t vec3_rotate_x(vec3_t v, float angle){ vec3_t rotated_vector = { .x = v.x, @@ -25,4 +63,43 @@ vec3_t vec3_rotate_z(vec3_t v, float angle) { .z = v.z }; return rotated_vector; +} +float vec3_length(vec3_t v) { + return sqrt(v.x * v.x + v.y *v.y + v.z *v.z); +} + + +vec3_t vec3_add(vec3_t a, vec3_t b) { + vec3_t result = { + .x = a.x + b.x, + .y = a.y + b.y, + .z = a.z + b.z, + }; + return result; +} + +vec3_t vec3_sub(vec3_t a, vec3_t b) { + vec3_t result = { + .x = a.x - b.x, + .y = a.y - b.y, + .z = a.z - b.z, + }; + return result; +} + +vec3_t vec3_mul(vec3_t a, float factor) { + vec3_t result = { + .x = a.x * factor, + .y = a.y * factor, + .z = a.z * factor, + }; + return result; +} +vec3_t vec3_div(vec3_t a, float factor){ + vec3_t result = { + .x = a.x / factor, + .y = a.y / factor, + .z = a.z / factor, + }; + return result; } \ No newline at end of file diff --git a/src/vector.h b/src/vector.h index 45a5993..3b99320 100644 --- a/src/vector.h +++ b/src/vector.h @@ -2,20 +2,38 @@ #define VECTOR_H typedef struct { - float x; - float y; + float x, y; } vec2_t; typedef struct { - float x; - float y; - float z; + float x, y, z; } vec3_t; +///// +// Vec 2d +///// +float vec2_length(vec2_t v); +vec2_t vec2_add(vec2_t a, vec2_t b); +vec2_t vec2_sub(vec2_t a, vec2_t b); +vec2_t vec2_mul(vec2_t a, float factor); +vec2_t vec2_div(vec2_t a, float factor); + + +///// +// Vec 3d +///// vec3_t vec3_rotate_x(vec3_t v, float angle); vec3_t vec3_rotate_y(vec3_t v, float angle); vec3_t vec3_rotate_z(vec3_t v, float angle); +float vec3_length(vec3_t v); +vec3_t vec3_add(vec3_t a, vec3_t b); +vec3_t vec3_sub(vec3_t a, vec3_t b); +vec3_t vec3_mul(vec3_t a, float factor); +vec3_t vec3_div(vec3_t a, float factor); + + + #endif