-
Notifications
You must be signed in to change notification settings - Fork 1
/
ray_tracing.c
104 lines (95 loc) · 2.9 KB
/
ray_tracing.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ray_tracing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mghalmi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/28 11:36:32 by mghalmi #+# #+# */
/* Updated: 2024/02/08 14:58:28 by mghalmi ### ########.fr */
/* */
/* ************************************************************************** */
#include "main.h"
void try_all_intersections(t_v3 ray, t_obj *lst,
t_obj *closest_figure, double *closest_intersection)
{
double dist;
while (lst)
{
if (lst->flag == SP)
dist = sphere_intersection(ray.o, ray.d, lst);
else if (lst->flag == PL)
dist = plane_intersection(ray.o, ray.d, lst);
else if (lst->flag == CY)
dist = cylinder_intersection(ray.o, ray.d, lst);
if (dist > EPSILON && dist < *closest_intersection)
{
*closest_figure = *lst;
*closest_intersection = dist;
}
lst = lst->next;
}
}
int trace_ray(t_point o, t_point d, t_wrapper *w, int depth)
{
t_v3 ray;
t_obj cl_fig;
t_inter inter;
double closest_intersection;
(void)depth;
ray.o = o;
ray.d = d;
closest_intersection = INFINITY;
cl_fig.flag = -1;
try_all_intersections(ray, w->lst, &cl_fig, &closest_intersection);
inter.p = vadd(o, scal_x_vec(closest_intersection, d));
calc_normal(inter.p, d, &(inter.normal), &cl_fig);
set_color(cl_fig, &inter, w);
apply_texture(cl_fig.texture, &inter);
compute_light(&inter, w->data, w->lst);
return (inter.color);
}
int average(int color1, int color2)
{
int average[3];
int mask;
int color[2];
int i;
mask = 255;
ft_memset(average, 0, 3 * sizeof(int));
color[0] = color1;
color[1] = color2;
i = 0;
while (i < 2)
{
average[0] += (color[i] & (mask << 16)) >> 16;
average[1] += (color[i] & (mask << 8)) >> 8;
average[2] += color[i] & mask;
i++;
}
average[0] = average[0] / 2;
average[1] = average[1] / 2;
average[2] = average[2] / 2;
return ((average[0] << 16) | (average[1] << 8) | average[2]);
}
int average_supersampled_color(int *color)
{
int ss_color[3];
int mask;
int n;
ft_memset(ss_color, 0, sizeof(int) * 3);
mask = 255;
n = 0;
while (n < 4)
{
ss_color[0] += (color[n] & (mask << 16)) >> 16;
ss_color[1] += (color[n] & (mask << 8)) >> 8;
ss_color[2] += color[n] & mask;
n++;
}
ss_color[0] = ss_color[0] / 4;
ss_color[1] = ss_color[1] / 4;
ss_color[2] = ss_color[2] / 4;
free(color);
return ((ss_color[0] << 16) | (ss_color[1] << 8) | ss_color[2]);
}