-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.c
More file actions
59 lines (50 loc) · 1.43 KB
/
Copy pathcamera.c
File metadata and controls
59 lines (50 loc) · 1.43 KB
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
#include "camera.h"
Camera* init_camera(Matrix* pos, Matrix* target, Matrix* up) {
Camera* camera = malloc(sizeof(Camera));
camera->pos = pos;
camera->target = target;
camera->up = up;
camera->xfov = M_PI / 8.0;
camera->yfov = M_PI / 6.0;
return camera;
}
Matrix* get_view_matrix(Camera* camera) {
Matrix* aux = sub_mat(camera->pos, camera->target);
Matrix* zaxis = normalize(aux);
free(aux);
aux = cross3D(camera->up, zaxis);
Matrix* xaxis = normalize(aux);
free(aux);
Matrix* yaxis = cross3D(zaxis, xaxis);
Matrix* view_matrix = init_matrix(4, 4);
for (int i = 0; i < view_matrix->height - 1; i++) {
switch (i) {
case 0:
aux = xaxis;
break;
case 1:
aux = yaxis;
break;
case 2:
aux = zaxis;
}
for (int j = 0; j < view_matrix->width - 1; j++) {
view_matrix->matrix[i][j] = aux->matrix[j][0];
}
view_matrix->matrix[i][3] = -dot(aux, camera->pos);
}
view_matrix->matrix[3][0] = 0.0;
view_matrix->matrix[3][1] = 0.0;
view_matrix->matrix[3][2] = 0.0;
view_matrix->matrix[3][3] = 1.0;
free(xaxis);
free(yaxis);
free(zaxis);
return view_matrix;
}
void free_camera(Camera* camera) { // might be a problem
free(camera->pos);
free(camera->target);
free(camera->up);
free(camera);
}