diff --git a/scripts/display.h b/scripts/display.h index 6119f44..96bf99b 100644 --- a/scripts/display.h +++ b/scripts/display.h @@ -4,7 +4,7 @@ #include #include #include -#include + #include "triangle.h" #define FPS 30 diff --git a/scripts/main.c b/scripts/main.c index d562b9e..f9bdbff 100644 --- a/scripts/main.c +++ b/scripts/main.c @@ -3,7 +3,6 @@ #include #include #include -#include #include "display.h" #include "mesh.h" #include "vector.h" @@ -139,7 +138,7 @@ void update(void) { mesh.translation.x += 0.004f; mesh.translation.z = 5.0f; - //mesh.scale.x += 0.002f; + mesh.scale.x += 0.002f; //mesh.scale.y += 0.002f; // create scale matrix that will be used @@ -153,6 +152,12 @@ void update(void) { mat4_t rotation_matrix_y = mat4_make_rotation_y(mesh.rotation.y); mat4_t rotation_matrix_z = mat4_make_rotation_z(mesh.rotation.z); + // create world matrix + mat4_t world_matrix = mat4_mul_mat4(&scale_matrix, &translation_matrix); + world_matrix = mat4_mul_mat4(&world_matrix, &rotation_matrix_x); + world_matrix = mat4_mul_mat4(&world_matrix, &rotation_matrix_y); + world_matrix = mat4_mul_mat4(&world_matrix, &rotation_matrix_z); + triangles_to_render = NULL; @@ -171,15 +176,7 @@ void update(void) { vec4_t transformed_vertex = vec4_from_vec3(face_vertices[j]); // use matrix to scale original matrix - transformed_vertex = mat4_multiply_vec4(scale_matrix, transformed_vertex); - - // use matrices to rotate - transformed_vertex = mat4_multiply_vec4(rotation_matrix_x, transformed_vertex); - transformed_vertex = mat4_multiply_vec4(rotation_matrix_y, transformed_vertex); - transformed_vertex = mat4_multiply_vec4(rotation_matrix_z, transformed_vertex); - - // use matrix to translate - transformed_vertex = mat4_multiply_vec4(translation_matrix, transformed_vertex); + transformed_vertex = mat4_multiply_vec4(world_matrix, transformed_vertex); // save translated vertex in the array of transformed vertices transformed_vertices[j] = transformed_vertex; diff --git a/scripts/matrix.c b/scripts/matrix.c index 5ab5d0a..cded963 100644 --- a/scripts/matrix.c +++ b/scripts/matrix.c @@ -100,4 +100,16 @@ vec4_t mat4_multiply_vec4(mat4_t m, vec4_t v) { ret.w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w; return ret; -} \ No newline at end of file +} + +mat4_t mat4_mul_mat4(mat4_t* a, mat4_t* b) { + mat4_t ret = {}; + + for (int r = 0; r < 4; r++) { + for (int c = 0; c < 4; c++) { + ret.m[r][c] = (a->m[r][0] * b->m[0][c]) + (a->m[r][1] * b->m[1][c]) + (a->m[r][2] * b->m[2][c]) + (a->m[r][3] * b->m[3][c]); + } + } + + return ret; +} diff --git a/scripts/matrix.h b/scripts/matrix.h index 2dac80d..1d3c55f 100644 --- a/scripts/matrix.h +++ b/scripts/matrix.h @@ -15,6 +15,7 @@ mat4_t mat4_make_translation(float tx, float ty, float tz); mat4_t mat4_make_rotation_x(float ra); mat4_t mat4_make_rotation_y(float ra); mat4_t mat4_make_rotation_z(float ra); +mat4_t mat4_mul_mat4(mat4_t* a, mat4_t* b); vec4_t mat4_multiply_vec4(mat4_t m, vec4_t v);