This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_math.c
294 lines (244 loc) · 7.78 KB
/
t_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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "t_math.h"
#include <stdint.h>
void t_printMatrix(t_Matrix* m1) {
int row;
for (row = 0; row < 4; row++)
printf("[ %f %f %f %f ]\n", m1->m[row][0], m1->m[row][1], m1->m[row][2], m1->m[row][3]);
}
// out = m1*m2
void t_multiMM(t_Matrix* m1, t_Matrix* m2, t_Matrix* out) {
int i,j,k;
float s;
for(i = 0; i < 4; i++) for(j = 0; j < 4; j++) {
s = 0.0;
for(k=0; k<4; k++)
s += m1->m[i][k] * m2->m[k][j];
out->m[i][j]=s;
}
}
void t_multiMV(t_Matrix * m1, t_Point * v1, t_Point * out)
{
int i, j;
for (i = 0; i < 3; i++)
{
out->m[i] = 0.0;
for (j = 0; j < 3; j++)
{
out->m[i] += m1->m[i][j] * v1->m[i];
};
}
}
// test 2
void t_rotate(float angle, int axis, t_Matrix* out) {
float s = sinf(angle * C);
float c = cosf(angle * C);
*out = (t_Matrix) {{
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0}
}};
// Construct the rotation matrices by substituting
// into the identity matrix
switch (axis) {
case X_AXIS:
out->m[1][1] = c; out->m[1][2] = -s;
out->m[2][1] = s; out->m[2][2] = c;
break;
case Y_AXIS:
// Right handed
out->m[0][0] = c; out->m[0][2] = s;
out->m[2][0] = -s; out->m[2][2] = c;
break;
case Z_AXIS:
out->m[0][0] = c; out->m[0][1] = -s;
out->m[1][0] = s; out->m[1][1] = c;
break;
}
}
// v1 + v2 = out
void t_vectAdd(t_Point* v1, t_Point* v2, t_Point* out) {
out->m[_X] = v1->m[_X] + v2->m[_X];
out->m[_Y] = v1->m[_Y] + v2->m[_Y];
out->m[_Z] = v1->m[_Z] + v2->m[_Z];
out->m[_W] = v1->m[_W] + v2->m[_W];
}
// v1 - v2 = out
void t_vectSub(t_Point* v1, t_Point* v2, t_Point* out) {
out->m[_X] = v1->m[_X] - v2->m[_X];
out->m[_Y] = v1->m[_Y] - v2->m[_Y];
out->m[_Z] = v1->m[_Z] - v2->m[_Z];
out->m[_W] = v1->m[_W] - v2->m[_W];
}
// returns v1 . v2
float t_vectDot(t_Point *v1, t_Point *v2) {
return v1->m[_X] * v2->m[_X]
+ v1->m[_Y] * v2->m[_Y]
+ v1->m[_Z] * v2->m[_Z];
// We are not concerned with w.
}
// v1 x v2 = out
void t_vectCross(t_Point *v1, t_Point *v2, t_Point *out) {
out->m[_X] = v1->m[_Y] * v2->m[_Z] - v1->m[_Z] * v2->m[_Y];
out->m[_Y] = v1->m[_Z] * v2->m[_X] - v1->m[_X] * v2->m[_Z];
out->m[_Z] = v1->m[_X] * v2->m[_Y] - v1->m[_Y] * v2->m[_X];
out->m[_W] = 1; // Cross product is only for 3-vectors.
}
// Put faces in correct order
void t_reorderFace(t_Face *in) {
// Formula N = (P2 - P1) x (P3 - P1) to calculate normal.
t_Point P2mP1;
t_Point P3mP1;
t_vectSub(&(in->p2), &(in->p1), &P2mP1);
t_vectSub(&(in->p3), &(in->p1), &P3mP1);
t_Point calculated_normal;
t_vectCross(&P2mP1, &P3mP1, &calculated_normal);
// If dot product is less than 0, the normal is backwards.
float dot_product = t_vectDot(&(in->normal), &calculated_normal);
if(dot_product < 0.0) {
// This reverses the order of the faces
t_Point tmp = in->p3;
in->p3 = in->p1;
in->p1 = tmp;
}
}
// w - width
// h - height
// FOV - field of view (in degrees)
// 1 and 1000 are used for near and far planes
void t_genProj(t_Matrix * proj, int *w, int *h, float *FOV) {
float AR, FOV_R, t, b, l, r; // top, bottom, left, right
float f = 1000.0; // far
float n = 1.0; // near
AR = (float) *w / (float) *h; // aspect ratio
FOV_R = *FOV * C;
t = n * tan(FOV_R / 2);
// b = -t;
r = t * AR;
// l = -r;
// Simplified, because r + l = 0, and r - l = 2r (same with t, b)
t_Matrix out = {{
{n / r, 0, 0, 0},
{0, n / t, 0, 0},
{0, 0, -(f + n)/(f - n), -2 * f * n / (f - n)},
{0, 0, -1, 0}
}};
*proj = out;
}
// Transpose the top left 3x3 submatrix of a 4x4 matrix.
// Matrix is changed.
void t_transpose(t_Matrix *m1) {
t_Matrix m2 = *m1;
m1->m[0][1] = m2.m[1][0];
m1->m[1][0] = m2.m[0][1];
m1->m[0][2] = m2.m[2][0];
m1->m[2][0] = m2.m[0][2];
m1->m[2][3] = m2.m[3][2];
m1->m[3][2] = m2.m[2][3];
}
// substitutes the translation into the matrix:
// [1 0 0 X].
// [0 1 0 Y].
// [0 0 1 Z].
// [0 0 0 1].
void t_translate(t_Matrix *in, float translation[3]) {
in->m[_X][3] = translation[_X];
in->m[_Y][3] = translation[_Y];
in->m[_Z][3] = translation[_Z];
}
// Generates camera matrix, and regenerates it.
// viewMatrix: pointer to matrix to be regenerated.
// rot_x, rot_y, rot_z: Euler rotations in order XYZ.
// translation: Vector containing x, y, z of camera position.
void t_genModel(t_Matrix *out, t_Model * model) {
t_Matrix x, y, z, r;
// Generate rotation matrices
t_rotate(model->rot[_X], X_AXIS, &x);
t_rotate(model->rot[_Y], Y_AXIS, &y);
t_rotate(model->rot[_Z], Z_AXIS, &z);
// Rotation order XYZ. Combine rotation matrices.
t_multiMM(&x, &y, &r);
t_multiMM(&r, &z, &r);
// Combine translation with rotation matrix
t_translate(&r, model->pos);
*out = r;
}
void t_genView(t_Matrix* viewMatrix, t_Model * model) {
// Generate model matrix for the camera.
t_genModel(viewMatrix, model);
// Invert rotation in viewMatrix using M^T = M^{-1}
t_transpose(viewMatrix);
// Invert translation by negating coordinates.
viewMatrix->m[_X][_W] = -viewMatrix->m[_X][_W];
viewMatrix->m[_Y][_W] = -viewMatrix->m[_Y][_W];
viewMatrix->m[_Z][_W] = -viewMatrix->m[_Z][_W];
}
void t_divPer(t_Face * in, int WIDTH, int HEIGHT) {
int W2 = WIDTH / 2;
int H2 = HEIGHT / 2;
in->p1.m[_X] /= in->p1.m[_Z];
in->p1.m[_Y] /= in->p1.m[_Z];
in->p2.m[_X] /= in->p2.m[_Z];
in->p2.m[_Y] /= in->p2.m[_Z];
in->p3.m[_X] /= in->p3.m[_Z];
in->p3.m[_Y] /= in->p3.m[_Z];
in->p1.m[_Z] = in->p1.m[_W] = 1;
in->p2.m[_Z] = in->p2.m[_W] = 1;
in->p3.m[_Z] = in->p3.m[_W] = 1;
// NDC --> screen space
in->p1.m[_X] = in->p1.m[_X] * W2 + W2;
in->p1.m[_Y] = in->p1.m[_Y] * H2 + H2;
in->p2.m[_X] = in->p2.m[_X] * W2 + W2;
in->p2.m[_Y] = in->p2.m[_Y] * H2 + H2;
in->p3.m[_X] = in->p3.m[_X] * W2 + W2;
in->p3.m[_Y] = in->p3.m[_Y] * H2 + H2;
}
void t_drawLine(int _x1, int _y1, int _x2, int _y2, uint32_t* pixels, int WIDTH, int HEIGHT) {
printf("I got the input ((%d, %d), (%d, %d)).\n", _x1, _y1, _x2, _y2);
int x1 = _x1,
x2 = _x2,
y1 = _y1,
y2 = _y2;
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
if (x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0)
return;
while (x1 != x2 || y1 != y2) {
if (x1 >= 0 && x1 < WIDTH && y1 >= 0 && y1 < HEIGHT) {
pixels[y1 * WIDTH + x1] = 0; // Set pixel to desired color value
}
int err2 = 2 * err;
if (err2 > -dy) {
err -= dy;
x1 += sx;
}
if (err2 < dx) {
err += dx;
y1 += sy;
}
}
}
void t_project(t_Matrix *proj, t_Matrix *view, t_Matrix *model, t_Face faces[MAX_FACES], t_Face out[MAX_FACES], int faces_count, int WIDTH, int HEIGHT) {
t_Matrix T;
t_multiMM(model, view, &T);
t_multiMM(&T, proj, &T);
t_Matrix scale = (t_Matrix) {{
{0.1, 0, 0, 0},
{0, 0.1, 0, 0},
{0, 0, 0.1, 0},
{0, 0, 0, 1}
}};
t_multiMM(&T, &scale, &T);
int i;
// Apply matrices
for (i = 0; i < faces_count; i++) {
t_multiMV(&T, &(faces[i].p1), &(out[i].p1));
t_multiMV(&T, &(faces[i].p2), &(out[i].p2));
t_multiMV(&T, &(faces[i].p3), &(out[i].p3));
// perspective division or something
t_divPer(&(out[i]), WIDTH, HEIGHT);
}
}