-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix4x4.c
183 lines (157 loc) · 4.3 KB
/
matrix4x4.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <math.h>
#include "matrix4x4.h"
#include "constants.h"
M4x4 m4x4_create_identity()
{
M4x4 res;
res.m[0][0] = 1; res.m[0][1] = 0; res.m[0][2] = 0; res.m[0][3] = 0;
res.m[1][0] = 0; res.m[1][1] = 1; res.m[1][2] = 0; res.m[1][3] = 0;
res.m[2][0] = 0; res.m[2][1] = 0; res.m[2][2] = 1; res.m[2][3] = 0;
res.m[3][0] = 0; res.m[3][1] = 0; res.m[3][2] = 0; res.m[3][3] = 1;
return res;
}
M4x4 m4x4_create_zero()
{
M4x4 res;
res.m[0][0] = 0; res.m[0][1] = 0; res.m[0][2] = 0; res.m[0][3] = 0;
res.m[1][0] = 0; res.m[1][1] = 0; res.m[1][2] = 0; res.m[1][3] = 0;
res.m[2][0] = 0; res.m[2][1] = 0; res.m[2][2] = 0; res.m[2][3] = 0;
res.m[3][0] = 0; res.m[3][1] = 0; res.m[3][2] = 0; res.m[3][3] = 0;
return res;
}
M4x4 m4x4_translate(Vec3 v)
{
M4x4 res = m4x4_create_identity();
res.m[0][3] = v.x;
res.m[1][3] = v.y;
res.m[2][3] = v.z;
return res;
}
M4x4 m4x4_rotate(Vec3 v)
{
float x_r = v.x * (M_PI / 180);
float y_r = v.y * (M_PI / 180);
float z_r = v.z * (M_PI / 180);
M4x4 x = m4x4_rotate_x(x_r);
M4x4 y = m4x4_rotate_y(y_r);
M4x4 z = m4x4_rotate_z(z_r);
M4x4 res = m4x4_mul(z, m4x4_mul(y, x));
return res;
}
M4x4 m4x4_rotate_x(float radians)
{
M4x4 res = m4x4_create_identity();
res.m[0][0] = 1;
res.m[1][1] = cosf(radians);
res.m[1][2] = -sinf(radians);
res.m[2][1] = sinf(radians);
res.m[2][2] = cosf(radians);
return res;
}
M4x4 m4x4_rotate_y(float radians)
{
M4x4 res = m4x4_create_identity();
res.m[0][0] = cosf(radians);
res.m[0][2] = sinf(radians);
res.m[2][0] = -sinf(radians);
res.m[2][2] = cosf(radians);
res.m[1][1] = 1;
return res;
}
M4x4 m4x4_rotate_z(float radians)
{
M4x4 res = m4x4_create_identity();
res.m[0][0] = cosf(radians);
res.m[0][1] = sinf(radians);
res.m[1][0] = -sinf(radians);
res.m[1][1] = cosf(radians);
res.m[2][2] = 1;
return res;
}
M4x4 m4x4_scale(Vec3 v)
{
M4x4 res = m4x4_create_identity();
res.m[0][0] = v.x;
res.m[1][1] = v.y;
res.m[2][2] = v.z;
res.m[3][3] = 1;
return res;
}
M4x4 m4x4_mul(M4x4 m1, M4x4 m2)
{
int i, q, j;
M4x4 res = m4x4_create_zero();
/* m1 rows */
for (i = 0; i < 4; i++)
{
/* Walk through the same row 4 times, */
/* also the column index of m2 */
for (q = 0; q < 4; q++)
{
// m1 columns
for (j = 0; j < 4; j++)
{
res.m[i][q] += m1.m[i][j] * m2.m[j][q];
}
}
}
/*
The below implementation can be used ONLY for linear transformations
Since the translation is affine transformation, we cannot assume
the columns match with the basis vectors of the transofrmations coordinate system,
with 1 at the end of each vector.
*/
//print_matrix(res);
//Vec3 x = get_basis_vector(m2, 0);
//Vec3 y = get_basis_vector(m2, 1);
//Vec3 z = get_basis_vector(m2, 2);
//Vec3 w = get_basis_vector(m2, 3);
//
///* Transform all of the basis vectors of m2 to the m1 coordinate system (m1 = some transformation) */
//Vec3 i = mul_m4x4_vec3(m1, x);
//Vec3 j = mul_m4x4_vec3(m1, y);
//Vec3 k = mul_m4x4_vec3(m1, z);
//Vec3 l = mul_m4x4_vec3(m1, w);
//
//res.m[0][0] = i.x; res.m[0][1] = j.x; res.m[0][2] = k.x; res.m[0][3] = l.x;
//res.m[1][0] = i.y; res.m[1][1] = j.y; res.m[1][2] = k.y; res.m[1][3] = l.y;
//res.m[2][0] = i.z; res.m[2][1] = j.z; res.m[2][2] = k.z; res.m[2][3] = l.z;
//res.m[3][0] = 0; res.m[3][1] = 0; res.m[3][2] = 0; res.m[3][3] = 1;
return res;
}
Vec3 m4x4_mul_vec3(M4x4 m, Vec3 v)
{
/*
Assume last coefficient w is 1,
until there is a homogeneous coordinates implementation
*/
Vec3 res;
res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * 1;
res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * 1;
res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * 1;
/* res.w left out */
return res;
}
/*
0 - the first column (x basis vector)
1 - the second column (y basis vector)
2 - the third column (z basis vector)
*/
Vec3 m4x4_get_basis_vector(M4x4 m, int loc)
{
Vec3 res;
res.x = m.m[0][loc];
res.y = m.m[1][loc];
res.z = m.m[2][loc];
return res;
}
void m4x4_print(M4x4 m)
{
if (!DEBUG)
return;
printf("%f %f %f %f\n", m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3]);
printf("%f %f %f %f\n", m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3]);
printf("%f %f %f %f\n", m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3]);
printf("%f %f %f %f\n", m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]);
}