-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotation_objects.c
90 lines (79 loc) · 2.61 KB
/
rotation_objects.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rotation_objects.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khlavaty <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/12 18:44:28 by khlavaty #+# #+# */
/* Updated: 2024/09/12 19:09:52 by khlavaty ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
void perform_cylinder_rotation(t_cy *cylinder, t_quat q)
{
t_float_3 center;
t_float_3 rotated_vec;
t_float_3 new_pos;
q = quaternion_normalize(q);
center.x = cylinder->pos.x + (cylinder->vec.x * (cylinder->hth / 2.0f));
center.y = cylinder->pos.y + (cylinder->vec.y * (cylinder->hth / 2.0f));
center.z = cylinder->pos.z + (cylinder->vec.z * (cylinder->hth / 2.0f));
rotated_vec = quaternion_rotate_vector(q, cylinder->vec);
new_pos.x = center.x - (rotated_vec.x * (cylinder->hth / 2.0f));
new_pos.y = center.y - (rotated_vec.y * (cylinder->hth / 2.0f));
new_pos.z = center.z - (rotated_vec.z * (cylinder->hth / 2.0f));
cylinder->vec = rotated_vec;
cylinder->pos = new_pos;
}
void perform_plane_rotation(t_pl *plane, t_quat q)
{
t_float_3 rotated_vec;
rotated_vec = quaternion_rotate_vector(q, plane->vec);
plane->vec = rotated_vec;
}
void rotate_object(t_obj *obj, t_quat q)
{
t_cy *cylinder;
t_pl *plane;
if (obj->type == CYLINDER)
{
cylinder = (t_cy *)obj->object;
perform_cylinder_rotation(cylinder, q);
}
else if (obj->type == PLANE)
{
plane = (t_pl *)obj->object;
perform_plane_rotation(plane, q);
}
}
void rotate_cylinder(t_win *win, int keysym, float angle)
{
t_quat q;
t_float_3 axis;
t_obj *obj;
axis = get_rot_axis_cylinder(keysym);
q = quaternion_from_axis_angle(axis, angle);
obj = win->map->objects;
while (obj)
{
if (obj->type == CYLINDER)
rotate_object(obj, q);
obj = obj->next;
}
}
void rotate_plane(t_win *win, int keysym, float angle)
{
t_quat q;
t_float_3 axis;
t_obj *obj;
axis = get_rot_axis_plane(keysym);
q = quaternion_from_axis_angle(axis, angle);
obj = win->map->objects;
while (obj)
{
if (obj->type == PLANE)
rotate_object(obj, q);
obj = obj->next;
}
}