world matrix

This commit is contained in:
jasobake 2025-07-13 01:14:41 -07:00
parent 6be5527b37
commit 17e5ad9b85
4 changed files with 23 additions and 13 deletions

View File

@ -4,7 +4,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include "triangle.h"
#define FPS 30

View File

@ -3,7 +3,6 @@
#include <stdbool.h>
#include <math.h>
#include <SDL.h>
#include <SDL_ttf.h>
#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;

View File

@ -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;
}
}
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;
}

View File

@ -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);