diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..56fffdb --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(t3dsr C) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") + +include_directories(src) + +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c) + +add_executable(main ${SOURCES}) \ No newline at end of file diff --git a/src/main.c b/src/main.c index d96d838..a201ef7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,7 @@ #include "math/vec3.h" #include -#define printVector3(v) { printf("(%f, %f, %f)", v.x, v.y, v.z); } - -int main() +int main(void) { - Vec3 vec = vec3(1, 1, 1); - Vec3 val = vec3_add(vec, vec3(1, 2, 3)); - printVector3(val); return 0; -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/math/mat3.c b/src/math/mat3.c index ca76001..b6f69d2 100644 --- a/src/math/mat3.c +++ b/src/math/mat3.c @@ -8,12 +8,12 @@ Mat3_t mat3(const float arr[9]) return mat; } -Mat3_t mat3_zro() +Mat3_t mat3_zro(void) { return (Mat3_t){0}; } -Mat3_t mat3_ity() +Mat3_t mat3_ity(void) { return (Mat3_t) {{ 1, 0, 0, @@ -115,4 +115,4 @@ Mat3_t mat3_inv(const Mat3_t* m) { inv.m[8] = (m->m[0] * m->m[4] - m->m[1] * m->m[3]) * invDet; return inv; -} \ No newline at end of file +} diff --git a/src/math/mat3.h b/src/math/mat3.h index 0095ea4..40ed167 100644 --- a/src/math/mat3.h +++ b/src/math/mat3.h @@ -8,9 +8,9 @@ typedef struct Mat3_t mat3(const float arr[9]); -Mat3_t mat3_zro(); +Mat3_t mat3_zro(void); -Mat3_t mat3_ity(); +Mat3_t mat3_ity(void); Mat3_t mat3_add(const Mat3_t* m1, const Mat3_t* m2); @@ -26,4 +26,4 @@ float mat3_det(const Mat3_t* m); Mat3_t mat3_inv(const Mat3_t* m); -#endif // MATRIX3_H \ No newline at end of file +#endif // MATRIX3_H diff --git a/src/math/mat4.c b/src/math/mat4.c new file mode 100644 index 0000000..5124c44 --- /dev/null +++ b/src/math/mat4.c @@ -0,0 +1,77 @@ +#include "mat4.h" +#include + +Mat4_t mat4(const float arr[16]) +{ + Mat4_t mat; + memcpy(mat.m, arr, 16*sizeof(float)); + return mat; +} + +Mat4_t mat4_zro(void) +{ + return (Mat4_t){0}; +} + +Mat4_t mat4_ity(void) +{ + return (Mat4_t) {{ + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1, + }}; +} + +Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2) +{ + Mat4_t mat; + + for(int i = 0; i<16; i++) { + mat.m[i] = m1->m[i] + m2->m[i]; + } + + return mat; +} + +Mat4_t mat4_sub(const Mat4_t* m1, const Mat4_t* m2) +{ + Mat4_t mat; + + for(int i = 0; i<16; i++) { + mat.m[i] = m1->m[i] - m2->m[i]; + } + + return mat; +} + +Mat4_t mat4_scl(const Mat4_t* m, float scalar) +{ + Mat4_t mat; + + for(int i = 0; i<16; i++) { + mat.m[i] = m->m[i] * scalar; + } + + return mat; +} + +Mat4_t mat4_mul(const Mat4_t* m1, const Mat4_t* m2) +{ + Mat4_t mat; + + for(int i = 0; i<4; i++) { + int i3 = i * 3; + for (int j = 0; j < 4; j++) { + float sum = 0; + + for (int k = 0; k < 3; k++) { + sum += m1->m[i3 + k] * m2->m[k*3 + j]; + } + + mat.m[i3 + j] = sum; + } + } + + return mat; +} \ No newline at end of file diff --git a/src/math/mat4.h b/src/math/mat4.h new file mode 100644 index 0000000..de0f96b --- /dev/null +++ b/src/math/mat4.h @@ -0,0 +1,30 @@ +#ifndef MATRIX4_H +#define MATRIX4_H + +typedef struct +{ + float m[16]; +} Mat4_t; + +Mat4_t mat4(const float arr[16]); + +Mat4_t mat4_zro(void); + +Mat4_t mat4_ity(void); + +Mat4_t mat4_add(const Mat4_t* m1, const Mat4_t* m2); + +Mat4_t mat4_sub(const Mat4_t* m1, const Mat4_t* m2); + +Mat4_t mat4_scl(const Mat4_t* m, float scalar); + +// row * col +Mat4_t mat4_mul(const Mat4_t* m1, const Mat4_t* m2); + +Mat4_t mat4_tpo(const Mat4_t* m); + +float mat4_det(const Mat4_t* m); + +Mat4_t mat4_inv(const Mat4_t* m); + +#endif // MATRIX4_H diff --git a/src/math/vec3.c b/src/math/vec3.c index ed1a243..36d2724 100644 --- a/src/math/vec3.c +++ b/src/math/vec3.c @@ -103,4 +103,4 @@ Vec3_t vec3_rotate(Vec3_t v, Vec3_t axis, float angle) ); return rlt; -} \ No newline at end of file +} diff --git a/src/math/vec3.h b/src/math/vec3.h index 7f4fc6e..b393108 100644 --- a/src/math/vec3.h +++ b/src/math/vec3.h @@ -34,4 +34,4 @@ float vec3_rist(Vec3_t a, Vec3_t b); Vec3_t vec3_rotate(Vec3_t v, Vec3_t axis, float angle); -#endif // VEC3__H \ No newline at end of file +#endif // VEC3__H diff --git a/src/math/vec4.c b/src/math/vec4.c index b29c568..13bfd1f 100644 --- a/src/math/vec4.c +++ b/src/math/vec4.c @@ -94,4 +94,4 @@ float vec4_dist(Vec4_t a, Vec4_t b) Vec4_t vsub = vec4_sub(a, b); float rlt = vec4_len(vsub); return rlt; -} \ No newline at end of file +} diff --git a/src/math/vec4.h b/src/math/vec4.h index 104d8f1..ef049b0 100644 --- a/src/math/vec4.h +++ b/src/math/vec4.h @@ -30,4 +30,4 @@ Vec4_t vec4_refl(Vec4_t v, Vec4_t normal); float vec4_dist(Vec4_t a, Vec4_t b); -#endif // VECTOR4_H \ No newline at end of file +#endif // VECTOR4_H