-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.c
85 lines (74 loc) · 2.71 KB
/
math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* math.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amajid <amajid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 17:45:53 by amajid #+# #+# */
/* Updated: 2023/12/22 23:26:13 by amajid ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include <math.h>
t_matrix4f get_identity_matrix(void)
{
return ((t_matrix4f){.m[0][0] = 1.0f, .m[1][0] = 0.0f,
.m[2][0] = 0.0f, .m[3][0] = 0.0f,
.m[0][1] = 0.0f, .m[1][1] = 1.0f,
.m[2][1] = 0.0f, .m[3][1] = 0.0f,
.m[0][2] = 0.0f, .m[1][2] = 0.0f,
.m[2][2] = 1.0f, .m[3][2] = 0.0f,
.m[0][3] = 0.0f, .m[1][3] = 0.0f,
.m[2][3] = 0.0f, .m[3][3] = 1.0f});
}
// left = x, right = y, bottom = z, top = w;
t_matrix4f orthogonal(t_point m, float near, float far)
{
t_matrix4f matrix;
matrix = get_identity_matrix();
matrix.m[0][0] = 2 / (m.y - m.x);
matrix.m[1][1] = 2 / (m.w - m.z);
matrix.m[2][2] = -2 / (far - near);
matrix.m[3][2] = (far + near) / (far - near);
matrix.m[3][0] = (m.y + m.x) / (m.y - m.x);
matrix.m[3][1] = (m.w + m.z) / (m.w - m.z);
return (matrix);
}
t_point point_matrix_multiply(t_matrix4f m, t_point v)
{
t_point result;
result = (t_point){v.x * m.m[0][0] + v.y * m.m[1][0]
+ v.z * m.m[2][0] + v.w * m.m[3][0],
v.x * m.m[0][1] + v.y * m.m[1][1]
+ v.z * m.m[2][1] + v.w * m.m[3][1],
v.x * m.m[0][2] + v.y * m.m[1][2]
+ v.z * m.m[2][2] + v.w * m.m[3][2],
v.x * m.m[0][3] + v.y * m.m[1][3]
+ v.z * m.m[2][3] + v.w * m.m[3][3]};
return (result);
}
t_matrix4f matrix4x4_set_scale(t_point vecScale)
{
t_matrix4f result;
result = get_identity_matrix();
result.m[0][0] = vecScale.x;
result.m[1][1] = vecScale.y;
result.m[2][2] = vecScale.z;
return (result);
}
t_point matrix4x4_set_rotation(float flAngle, t_point axis, t_point v)
{
t_point result;
result = v;
if (axis.x == 1.0f)
result = point_matrix_multiply_sse(
matrix4x4_set_rotation_x(flAngle), result);
if (axis.y == 1.0f)
result = point_matrix_multiply_sse(
matrix4x4_set_rotation_y(flAngle), result);
if (axis.z == 1.0f)
result = point_matrix_multiply_sse(
matrix4x4_set_rotation_z(flAngle), result);
return (result);
}