-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
96 lines (88 loc) · 3.63 KB
/
engine.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* engine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dcelsa <dcelsa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/04 02:39:57 by dcelsa #+# #+# */
/* Updated: 2022/03/04 17:15:24 by dcelsa ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void quartrot(t_cart *pos, t_axis *axis)
{
double mlts[3][3];
t_cart axsr;
axsr.x = sin(axis->ang / 2) * axis->vector.x;
axsr.y = sin(axis->ang / 2) * axis->vector.y;
axsr.z = sin(axis->ang / 2) * axis->vector.z;
mlts[0][0] = (1 - 2 * (pow(axsr.y, 2) + pow(axsr.z, 2))) * pos->x;
mlts[0][1] = (2 * (axsr.x * axsr.y - axsr.z * cos(axis->ang / 2))) * pos->y;
mlts[0][2] = (2 * (axsr.x * axsr.z + axsr.y * cos(axis->ang / 2))) * pos->z;
mlts[1][0] = (2 * (axsr.x * axsr.y + axsr.z * cos(axis->ang / 2))) * pos->x;
mlts[1][1] = (1 - 2 * (pow(axsr.x, 2) + pow(axsr.z, 2))) * pos->y;
mlts[1][2] = (2 * (axsr.y * axsr.z - axsr.x * cos(axis->ang / 2))) * pos->z;
mlts[2][0] = (2 * (axsr.x * axsr.z - axsr.y * cos(axis->ang / 2))) * pos->x;
mlts[2][1] = (2 * (axsr.y * axsr.z + axsr.x * cos(axis->ang / 2))) * pos->y;
mlts[2][2] = (1 - 2 * (pow(axsr.x, 2) + pow(axsr.y, 2))) * pos->z;
pos->x = mlts[0][0] + mlts[0][1] + mlts[0][2];
pos->y = mlts[1][0] + mlts[1][1] + mlts[1][2];
pos->z = mlts[2][0] + mlts[2][1] + mlts[2][2];
}
void engine(t_win *win, t_dot *dots)
{
int i;
int j;
quartrot(&dots->norm.vector, &win->view.axis);
i = -1;
while (++i < dots->rows)
{
j = -1;
while (++j < dots->cols)
{
quartrot(&dots->dots[i][j], &win->view.axis);
dots->pos[i][j].x = dots->dots[i][j].x * win->view.scale.cur;
dots->pos[i][j].y = dots->dots[i][j].y * win->view.scale.cur;
dots->pos[i][j].z = dots->dots[i][j].z * win->view.scale.cur;
if (win->view.perspective)
{
dots->pos[i][j].x *= win->view.focus
/ (win->view.dstnc + dots->pos[i][j].z);
dots->pos[i][j].y *= win->view.focus
/ (win->view.dstnc + dots->pos[i][j].z);
}
}
}
win->view.axis.ang = 0;
}
void vectorbuilder(double x, double y, double z, t_axis *vector)
{
vector->length = sqrt(x * x + y * y + z * z);
vector->vector.x = x / vector->length;
vector->vector.y = y / vector->length;
vector->vector.z = z / vector->length;
if (!vector->ang)
vector->ang = DEFANG * M_PI / 180;
}
void axisbuilder(t_axis *v1, t_axis *v2, t_axis *axis)
{
double scalar;
axis->vector.x = v1->vector.y * v2->vector.z - v1->vector.z * v2->vector.y;
axis->vector.y = v1->vector.z * v2->vector.x - v1->vector.x * v2->vector.z;
axis->vector.z = v1->vector.x * v2->vector.y - v1->vector.y * v2->vector.x;
axis->length = sqrt(pow(axis->vector.x, 2) + pow(axis->vector.y, 2)
+ pow(axis->vector.z, 2));
scalar = axis->length;
scalar /= (scalar * (scalar >= 1) + (scalar < 1));
axis->ang = asin(scalar);
scalar = v1->vector.x * v2->vector.x + v1->vector.y * v2->vector.y
+ v1->vector.z * v2->vector.z;
if (scalar < 0)
axis->ang = M_PI - axis->ang;
if (!axis->length)
return ;
axis->vector.x /= axis->length;
axis->vector.y /= axis->length;
axis->vector.z /= axis->length;
}